python urllib2.ProxyBasicAuthHandler Examples

urllib2.ProxyBasicAuthHandler用来挂载访问网页时的代理

Example 1

1
2
3
4
5
6
7
def build_opener(self):
cookiejar = cookielib.FileCookieJar(cookiefile)
cookie_handler = urllib2.HTTPCookieProcessor(cookiejar)
redirect_handler = urllib2.HTTPRedirectHandler()
#proxy_handler = urllib2.ProxyHandler()
#proxy_auth_handker = urllib2.ProxyBasicAuthHandler()
return urllib2.build_opener(cookie_handler, redirect_handler)

Example 2

1
2
3
4
5
6
7
8
9
10
def install_proxy():
USE_PROXY=True
import urllib2
global urlretrieve
proxy_handler = urllib2.ProxyHandler({'http': 'http://proxy.cma-cgm.com:8080/'})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
#proxy_auth_handler.add_password('realm', 'host', 'username', 'password')
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
urlretrieve=opener.open

Example 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def get_urlopen():
proxy_type = get_prefs('proxy_type')
if proxy_type == 'http':
scheme = 'http'
host = str(get_prefs('proxy_host'))
port = str(get_prefs('proxy_port'))
url = scheme + '://' + host + ':' + port
if get_prefs('proxy_auth'):
proxy_support = urllib2.ProxyHandler({ 'http': url, 'https': url })
username = str(get_prefs('proxy_auth_name'))
password = str(get_prefs('proxy_auth_password'))
auth_handler = urllib2.ProxyBasicAuthHandler()
auth_handler.add_password(None, url, username, password)
return urllib2.build_opener(proxy_support, auth_handler).open
else:
proxy_support = urllib2.ProxyHandler({ 'http': url, 'https': url })
return urllib2.build_opener(proxy_support).open
elif proxy_type == 'system':
if 'http_proxy' in os.environ and os.environ["http_proxy"]:
url = os.environ["http_proxy"]
elif 'HTTP_PROXY' in os.environ and os.environ["HTTP_PROXY"]:
url = os.environ["HTTP_PROXY"]
else:
url = None

if not url:
return urllib2.urlopen
else:
proxy_support = urllib2.ProxyHandler({ 'http': url, 'https': url })
return urllib2.build_opener(proxy_support).open
else:
return urllib2.urlopen

Example 4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def get_urlopen():
proxy_type = get_prefs('proxy_type');
if proxy_type == 'http':
scheme = 'http'
host = str(get_prefs('proxy_host'))
port = str(get_prefs('proxy_port'))
url = scheme + '://' + host + ':' + port
if get_prefs('proxy_auth'):
proxy_support = urllib2.ProxyHandler({ 'http': url, 'https': url })
username = str(get_prefs('proxy_auth_name'))
password = str(get_prefs('proxy_auth_password'))
auth_handler = urllib2.ProxyBasicAuthHandler()
auth_handler.add_password(None, url, username, password)
return urllib2.build_opener(proxy_support, auth_handler).open
else:
proxy_support = urllib2.ProxyHandler({ 'http': url, 'https': url })
return urllib2.build_opener(proxy_support).open
elif proxy_type == 'system':
if 'http_proxy' in os.environ and os.environ["http_proxy"]:
url = os.environ["http_proxy"]
elif 'HTTP_PROXY' in os.environ and os.environ["HTTP_PROXY"]:
url = os.environ["HTTP_PROXY"]
else:
url = None

if not url:
return urllib2.urlopen
else:
proxy_support = urllib2.ProxyHandler({ 'http': url, 'https': url })
return urllib2.build_opener(proxy_support).open
else:
return urllib2.urlopen

Example 5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def _opener(self):

build = [urllib2.HTTPHandler()]

if self.request.redirect:
build.append(urllib2.HTTPRedirectHandler())

if self.request.proxy_host and self.request.proxy_port:
build.append(urllib2.ProxyHandler(
{self.request.proxy_protocol: self.request.proxy_host + ':' + str(self.request.proxy_port)}))

if self.request.proxy_username:
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'uri', self.request.proxy_username,
self.request.proxy_password)
build.append(proxy_auth_handler)

if self.request.cookies:
self.request.cookies = os.path.join(self._dirname, self.request.cookies)
self.cookies = cookielib.MozillaCookieJar()
if os.path.isfile(self.request.cookies):
self.cookies.load(self.request.cookies)
build.append(urllib2.HTTPCookieProcessor(self.cookies))

urllib2.install_opener(urllib2.build_opener(*build))

Example 6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def setup_opener(self, url, timeout):
"""
Sets up a urllib OpenerDirector to be used for requests. There is a
fair amount of custom urllib code in Package Control, and part of it
is to handle proxies and keep-alives. Creating an opener the way
below is because the handlers have been customized to send the
"Connection: Keep-Alive" header and hold onto connections so they
can be re-used.

:param url:
The URL to download

:param timeout:
The int number of seconds to set the timeout to
"""

if not self.opener:
http_proxy = self.settings.get('http_proxy')
https_proxy = self.settings.get('https_proxy')
if http_proxy or https_proxy:
proxies = {}
if http_proxy:
proxies['http'] = http_proxy
if https_proxy:
proxies['https'] = https_proxy
proxy_handler = ProxyHandler(proxies)
else:
proxy_handler = ProxyHandler()

password_manager = HTTPPasswordMgrWithDefaultRealm()
proxy_username = self.settings.get('proxy_username')
proxy_password = self.settings.get('proxy_password')
if proxy_username and proxy_password:
if http_proxy:
password_manager.add_password(None, http_proxy, proxy_username,
proxy_password)
if https_proxy:
password_manager.add_password(None, https_proxy, proxy_username,
proxy_password)

handlers = [proxy_handler]

basic_auth_handler = ProxyBasicAuthHandler(password_manager)
digest_auth_handler = ProxyDigestAuthHandler(password_manager)
handlers.extend([digest_auth_handler, basic_auth_handler])

debug = self.settings.get('debug')

if debug:
console_write(u"Urllib Debug Proxy", True)
console_write(u" http_proxy: %s" % http_proxy)
console_write(u" https_proxy: %s" % https_proxy)
console_write(u" proxy_username: %s" % proxy_username)
console_write(u" proxy_password: %s" % proxy_password)

secure_url_match = re.match('^https://([^/]+)', url)
if secure_url_match != None:
secure_domain = secure_url_match.group(1)
bundle_path = self.check_certs(secure_domain, timeout)
bundle_path = bundle_path.encode(sys.getfilesystemencoding())
handlers.append(ValidatingHTTPSHandler(ca_certs=bundle_path,
debug=debug, passwd=password_manager,
user_agent=self.settings.get('user_agent')))
else:
handlers.append(DebuggableHTTPHandler(debug=debug,
passwd=password_manager))
self.opener = build_opener(*handlers)