[issue25838] Lib/httplib.py: Resend http request on server close connection
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.pyWed Dec 09 19:44:30 2015 +0200 +++ b/Lib/httplib.pyFri 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 <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Changes by Mikhail Gulyaev : -- keywords: +patch Added file: http://bugs.python.org/file41284/httplib.patch ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Mikhail Gulyaev added the comment: > Why can’t you do the file rewinding yourself, when you retry the request? Actually I'm not retry to send a request. This behavior I met for httplib library while I'm send a single request, httplib send it to exist connection and then recreates connection on failure and send it again. Maybe this doing tcp socket For GET POST and DELETE I meet the same behavior > if someone wrote code that expects data to be sent from a non-zero file > position. Well in this case I'd prefer to trunkate file before send. -- Added file: http://bugs.python.org/file41285/httplib.pcapng ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Changes by Mikhail Gulyaev : Added file: http://bugs.python.org/file41286/Выделение_058.png ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Changes by Mikhail Gulyaev : Added file: http://bugs.python.org/file41287/Выделение_059.png ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Changes by Mikhail Gulyaev : Added file: http://bugs.python.org/file41288/Выделение_061.png ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Mikhail Gulyaev added the comment: You right I found who resend requests! My request is goes through httplb2(which use httplib and resends request on failure), but the issue is that if request body contains file and then that file is read out and on retry there is nothing to read since we already read it in httplib. So what solution could you suggest for me? Is it some patch for httplib2 or totally my own troubles This issue is on border of interacting httplib and httplib2 - httplib sends request and reads out a file - httplib2 resends a request but file is already readed out Will it be honest if we rereads file in httplib2? and could we able assume that readable object(hasattr(data,'read') == True) has also tell and seek methods -- ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25838] Lib/httplib.py: Resend http request on server close connection
Mikhail Gulyaev added the comment: Thanks for attention -- ___ Python tracker <http://bugs.python.org/issue25838> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com