Python 2.7(urllib2)。如何使用SSL HTTPS代理?

时间:2019-05-12 15:29:30

标签: python ssl https proxy urllib2

如何将安全Web代理(如记录的this(443,HTTPS)与urllib2一起使用? 例如,我正在尝试

opener = urllib2.build_opener(urllib2.HTTPHandler, urllib2.HTTPSHandler,urllib2.ProxyHandler({"https": 'https://some_proxy.com:443'}))
urllib2.install_opener(opener)

但是超时。 该代理可在其他应用程序中使用,例如通过PAC文件在浏览器上使用。

P.S。 Proxy with urllib2重复的问题,因为没有有关安全Web代理的信息。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用urllib3

from urllib3 import ProxyManager

http = ProxyManager("https://some.proxy.com:8080/")
response = http.request('GET', 'https://stackoverflow.com/')

如果您还希望将标头发送到代理,则

from urllib3 import ProxyManager, make_headers

default_headers = make_headers(proxy_basic_auth='username:password')
http = ProxyManager("https://some.proxy.com:8080/", headers=default_headers)
response = http.request('GET', 'https://stackoverflow.com/')