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.

Author gmixo
Recipients gmixo
Date 2015-12-11.07:36:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1449819406.11.0.669175603507.issue25838@psf.upfronthosting.co.za>
In-reply-to
Content
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)
-----------------------------------
History
Date User Action Args
2015-12-11 07:36:46gmixosetrecipients: + gmixo
2015-12-11 07:36:46gmixosetmessageid: <1449819406.11.0.669175603507.issue25838@psf.upfronthosting.co.za>
2015-12-11 07:36:46gmixolinkissue25838 messages
2015-12-11 07:36:44gmixocreate