本文整理汇总了Python中urllib.request.HTTPPasswordMgrWithDefaultRealm方法的典型用法代码示例。如果您正苦于以下问题:Python request.HTTPPasswordMgrWithDefaultRealm方法的具体用法?Python request.HTTPPasswordMgrWithDefaultRealm怎么用?Python request.HTTPPasswordMgrWithDefaultRealm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib.request
的用法示例。
在下文中一共展示了request.HTTPPasswordMgrWithDefaultRealm方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request 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)
示例2: urlretrieve
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request 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
示例3: get
# 需要导入模块: from urllib import request [as 别名]
# 或者: from urllib.request import HTTPPasswordMgrWithDefaultRealm [as 别名]
def get(self, uri, params={}, headers={}, with_status_code=False, timeout=10, user=None, password=None):
data = None # always none in GET
if params:
uri = "%s?%s" % (uri, urlencode(params))
# SSL, user/password and basic
# NOTE: currently don't manage ssl & user/password
if uri.startswith('https://'):
handler = HTTPSHandler(context=self.ssl_context)
elif user and password:
passwordMgr = HTTPPasswordMgrWithDefaultRealm()
passwordMgr.add_password(None, uri, user, password)
handler = HTTPBasicAuthHandler(passwordMgr)
else:
handler = HTTPHandler
url_opener = build_opener(handler)
req = Request(uri, data)
req.get_method = lambda: 'GET'
for (k, v) in headers.items():
req.add_header(k, v)
request = url_opener.open(req, timeout=timeout)
response = request.read()
status_code = request.code
request.close()
if not with_status_code:
return response
else:
return (status_code, response)