本文整理汇总了Python中urllib2.HTTPPasswordMgrWithDefaultRealm方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.HTTPPasswordMgrWithDefaultRealm方法的具体用法?Python urllib2.HTTPPasswordMgrWithDefaultRealm怎么用?Python urllib2.HTTPPasswordMgrWithDefaultRealm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.HTTPPasswordMgrWithDefaultRealm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_status_page
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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: prepare_us_proxy
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def prepare_us_proxy(cookie_handler):
if (addon.getSetting('us_proxy_socks5') == 'true'):
if ((addon.getSetting('us_proxy_pass') is not '') and (addon.getSetting('us_proxy_user') is not '')):
print 'Using socks5 authenticated proxy: ' + addon.getSetting('us_proxy') + ':' + addon.getSetting('us_proxy_port')
socks_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, addon.getSetting('us_proxy'), int(addon.getSetting('us_proxy_port')), True, addon.getSetting('us_proxy_user'), addon.getSetting('us_proxy_pass'))
opener = urllib2.build_opener(socks_handler, cookie_handler)
else:
print 'Using socks5 proxy: ' + addon.getSetting('us_proxy') + ':' + addon.getSetting('us_proxy_port')
socks_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, addon.getSetting('us_proxy'), int(addon.getSetting('us_proxy_port')), True)
opener = urllib2.build_opener(socks_handler, cookie_handler)
elif (addon.getSetting('us_proxy_socks5') == 'false'):
us_proxy = 'http://' + addon.getSetting('us_proxy') + ':' + addon.getSetting('us_proxy_port')
proxy_handler = urllib2.ProxyHandler({'http' : us_proxy})
if ((addon.getSetting('us_proxy_pass') is not '') and (addon.getSetting('us_proxy_user') is not '')):
print 'Using authenticated proxy: ' + us_proxy
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, us_proxy, addon.getSetting('us_proxy_user'), addon.getSetting('us_proxy_pass'))
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler, cookie_handler)
else:
print 'Using proxy: ' + us_proxy
opener = urllib2.build_opener(proxy_handler, cookie_handler)
return opener
示例3: retrieve_status_page
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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)
示例4: bing_search
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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
示例5: get_api_opener_v1
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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: _createUser
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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, }
示例7: run
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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
示例8: run
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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
示例9: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [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
示例10: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def __init__(self, base_url, exc_class=None, logger=None):
"""
@param base_url: The base url to the API.
@param exc_class: An exception class to handle non-200 results.
Creates an HTTP(S) client to connect to the Cloudera Manager API.
"""
self._base_url = base_url.rstrip('/')
self._exc_class = exc_class or RestException
self._logger = logger or LOG
self._headers = { }
# Make a basic auth handler that does nothing. Set credentials later.
self._passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
authhandler = urllib2.HTTPBasicAuthHandler(self._passmgr)
# Make a cookie processor
cookiejar = cookielib.CookieJar()
self._opener = urllib2.build_opener(
HTTPErrorProcessor(),
urllib2.HTTPCookieProcessor(cookiejar),
authhandler)
示例11: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def __init__(self, **kwargs):
"""
@param kwargs: Keyword arguments.
- B{proxy} - An HTTP proxy to be specified on requests.
The proxy is defined as {protocol:proxy,}
- type: I{dict}
- default: {}
- B{timeout} - Set the URL open timeout (seconds).
- type: I{float}
- default: 90
- B{username} - The username used for HTTP authentication.
- type: I{str}
- default: None
- B{password} - The password used for HTTP authentication.
- type: I{str}
- default: None
"""
HttpTransport.__init__(self, **kwargs)
self.pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
示例12: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def __init__(self, username, password, # pylint: disable=E1002
verbose=0, use_datetime=False, https_handler=None):
"""Initialize"""
if PY2:
Transport.__init__(self, use_datetime=False)
else:
super().__init__(use_datetime=False)
self._username = username
self._password = password
self._use_datetime = use_datetime
self.verbose = verbose
self._username = username
self._password = password
self._handlers = []
if self._username and self._password:
self._passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
self._auth_handler = urllib2.HTTPDigestAuthHandler(self._passmgr)
else:
self._auth_handler = None
self._passmgr = None
if https_handler:
self._handlers.append(https_handler)
self._scheme = 'https'
else:
self._scheme = 'http'
if self._auth_handler:
self._handlers.append(self._auth_handler)
示例13: _download_url
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def _download_url(cls, url, data_dict=None, auth_user=None, auth_passwd=None):
"""Download data from given URL
Args:
url: URL
data_dict: optional dictionary to pass in POST request
auth_user: optional user identifier
auth_passwd: optional user password
Returns:
urllib2.urlopen: data downloaded from URL
or None when error occures
"""
if auth_user is not None and auth_passwd is not None:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, auth_user, auth_passwd)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
else:
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
if data_dict is not None:
data_string = urllib.urlencode(data_dict)
req = urllib2.Request(url, data_string)
else:
req = urllib2.Request(url)
data = None
try:
data = opener.open(req, timeout=60)
except urllib2.URLError:
pass
return data
示例14: urlretrieve
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def urlretrieve(url, filename, data=None, auth=None):
if auth is not None:
# https://docs.python.org/2.7/howto/urllib2.html#id6
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
# Add the username and password.
# If we knew the realm, we could use it instead of None.
username, password = auth
top_level_url = urlparse(url).netloc
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib2.build_opener(handler)
else:
opener = urllib2.build_opener()
res = opener.open(url, data=data)
headers = res.info()
with open(filename, "wb") as fp:
fp.write(res.read())
return filename, headers
示例15: start
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgrWithDefaultRealm [as 别名]
def start(self):
if not self.is_available():
return
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
webdis_url = "http://%s:%s/" % (self.hostname, self.port)
password_mgr.add_password(None, webdis_url, self.username, self.password)
self.opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(password_mgr))