[New-bugs-announce] [issue25838] Lib/httplib.py: Resend http request on server close connection

Mikhail Gulyaev report at bugs.python.org
Fri Dec 11 02:36:46 EST 2015


New submission from Mikhail Gulyaev:

Hello guys!

Recently I recived some strange behavior for sending http requests using httplib

My python script uses httplib and interacts with a web server which have a keep-alive timeout 5 seconds. Script makes PUT requests and sends files to server. At first time it works ok. Then after 5 seconds server closes connection. And then I doing second PUT request, that fails in two stages:
 - HTTPConnection trys to send PUT request to closed socket.
 - HTTPConnection reconnects to server and sends request again but no file sended.
This behavior was checked using wireshark and debug output of httplib.

The best solution IMHO would be to checking socket state on each request, and reconnects if needed - but it seems this issue has no unique solution.

So I offer a patch which simply rewinds file before sending if needed. 
hasattr(data,'tell') could be used in checking condition
-----------------------------------
diff -r 002d8b981128 Lib/httplib.py
--- a/Lib/httplib.py	Wed Dec 09 19:44:30 2015 +0200
+++ b/Lib/httplib.py	Fri Dec 11 12:59:47 2015 +0600
@@ -865,6 +865,7 @@
         blocksize = 8192
         if hasattr(data,'read') and not isinstance(data, array):
             if self.debuglevel > 0: print "sendIng a read()able"
+            if data.tell() > 0: data.seek(0) # rewind for retry send file
             datablock = data.read(blocksize)
             while datablock:
                 self.sock.sendall(datablock)
-----------------------------------

----------
messages: 256204
nosy: gmixo
priority: normal
severity: normal
status: open
title: Lib/httplib.py: Resend http request on server close connection
type: enhancement
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25838>
_______________________________________


More information about the New-bugs-announce mailing list