如何处理 urllib3 中的代理

How to handle proxies in urllib3

我无法找到有关如何在 urllib3 中构建简单脚本的可靠示例,该脚本会打开 url(通过代理),然后读取并最终打印出来。代理需要 user/pass 进行身份验证,但我不清楚您是如何做到这一点的?任何帮助将不胜感激。

urllib3 在 urllib3 中有一个 ProxyManager component which you can use. You'll need to build headers for the Basic Auth component, you can either do that manually or use the make_headers 助手。

总的来说,它看起来像这样:

from urllib3 import ProxyManager, make_headers

default_headers = make_headers(proxy_basic_auth='myusername:mypassword')
http = ProxyManager("https://myproxy.com:8080/", proxy_headers=default_headers)

# Now you can use `http` as you would a normal PoolManager
r = http.request('GET', 'https://whosebug.com/')

我认为正确答案应该是

from urllib3 import ProxyManager, make_headers

default_headers = make_headers(proxy_basic_auth='myusername:mypassword')
http = ProxyManager("https://myproxy.com:8080/", headers=default_headers)

# Now you can use `http` as you would a normal PoolManager
r = http.request('GET', 'https://whosebug.com/')

(注:proxy_basic_auth,不是basic_auth)

我在我的环境中用 basic_auth 尝试过这个,但没有任何运气。 shazow you committed this comment to git which pointed me in the right direction