Python Glossary Русский

all Python 3http.clientexception

http.client.CannotSendHeader

exceptions

putheader() was called at the wrong time: before putrequest() or after endheaders(). A subclass of ImproperConnectionState.

Syntax

except http.client.CannotSendHeader: ...

It only happens when a request is built by hand with the low-level methods putrequest() → putheader() → endheaders() → send(). Headers can be added only between the first two steps; after endheaders() they have already gone to the server. With a plain request(method, url, headers=...) this error cannot happen. The exception carries an empty message.

Examples

import http.client
conn = http.client.HTTPConnection('example.com')
try:
    conn.putheader('Accept', 'text/plain')  # putrequest() ещё не было
except http.client.CannotSendHeader as e:
    print(repr(e))  # → CannotSendHeader()
conn.putrequest('GET', '/')
conn.putheader('Accept', 'text/plain')  # а теперь можно
print('ok')  # → ok

See also

Open in the full glossarydocs.python.orgReport an inaccuracy