httplib2
httplib2 copied to clipboard
rewind file body for retry send request
Hello guys!
Recently I recived some strange behavior for sending http requests using httplib2
My python script uses httplib2 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:
- HTTPConnectionWithTimeout trys to send PUT request to closed socket.
- HTTPConnectionWithTimeout reconnects to server and sends request again but no file sended. This behavior was checked using wireshark and debug output of httplib.
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
Note that it seem to make more sense to: a) if the body supports .tell(), on retry it should be rewinded to the initial position that was passed on first invocation b) if the body doesn't support tell/seek, then exception should be raised explicitly if some data was attempted to be sent, but failed and so is not recoverable.
well I could rewrite patch in such case
if hasattr(body, 'read') and body.tell() > 0:
body.seek(0) # rewind for retry send file
since httplib file data sending is based on assumption that object have a read method we could check the same and just do what we do and if it fails it will raise an exception