'''
Created on 2015-9-29
@author: Administrator
'''
#/usr/bin/env python
#Obtain Web Page Information With Authentication - Chapter 6 - dump_info.py
import sys, urllib2, getpass
class TermanalPassword(urllib2.HTTPPasswordMgr):
def find_user_password(self, realm, authuri):
retval = urllib2.HTTPPasswordMgr.find_user_password(self, realm, authuri)
if retval[0] == None and retval[1] == None:
#Did not find it in stored values; prompt user
sys.stdout.write("Login required for %s at %s\n" % (realm, authuri))
sys.stdout.write("Username: ")
username = sys.stdin.readline().rstrip()
password = getpass.getpass().rstrip()
else:
return retval
req = urllib2.Request(sys.argv[1])
opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(TermanalPassword()))
fd = opener.open(req)
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
python登錄認證HTTP並打印頭信息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.