本文整理汇总了Python中urllib2.ProxyBasicAuthHandler方法的典型用法代码示例。如果您正苦于以下问题:Python urllib2.ProxyBasicAuthHandler方法的具体用法?Python urllib2.ProxyBasicAuthHandler怎么用?Python urllib2.ProxyBasicAuthHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2
的用法示例。
在下文中一共展示了urllib2.ProxyBasicAuthHandler方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_us_proxy
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import ProxyBasicAuthHandler [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
示例2: test_proxy_basic_auth
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import ProxyBasicAuthHandler [as 别名]
def test_proxy_basic_auth(self):
opener = OpenerDirector()
ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128"))
opener.add_handler(ph)
password_manager = MockPasswordManager()
auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
realm = "ACME Networks"
http_handler = MockHTTPHandler(
407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
opener.add_handler(auth_handler)
opener.add_handler(http_handler)
self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
realm, http_handler, password_manager,
"http://acme.example.com:3128/protected",
"proxy.example.com:3128",
)
示例3: get_opener
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import ProxyBasicAuthHandler [as 别名]
def get_opener():
default_opener = urllib2.build_opener()
if not exists(YUM_CONF):
return default_opener
config = RawConfigParser()
config.read(YUM_CONF)
if not config.has_section('main'):
return default_opener
if not config.has_option('main', 'proxy'):
return default_opener
proxy = {}
url = config.get('main', 'proxy').strip()
if not url:
return default_opener
http_proxy_handler = urllib2.ProxyHandler({'http': url, 'https': url})
# urllib2 can open HTTPS ressources through a proxy since python 2.6.3
# should be OK on Centos OS (Python 2.6.6)
if config.has_option('main', 'proxy_username') and config.has_option(
'main', 'proxy_password'):
username = config.get('main', 'proxy_username').strip()
password = config.get('main', 'proxy_password').strip()
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, username, password)
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_manager)
return urllib2.build_opener(http_proxy_handler, proxy_auth_handler)
return urllib2.build_opener(http_proxy_handler)
示例4: _fetchUrl
# 需要导入模块: import urllib2 [as 别名]
# 或者: from urllib2 import ProxyBasicAuthHandler [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