This class encapsulates a HTTP connection.
In contrast to the standard library’s http.client module, this class
These features come for a price:
- It is recommended to use this class only for idempotent HTTP methods. This is because if a connection is terminated earlier than expected (e.g. because of the server sending an unsupported reply) but responses for multiple requests are pending, the client cannot determine which requests have been processed.
- Only HTTP 1.1 connections are supported
- Responses and requests must specify a Content-Length header when not using chunked encoding.
If a server response doesn’t fulfill the last two requirements, an UnsupportedResponse exception is raised. Typically, this means that synchronization with the server will be lost, so the connection needs to be reset by calling the close method.
All request and response headers are represented as strings, but must be encodable in latin1. Request and response body must be bytes.
The HTTPConnection class allows you to send an unlimited number of requests to the server before reading any of the responses. However, at some point the transmit and receive buffers on both the ends of the connection will fill up, and no more requests can be send before at least some of the responses are read, and attempts to send more data to the server will block. If the thread that attempts to send data is is also responsible for reading the responses, this will result in a deadlock.
There are several ways to avoid this: