Python Glossary Русский

all Python 3http.clientexception

http.client.ImproperConnectionState

exceptions

The base class for calling methods on a connection in the wrong order: CannotSendRequest, CannotSendHeader and ResponseNotReady. A subclass of HTTPException.

Syntax

except http.client.ImproperConnectionState as e: ...

HTTPConnection is a state machine: Idle → request started → request sent → response received and read → Idle again. Any step out of order raises a subclass of this class, and the exception text names the current state ('Idle', 'Request-started', 'Request-sent'). The usual cause is an unread response or one connection shared by several threads: HTTPConnection is not thread-safe.

Examples

import http.client
conn = http.client.HTTPConnection('example.com')
try:
    conn.getresponse()  # запроса ещё не было
except http.client.ImproperConnectionState as e:
    print(type(e).__name__, e)  # → ResponseNotReady Idle

See also

Open in the full glossarydocs.python.orgReport an inaccuracy