本文整理匯總了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