本文整理汇总了Python中urllib2.HTTPPasswordMgr方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.HTTPPasswordMgr方法的具体用法?Python urllib2.HTTPPasswordMgr怎么用?Python urllib2.HTTPPasswordMgr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.HTTPPasswordMgr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgr [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.HTTPDigestAuthHandler(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
示例2: __init__
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgr [as 别名]
def __init__(self, password_mgr=None, debuglevel=0):
"""Initialize an instance of a AbstractNtlmAuthHandler.
Verify operation with all default arguments.
>>> abstrct = AbstractNtlmAuthHandler()
Verify "normal" operation.
>>> abstrct = AbstractNtlmAuthHandler(urllib2.HTTPPasswordMgrWithDefaultRealm())
"""
if password_mgr is None:
password_mgr = urllib2.HTTPPasswordMgr()
self.passwd = password_mgr
self.add_password = self.passwd.add_password
self._debuglevel = debuglevel
示例3: test_password_manager_default_port
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgr [as 别名]
def test_password_manager_default_port(self):
"""
>>> mgr = urllib2.HTTPPasswordMgr()
>>> add = mgr.add_password
The point to note here is that we can't guess the default port if there's
no scheme. This applies to both add_password and find_user_password.
>>> add("f", "http://g.example.com:80", "10", "j")
>>> add("g", "http://h.example.com", "11", "k")
>>> add("h", "i.example.com:80", "12", "l")
>>> add("i", "j.example.com", "13", "m")
>>> mgr.find_user_password("f", "g.example.com:100")
(None, None)
>>> mgr.find_user_password("f", "g.example.com:80")
('10', 'j')
>>> mgr.find_user_password("f", "g.example.com")
(None, None)
>>> mgr.find_user_password("f", "http://g.example.com:100")
(None, None)
>>> mgr.find_user_password("f", "http://g.example.com:80")
('10', 'j')
>>> mgr.find_user_password("f", "http://g.example.com")
('10', 'j')
>>> mgr.find_user_password("g", "h.example.com")
('11', 'k')
>>> mgr.find_user_password("g", "h.example.com:80")
('11', 'k')
>>> mgr.find_user_password("g", "http://h.example.com:80")
('11', 'k')
>>> mgr.find_user_password("h", "i.example.com")
(None, None)
>>> mgr.find_user_password("h", "i.example.com:80")
('12', 'l')
>>> mgr.find_user_password("h", "http://i.example.com:80")
('12', 'l')
>>> mgr.find_user_password("i", "j.example.com")
('13', 'm')
>>> mgr.find_user_password("i", "j.example.com:80")
(None, None)
>>> mgr.find_user_password("i", "http://j.example.com")
('13', 'm')
>>> mgr.find_user_password("i", "http://j.example.com:80")
(None, None)
"""
示例4: _fetchUrl
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import HTTPPasswordMgr [as 别名]
def _fetchUrl(self, url, headers):
if isinstance(url, str):
url = laUrl(url)
retries = 3
if self.cfg.proxy and not self.noproxyFilter.bypassProxy(url.host):
retries = 7
inFile = None
for i in range(retries):
try:
# set up a handler that tracks cookies to handle
# sites like Colabnet that want to set a session cookie
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
# add password handler if needed
if url.user:
url.passwd = url.passwd or ''
opener.add_handler(
HTTPBasicAuthHandler(url.user, url.passwd))
# add proxy and proxy password handler if needed
if self.cfg.proxy and \
not self.noproxyFilter.bypassProxy(url.host):
proxyPasswdMgr = urllib2.HTTPPasswordMgr()
for v in self.cfg.proxy.values():
pUrl = laUrl(v[1])
if pUrl.user:
pUrl.passwd = pUrl.passwd or ''
proxyPasswdMgr.add_password(
None, pUrl.asStr(noAuth=True, quoted=True),
url.user, url.passwd)
opener.add_handler(
urllib2.ProxyBasicAuthHandler(proxyPasswdMgr))
opener.add_handler(
urllib2.ProxyHandler(self.cfg.proxy))
if url.scheme == 'ftp':
urlStr = url.asStr(noAuth=False, quoted=True)
else:
urlStr = url.asStr(noAuth=True, quoted=True)
req = urllib2.Request(urlStr, headers=headers)
inFile = opener.open(req)
if not urlStr.startswith('ftp://'):
content_type = inFile.info().get('content-type')
if not url.explicit() and 'text/html' in content_type:
raise urllib2.URLError('"%s" not found' % urlStr)
log.info('Downloading %s...', urlStr)
break
except urllib2.HTTPError, msg:
if msg.code == 404:
return None
else:
log.error('error downloading %s: %s',
urlStr, str(msg))
return None
except urllib2.URLError:
return None