This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: httplib throws a TypeError when the target host disconnects
Type: Stage:
Components: Library (Lib) Versions:
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: jhylton Nosy List: irmen, jhylton, jlgijsbers, rbgrn
Priority: normal Keywords:

Created on 2002-08-06 00:42 by rbgrn, last changed 2022-04-10 16:05 by admin. This issue is now closed.

File name Uploaded Description Edit
python_httpbug_testcase.tgz rbgrn, 2002-08-07 06:16 Test code which reproduces the error
httplib.diff rbgrn, 2002-08-08 04:14 a patch which fixes this so the proper exception is raised
Messages (12)
msg11832 - (view) Author: Rob Green (rbgrn) Date: 2002-08-06 00:42
This is the output that occurs about 1 in 500 hits to any 
particular URL. I've only seen it so far hitting servers 
running Apache 1.3.20, but I don't have enough data to 
limit it to that.

Python 2.1.2 (#1, Mar 16 2002, 18:24:08)
[GCC 2.95.3 [FreeBSD] 20010315 (release)] on freebsd4

h = httplib.HTTPConnection(host)
response = h.getresponse()
data =
File "/usr/local/lib/python2.1/", line 246, in read
value = value + self._safe_read(chunk_left)
File "/usr/local/lib/python2.1/", line 314, in 
chunk =
TypeError: an integer is required

msg11833 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-08-06 12:42
Logged In: YES 

Can you provide any more information about what URLs cause
problems?  You could call set_debuglevel(1) to enable output
of all  HTTP trafffic.  Or just a list of some of the URLs
that failed.
msg11834 - (view) Author: Rob Green (rbgrn) Date: 2002-08-07 05:14
Logged In: YES 

I'd give you the URLs but I don't think it matters that much, 
I've seen this problem now hitting 4 different machines, all 
running linux/apache. It's not something very easy for me to 
reproduce because I only see the exception thrown once or 
maybe twice during the day where there are 20,000 hits from 
my daemon that day. I suppose that the way to reproduce it 
would be to have a list of urls, and to just have an app sit 
there and cycle through them doing a GET every 5 seconds 
or so, and eventually it should show up. I suppose to be more 
accurate the app would have to be threaded as well, maybe 
having a thread for each url that just opens an 
HTTPConnection every 5 seconds.
msg11835 - (view) Author: Rob Green (rbgrn) Date: 2002-08-07 06:16
Logged In: YES 

Ok I put together some test code that reproduced the bug in 
under an hour on my machine. Here it is...
msg11836 - (view) Author: Rob Green (rbgrn) Date: 2002-08-08 03:51
Logged In: YES 

Ok I figured out what causes this. It's not a threading issue or 
anything like that, basically what happens is this:

When httplib connects to a server and the hostname is good 
and the port is open and connects, but the server 
immediately disconnects without printing any text or 
anything, this exception is then thrown.

I was able to reproduce it by running netcat -l -p <port> 
locally and starting an HTTPConnection to that port, then 
punting netcat which causes python to throw the exception.
msg11837 - (view) Author: Rob Green (rbgrn) Date: 2002-08-08 04:14
Logged In: YES 

Line 245 should be "if chunk_left is not _UNKNOWN:"
And the next block up to line 259 should be indented. This 
causes an IncompleteRead exception to be thrown, which is 
IMO the correct one in this case.

I attached a diff that patches the Python 2.1.2 httplib.
msg11838 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-09-03 22:20
Logged In: YES 

A bunch of questions.  You seem to have narrowed this
problem down a lot, but I'm not sure I understand the diagnosis.

First off, have you tried the latest version of the code
from CVS?  It has changed in several ways, so it would be
helpful if you could test with that version.  I've been
running a test driver for about an hour now without seeing
any errors caused by httplib.

The real problem I have is understanding what path through
the code leaves chunked set to a true value and chunk_left
set to something invalid.
The begin() method of an HTTPResponse always sets chunked to
1 or 0.  If  it sets chunked to 1, it sets chunk_left to None.
msg11839 - (view) Author: Rob Green (rbgrn) Date: 2002-09-26 04:40
Logged In: YES 

I have not tried the latest version, however I defined the test 
case which reproduces the problem. It takes under a minute 
to try, so please try it on your version as I do not currently 
have time to checkout and build it myself. Thank-you.

Here's the test case again

`nc -l -p 8888` on some terminal

then in python have your test prog connect to the IP of that 
terminal port 8888 and GET / HTTP/1.0 or whatever

then on the terminal hit Ctrl-C instead of handing back a 

the test prog should throw the exception if affected.

msg11840 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2003-01-31 14:19
Logged In: YES 

Just getting back to this bug: What is the nc program?  I
don't have it on my RedHat 7.3 installation.
msg11841 - (view) Author: Rob Green (rbgrn) Date: 2003-01-31 15:25
Logged In: YES 


the rpm is called netcat but the executable is nc

you should be able to get an rpm for it off of

It's a very common program for diagnosing plain-text protocol 
network code.
msg11842 - (view) Author: Irmen de Jong (irmen) (Python triager) Date: 2004-11-07 22:39
Logged In: YES 

Can reproduce problem with Python 2.1.x, but
cannot reproduce with Python versions >= 2.2.x
For instance, python 2.4 gives a socket.error: (104,
'Connection reset by peer'). Which seems correct.
So I think this bug can be closed
msg11843 - (view) Author: Johannes Gijsbers (jlgijsbers) * (Python triager) Date: 2004-11-08 10:47
Logged In: YES 

Hmm, I'm getting a BadStatusLine exception on 2.3 and 2.4,
but that seems to be the right exception as well, so I'm
closing this.
Date User Action Args
2022-04-10 16:05:33adminsetgithub: 36987
2002-08-06 00:42:30rbgrncreate