Python Glossary Русский

Python 3.5+http.clientexception

http.client.RemoteDisconnected

exceptions

The server closed the connection without sending a single byte of response. It inherits both ConnectionResetError and BadStatusLine. Added in Python 3.5.

Syntax

except http.client.RemoteDisconnected: reconnect and retry

The classic case is keep-alive: the connection sat idle, the server quietly closed it, and the next request on it meets silence. The right reaction is to reopen the connection and retry if the method is idempotent. Thanks to dual inheritance both except OSError and except http.client.HTTPException catch it. If the drop happens mid-body, you get IncompleteRead instead.

Examples

import http.client
import socket
server, client = socket.socketpair()
server.sendall(b'')
server.close()
resp = http.client.HTTPResponse(client)
try:
    resp.begin()
except ConnectionResetError as e:
    print(type(e).__name__, '-', e)  # → RemoteDisconnected - Remote end closed connection without response
    print(isinstance(e, http.client.BadStatusLine))  # → True
client.close()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy