Navigation

This module defines classes which implement the client side of the HTTP and
HTTPS protocols. It is normally not used directly --- the module
urllib.request uses it to handle URLs that use HTTP and HTTPS.

An HTTPConnection instance represents one transaction with an HTTP
server. It should be instantiated passing it a host and optional port
number. If no port number is passed, the port is extracted from the host
string if it has the form host:port, else the default HTTP port (80) is
used. If the optional timeout parameter is given, blocking
operations (like connection attempts) will timeout after that many seconds
(if it is not given, the global default timeout setting is used).
The optional source_address parameter may be a tuple of a (host, port)
to use as the source address the HTTP connection is made from.
The optional blocksize parameter sets the buffer size in bytes for
sending a file-like message body.

For example, the following calls all create instances that connect to the server
at the same host and port:

Modifié dans la version 3.4.3: This class now performs all the necessary certificate and hostname checks
by default. To revert to the previous, unverified, behavior
ssl._create_unverified_context() can be passed to the context
parameter.

This will send a request to the server using the HTTP request
method method and the selector url.

If body is specified, the specified data is sent after the headers are
finished. It may be a str, a bytes-like object, an
open file object, or an iterable of bytes. If body
is a string, it is encoded as ISO-8859-1, the default for HTTP. If it
is a bytes-like object, the bytes are sent as is. If it is a file
object, the contents of the file is sent; this file object should
support at least the read() method. If the file object is an
instance of io.TextIOBase, the data returned by the read()
method will be encoded as ISO-8859-1, otherwise the data returned by
read() is sent as is. If body is an iterable, the elements of the
iterable are sent as is until the iterable is exhausted.

The headers argument should be a mapping of extra HTTP headers to send
with the request.

If headers contains neither Content-Length nor Transfer-Encoding,
but there is a request body, one of those
header fields will be added automatically. If
body is None, the Content-Length header is set to 0 for
methods that expect a body (PUT, POST, and PATCH). If
body is a string or a bytes-like object that is not also a
file, the Content-Length header is
set to its length. Any other type of body (files
and iterables in general) will be chunk-encoded, and the
Transfer-Encoding header will automatically be set instead of
Content-Length.

The encode_chunked argument is only relevant if Transfer-Encoding is
specified in headers. If encode_chunked is False, the
HTTPConnection object assumes that all encoding is handled by the
calling code. If it is True, the body will be chunk-encoded.

Note

Chunked transfer encoding has been added to the HTTP protocol
version 1.1. Unless the HTTP server is known to handle HTTP 1.1,
the caller must either specify the Content-Length, or must pass a
str or bytes-like object that is not also a file as the
body representation.

Nouveau dans la version 3.2: body can now be an iterable.

Modifié dans la version 3.6: If neither Content-Length nor Transfer-Encoding are set in
headers, file and iterable body objects are now chunk-encoded.
The encode_chunked argument was added.
No attempt is made to determine the Content-Length for file
objects.

Set the debugging level. The default debug level is 0, meaning no
debugging output is printed. Any value greater than 0 will cause all
currently defined debug output to be printed to stdout. The debuglevel
is passed to any new HTTPResponse objects that are created.

Set the host and the port for HTTP Connect Tunnelling. This allows running
the connection through a proxy server.

The host and port arguments specify the endpoint of the tunneled connection
(i.e. the address included in the CONNECT request, not the address of the
proxy server).

The headers argument should be a mapping of extra HTTP headers to send with
the CONNECT request.

For example, to tunnel through a HTTPS proxy server running locally on port
8080, we would pass the address of the proxy to the HTTPSConnection
constructor, and the address of the host that we eventually want to reach to
the set_tunnel() method:

This should be the first call after the connection to the server has been
made. It sends a line to the server consisting of the method string,
the url string, and the HTTP version (HTTP/1.1). To disable automatic
sending of Host: or Accept-Encoding: headers (for example to accept
additional content encodings), specify skip_host or skip_accept_encoding
with non-False values.

Send an RFC 822-style header to the server. It sends a line to the server
consisting of the header, a colon and a space, and the first argument. If more
arguments are given, continuation lines are sent, each consisting of a tab and
an argument.

Send a blank line to the server, signalling the end of the headers. The
optional message_body argument can be used to pass a message body
associated with the request.

If encode_chunked is True, the result of each iteration of
message_body will be chunk-encoded as specified in RFC 7230,
Section 3.3.1. How the data is encoded is dependent on the type of
message_body. If message_body implements the buffer interface the encoding will result in a single chunk.
If message_body is a collections.abc.Iterable, each iteration
of message_body will result in a chunk. If message_body is a
file object, each call to .read() will result in a chunk.
The method automatically signals the end of the chunk-encoded data
immediately after message_body.

Note

Due to the chunked encoding specification, empty chunks
yielded by an iterator body will be ignored by the chunk-encoder.
This is to avoid premature termination of the read of the request by
the target server due to malformed encoding.

Return the value of the header name, or default if there is no header
matching name. If there is more than one header with the name name,
return all of the values joined by ', '. If 'default' is any iterable other
than a single string, its elements are similarly returned joined by commas.

Client side HTTPPUT requests are very similar to POST requests. The
difference lies only the server side where HTTP server will allow resources to
be created via PUT request. It should be noted that custom HTTP methods
+are also handled in urllib.request.Request by sending the appropriate
+method attribute.Here is an example session that shows how to do PUT
request using http.client:

>>> # This creates an HTTP message>>> # with the content of BODY as the enclosed representation>>> # for the resource http://localhost:8080/file...>>> importhttp.client>>> BODY="***filecontents***">>> conn=http.client.HTTPConnection("localhost",8080)>>> conn.request("PUT","/file",BODY)>>> response=conn.getresponse()>>> print(response.status,response.reason)200, OK