Skip to main content
 首页 » 编程设计

python之从 python-2.5 升级到 2.7 时带有 HTTPS 回归的 urllib2.ProxyHandler

2025年05月04日15lvdongjie

从 python-2.5 升级到 python-2.7 时,以下代码被破坏:

>>> import urllib 
>>> import urllib2 
>>> from django.conf import settings 
 
>>> settings.AUTH_URL 
'https://auth.example.net/auth/' 
>>> settings.HTTP_PROXY 
{'http': 'http://proxy.example.net', 'https': 'https://proxy.example.net'} 
>>> cred = urllib.urlencode({'username': 'someuser', 'password': 'somepasswd'}) 
 
>>> urllib2.urlopen(settings.AUTH_URL, data=cred).read() 
'<?xml version="1.0" encoding="UTF-8"?><auth><username value="someuser">true</username><password>true</password></auth>' 
 
>>> urllib2.build_opener().open(settings.AUTH_URL, data=cred).read() 
'<?xml version="1.0" encoding="UTF-8"?><auth><username value="someuser">true</username><password>true</password></auth>' 
 
>>> urllib2.build_opener(urllib2.ProxyHandler(settings.HTTP_PROXY)).open(settings.AUTH_URL, data=cred).read() 
Traceback (most recent call last): 
  File "<console>", line 1, in <module> 
  File "/usr/lib/python2.7/urllib2.py", line 394, in open 
    response = self._open(req, data) 
  File "/usr/lib/python2.7/urllib2.py", line 412, in _open 
    '_open', req) 
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain 
    result = func(*args) 
  File "/usr/lib/python2.7/urllib2.py", line 1207, in https_open 
    return self.do_open(httplib.HTTPSConnection, req) 
  File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open 
    raise URLError(err) 
URLError: <urlopen error [Errno 111] Connection refused> 
 
>>> opener = urllib2.build_opener(urllib2.ProxyHandler(settings.HTTP_PROXY)) 
>>> opener 
<urllib2.OpenerDirector instance at 0x1bc39d40> 
>>> opener.__dict__ 
{'addheaders': [('User-agent', 'Python-urllib/2.7')], 
 'handle_error': {'http': {301: [<urllib2.HTTPRedirectHandler instance at 0x1bc3f7a0>], 
                       302: [<urllib2.HTTPRedirectHandler instance at 0x1bc3f7a0>], 
                       303: [<urllib2.HTTPRedirectHandler instance at 0x1bc3f7a0>], 
                       307: [<urllib2.HTTPRedirectHandler instance at 0x1bc3f7a0>], 
                       'default': [<urllib2.HTTPDefaultErrorHandler instance at 0x1bc3f7e8>]}}, 
 'handle_open': {'file': [<urllib2.FileHandler instance at 0x1bc43098>], 
             'ftp': [<urllib2.FTPHandler instance at 0x1bc3ffc8>], 
             'http': [<urllib2.ProxyHandler instance at 0x1bfa0638>, 
                      <urllib2.HTTPHandler instance at 0x1bc3f758>], 
             'https': [<urllib2.ProxyHandler instance at 0x1bfa0638>, 
                       <urllib2.HTTPSHandler instance at 0x1bc43200>], 
             'unknown': [<urllib2.UnknownHandler instance at 0x1bc3f830>]}, 
 'handlers': [<urllib2.ProxyHandler instance at 0x1bfa0638>, 
          <urllib2.UnknownHandler instance at 0x1bc3f830>, 
          <urllib2.HTTPHandler instance at 0x1bc3f758>, 
          <urllib2.HTTPDefaultErrorHandler instance at 0x1bc3f7e8>, 
          <urllib2.HTTPRedirectHandler instance at 0x1bc3f7a0>, 
          <urllib2.FTPHandler instance at 0x1bc3ffc8>, 
          <urllib2.FileHandler instance at 0x1bc43098>, 
          <urllib2.HTTPSHandler instance at 0x1bc43200>, 
          <urllib2.HTTPErrorProcessor instance at 0x1bc43128>], 
 'process_request': {'http': [<urllib2.HTTPHandler instance at 0x1bc3f758>], 
                 'https': [<urllib2.HTTPSHandler instance at 0x1bc43200>]}, 
 'process_response': {'http': [<urllib2.HTTPErrorProcessor instance at 0x1bc43128>], 
                  'https': [<urllib2.HTTPErrorProcessor instance at 0x1bc43128>]}} 
>>> sock = opener.open(settings.AUTH_URL, data=cred) 
Traceback (most recent call last): 
  File "<console>", line 1, in <module> 
  File "/usr/lib/python2.7/urllib2.py", line 394, in open 
    response = self._open(req, data) 
  File "/usr/lib/python2.7/urllib2.py", line 412, in _open 
    '_open', req) 
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain 
    result = func(*args) 
  File "/usr/lib/python2.7/urllib2.py", line 1207, in https_open 
    return self.do_open(httplib.HTTPSConnection, req) 
  File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open 
    raise URLError(err) 
URLError: <urlopen error [Errno 111] Connection refused> 

最后一个表达式在 python-2.5 上仍然有效,但在具有相同代理和 url 设置的同一台物理机器上的 2.7 上不起作用。知道为什么吗?

请您参考如下方法:

通过从 settings.HTTP_PROXY 中删除“https”键解决,但不确定原因。 URL 仍然是 HTTPS。