从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。知道为什么吗?
答案 0 :(得分:0)
通过从settings.HTTP_PROXY中删除“https”键解决但不确定原因。该网址仍为HTTPS。