更新时间:2022-04-23 04:04:56
问题是当您将 context
参数传递给 urllib2.urlopen()
然后 urllib2 创建 opener 本身 而不是 使用 全局的,这是调用 时设置的urllib2.install_opener()
.因此,您打算使用的 ProxyHandler
实例没有被使用.
解决办法不是安装opener,而是直接使用opener.在构建开启器时,您必须同时传递 ProxyHandler
类的实例(为 http 和 https 协议设置代理)和 HTTPSHandler
类的实例(设置https上下文).
The problem is when you pass context
argument to urllib2.urlopen()
then urllib2 creates opener itself instead of using the global one, which is the one that gets set when you call urllib2.install_opener()
. As a result your instance of ProxyHandler
which you meant to be used is not being used.
The solution is not to install opener but to use the opener directly. When building your opener, you have to pass both an instance of your ProxyHandler
class (to set proxies for http and https protocols) and an instance of HTTPSHandler
class (to set https context).
我为此问题创建了https://bugs.python.org/issue29379.