◐ Shell
clean mode source ↗

Issue 1160328: urllib2 post error when using httpproxy

==============
program
==============
# -*- coding: gbk -*-
import httplib, urllib, urllib2#, cookielib

proxy = urllib2.ProxyHandler
({'http':'http://pic:iLusalt@proxy.pconline.com.cn:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

f = urllib2.urlopen
("http://192.168.10.177:8080/price/login.do?
method=list")
print f.read()

postdata = urllib.urlencode
({'userId':'admin', 'password':'admin'})
request = urllib2.Request
('http://192.168.10.177:8080/price/login.do?
method=login')
response = urllib2.urlopen(request, postdata)
print response.read()

====================
out put
====================
E:\jt>c:\python24\python t.py
Traceback (most recent call last):
  File "t.py", line 13, in ?
    response = urllib2.urlopen(request, postdata)
  File "c:\python24\lib\urllib2.py", line 130, in urlopen
    return _opener.open(url, data)
  File "c:\python24\lib\urllib2.py", line 358, in open
    response = self._open(req, data)
  File "c:\python24\lib\urllib2.py", line 376, in _open
    '_open', req)
  File "c:\python24\lib\urllib2.py", line 337, in _call_chain
    result = func(*args)
  File "c:\python24\lib\urllib2.py", line 1021, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "c:\python24\lib\urllib2.py", line 996, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error (10053, 'Software 
caused connection abort')>
Today, proxy.pconline.com.cn resolves to a local (inaccessible) address for me (192.168.11.254). After changing the proxy address to localhost, and testing with Python 2.6.8, I can’t see any evidence of a bug in Python. Winsock error 10053 is WSAECONNABORTED, which apparently can be triggered after a lower-level protocol failure or timeout. So this could be the fault of the proxy, network, or a firewall. I am closing this, assuming nobody else can to reproduce this fifteen years later.

The closest I got is the following HTTP seen at the proxy:

>>> [conn, address] = listener.accept()
>>> pprint(conn.recv(3000).splitlines(keepends=True))
[b'GET http://192.168.10.177:8080/price/login.do?method=list HTTP/1.1\r\n',
 b'Accept-Encoding: identity\r\n',
 b'Host: 192.168.10.177:8080\r\n',
 b'Proxy-Authorization: Basic cGljOmlMdXNhbHQ=\r\n',
 b'Connection: close\r\n',
 b'User-Agent: Python-urllib/2.6\r\n',
 b'\r\n']
>>> conn.sendall(b'HTTP/1.1 200 Okay\r\nContent-Length: 0\r\n\r\n')
>>> conn.close()
>>> [conn, address] = listener.accept()
>>> pprint(conn.recv(3000).splitlines(keepends=True))
[b'POST http://192.168.10.177:8080/price/login.do?method=login HTTP/1.1\r\n',
 b'Accept-Encoding: identity\r\n',
 b'Content-Length: 27\r\n',
 b'Host: 192.168.10.177:8080\r\n',
 b'User-Agent: Python-urllib/2.6\r\n',
 b'Connection: close\r\n',
 b'Proxy-Authorization: Basic cGljOmlMdXNhbHQ=\r\n',
 b'Content-Type: application/x-www-form-urlencoded\r\n',
 b'\r\n',
 b'password=admin&userId=admin']

If I close the proxy’s listener before sending the first response, this causes ECONNREFUSED for the second request, with a similar back trace:

$ python2.6 t.py

Traceback (most recent call last):
  File "t.py", line 13, in <module>
    response = urllib2.urlopen(request, postdata)
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1181, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1156, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>