本文整理汇总了Python中urllib2.HTTPBasicAuthHandler方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.HTTPBasicAuthHandler方法的具体用法?Python urllib2.HTTPBasicAuthHandler怎么用?Python urllib2.HTTPBasicAuthHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.HTTPBasicAuthHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_status_page
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def retrieve_status_page(user, password, url):
try:
ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = ssl._create_unverified_context
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, user, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
req = urllib2.Request(url)
try:
response = urllib2.urlopen(req)
return response.read()
except Exception:
raise CrawlError("can't access to http://%s", url)
示例2: retrieve_status_page
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def retrieve_status_page(hostname, port, user, password):
statusPage = "http://%s:%s/manager/status?XML=true" % (hostname, port)
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, statusPage, user, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
req = urllib2.Request(statusPage)
try:
response = urllib2.urlopen(req)
return response.read()
except Exception:
raise CrawlError("can't access to http://%s:%s",
hostname, port)
示例3: bing_search
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def bing_search(query, key, offset, **kwargs):
''' Make the search '''
username = ''
baseURL = 'https://api.datamarket.azure.com/Bing/Search/'
query = urllib.quote(query)
user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; FDM; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 1.1.4322)'
credentials = (':%s' % key).encode('base64')[:-1]
auth = 'Basic %s' % credentials
url = baseURL+'Web?Query=%27'+query+'%27&$top=50&$format=json&$skip='+offset
print '[*] Fetching '+url
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, username, key)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
try:
readURL = urllib2.urlopen(url, timeout=60).read()
except Exception as e:
sys.exit('[-] Failed to fetch bing results. Are you sure you have the right API key?\n Error: '+str(e))
return readURL
示例4: test_basic_auth_with_unquoted_realm
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def test_basic_auth_with_unquoted_realm(self):
opener = OpenerDirector()
password_manager = MockPasswordManager()
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
realm = "ACME Widget Store"
http_handler = MockHTTPHandler(
401, 'WWW-Authenticate: Basic realm=%s\r\n\r\n' % realm)
opener.add_handler(auth_handler)
opener.add_handler(http_handler)
msg = "Basic Auth Realm was unquoted"
with test_support.check_warnings((msg, UserWarning)):
self._test_basic_auth(opener, auth_handler, "Authorization",
realm, http_handler, password_manager,
"http://acme.example.com/protected",
"http://acme.example.com/protected"
)
示例5: get_api_opener_v1
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def get_api_opener_v1(url, username, password):
"""
get the http api opener with base url and username,password
:param url: http url
:param username: username for api auth
:param password: password for api auth
"""
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
password_mgr.add_password(None, url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
return opener
示例6: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def __init__(self, configuration):
self.setup(configuration)
self.echo = None
if "ECHO" in configuration:
self.echo = configuration['ECHO']
if self.proxy_scheme is not None and self.proxy_host is not None and \
self.proxy_port is not None:
credentials = ""
if self.proxy_username is not None and self.proxy_password is not None:
credentials = self.proxy_username + ":" + self.proxy_password + "@"
proxyDict = {
self.proxy_scheme: self.proxy_scheme + "://" + credentials +
self.proxy_host + ":" + self.proxy_port
}
proxy = urllib2.ProxyHandler(proxyDict)
if credentials != '':
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
else:
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
示例7: _createUser
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def _createUser(self, number):
record = self._records[number]
user = record.uid
authBasic = HTTPBasicAuthHandler(password_mgr=HTTPPasswordMgrWithDefaultRealm())
authBasic.add_password(
realm=None,
uri=self.servers[record.podID]["uri"],
user=user.encode('utf-8'),
passwd=record.password.encode('utf-8'))
authDigest = HTTPDigestAuthHandler(passwd=HTTPPasswordMgrWithDefaultRealm())
authDigest.add_password(
realm=None,
uri=self.servers[record.podID]["uri"],
user=user.encode('utf-8'),
passwd=record.password.encode('utf-8'))
return record, user, {"basic": authBasic, "digest": authDigest, }
示例8: run
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def run(self):
password = getword()
try:
print "-"*12
print "User:",username,"Password:",password
req = urllib2.Request(sys.argv[1])
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, sys.argv[1], username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
fd = opener.open(req)
print "\t\n\n[+] Login successful: Username:",username,"Password:",password,"\n"
print "[+] Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
sys.exit(2)
except (urllib2.HTTPError,socket.error):
pass
示例9: run
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def run(self):
username, password = getword()
try:
print "-"*12
print "User:",username,"Password:",password
req = urllib2.Request(sys.argv[1])
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, sys.argv[1], username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
fd = opener.open(req)
print "\t\n\nUsername:",username,"Password:",password,"----- Login successful!!!\n\n"
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
sys.exit(2)
except (urllib2.HTTPError, httplib.BadStatusLine,socket.error), msg:
print "An error occurred:", msg
pass
示例10: send
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def send(self, data):
"""
Send data via sendall.
@type data: string
@param data: Data to send
"""
passmgr = urllib2.HTTPPasswordMgr()
passmgr.add_password(self._realm, self._url, self._username, self._password)
auth_handler = urllib2.HTTPBasicAuthHandler(passmgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request(self._url, data, self._headers)
try:
self._fd = urllib2.urlopen(req)
except:
self._fd = None
示例11: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def __init__(self, userCredentials=None, retries=3, sleepTime=5):
DirectoryWalker.__init__(self, userCredentials, retries, sleepTime)
if self.userCredentials:
if self.userCredentials.httpProxy:
os.environ['http_proxy'] = self.userCredentials.httpProxy
# global kludge, default proxyHandler looks up proxy there
passwordMgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
for url, cred in self.userCredentials.credentials.iteritems():
passwordMgr.add_password(None, url, cred.username, cred.password)
authHandler = urllib2.HTTPBasicAuthHandler(passwordMgr)
opener = urllib2.build_opener(authHandler)
else:
# opener = urllib2.build_opener()
opener = None
# opener.add_headers = [('User-agent', 'Mozilla/5.0')]
self.opener = opener
示例12: test_basic_auth_success
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def test_basic_auth_success(self):
ah = urllib2.HTTPBasicAuthHandler()
ah.add_password(self.REALM, self.server_url, self.USER, self.PASSWD)
urllib2.install_opener(urllib2.build_opener(ah))
try:
self.assertTrue(urllib2.urlopen(self.server_url))
except urllib2.HTTPError:
self.fail("Basic Auth Failed for url: %s" % self.server_url)
except Exception as e:
raise e
示例13: test_basic_auth_httperror
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def test_basic_auth_httperror(self):
ah = urllib2.HTTPBasicAuthHandler()
ah.add_password(self.REALM, self.server_url, self.USER,
self.INCORRECT_PASSWD)
urllib2.install_opener(urllib2.build_opener(ah))
self.assertRaises(urllib2.HTTPError, urllib2.urlopen, self.server_url)
示例14: test_basic_auth
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def test_basic_auth(self, quote_char='"'):
opener = OpenerDirector()
password_manager = MockPasswordManager()
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
realm = "ACME Widget Store"
http_handler = MockHTTPHandler(
401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
(quote_char, realm, quote_char) )
opener.add_handler(auth_handler)
opener.add_handler(http_handler)
self._test_basic_auth(opener, auth_handler, "Authorization",
realm, http_handler, password_manager,
"http://acme.example.com/protected",
"http://acme.example.com/protected"
)
示例15: drop
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPBasicAuthHandler [as 别名]
def drop(host, port, image, payload):
# here is where we set all of our proxy settings
if PROXY_SUPPORT == "ON":
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='RESTRICTED ACCESS',
uri=PROXY_URL, # PROXY SPECIFIED ABOVE
user=USERNAME, # USERNAME SPECIFIED ABOVE
passwd=PASSWORD) # PASSWORD SPECIFIED ABOVE
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
#Grab our file file from the web server and save it to a file
req = urllib2.Request('http://%s:%s/%s' % (host,port,image))
message = urllib2.urlopen(req)
localFile = open('temp.png', 'w')
localFile.write(message.read())
localFile.close()
#Destego binary
inp = cv.LoadImage('temp.png')
steg = LSBSteg(inp)
bin = steg.unhideBin()
f = open(payload,"wb") #Write the binary back to a file
f.write(bin)
f.close()
os.system('rm temp.png')