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

标签 python https proxy python-2.7 urllib2

从 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。

关于python - 从 python-2.5 升级到 2.7 时带有 HTTPS 回归的 urllib2.ProxyHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14987106/

相关文章:

python - 从数组中删除所有特定值

python - 在 python re.findall 中使用多个标志

python - 压缩此数据的更快方法是什么?

http - 与此站点的连接不完全安全

ssl - Owin self 主机 SSL 连接重置

php - UI 在使用 https 后中断

php - 有没有与 cURL 相当的 PHP 库,不需要安装?

angular - 如何配置 nginx 以在/和代理/api 上为另一个 api 服务器提供 Angular 应用程序?

python - 从 CMake 运行 pytest 测试,其中测试和源位于不同的文件夹中

apache - 使用 Apache2 作为 Tomcat7 的代理