Martin Panter added the comment:
Here is a patch, including tests and documentation. It ended up a bit more
complicated than I anticipated, so I’m interested in hearing other ideas or
options.
* Added http.client.ConnectionClosed exception
* HTTPConnection.close() is implicitly called for a persistent connection
closure
* BadStatusLine or ConnectionError (rather than new exception) is still raised
on first getresponse()
* request() raising a ConnectionError does not necessarily mean the server did
not send a response, so ConnectionClosed is only raised by getresponse()
* ConnectionClosed wraps ECONNRESET from the first recv() of the status line,
but not after part of the status line has already been received
With this I hope code for making idempotent requests on a persistent connection
would look a bit like this:
def idempotent_request(connection)
try:
attempt_request(connection, ...)
response = connection.get_response()
if response.status == HTTPStatus.REQUEST_TIMEOUT:
raise ConnectionClosed(response.reason)
except ConnectionClosed:
attempt_request(connection, ...)
response = connection.get_response()
return response
def attempt_request(connection):
try:
connection.request(...)
except ConnectionError:
pass # Ignore and read server response
----------
keywords: +patch
Added file: http://bugs.python.org/file37834/ConnectionClosed.patch
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue3566>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com