Connections#

class urllib3.connection.HTTPConnection(host, port=None, *, timeout=_TYPE_DEFAULT.token, source_address=None, blocksize=8192, socket_options=[(6, 1, 1)], proxy=None, proxy_config=None)#

Bases: HTTPConnection

Based on http.client.HTTPConnection but provides an extra constructor backwards-compatibility layer between older and newer Pythons.

Additional keyword parameters are used to configure attributes of the connection. Accepted parameters include:

  • source_address: Set the source address for the current connection.

  • socket_options: Set specific options on the underlying socket. If not specified, then defaults are loaded from HTTPConnection.default_socket_options which includes disabling Nagle’s algorithm (sets TCP_NODELAY to 1) unless the connection is behind a proxy.

    For example, if you wish to enable TCP Keep Alive in addition to the defaults, you might pass:

    HTTPConnection.default_socket_options + [
        (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
    ]
    

    Or you may want to disable the defaults by passing an empty list (e.g., []).

Parameters:
close()#

Close the connection to the HTTP server.

Return type:

None

connect()#

Connect to the host and port specified in __init__.

Return type:

None

default_socket_options: ClassVar[Sequence[Tuple[int, int, int | bytes]]] = [(6, 1, 1)]#

Disable Nagle’s algorithm by default. [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]

getresponse()#

Get the response from the server.

If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable.

If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates that the connection should be closed, then it will be closed before the response is returned. When the connection is closed, the underlying socket is closed.

Return type:

HTTPResponse

property host: str#

Getter method to remove any trailing dots that indicate the hostname is an FQDN.

In general, SSL certificates don’t include the trailing dot indicating a fully-qualified domain name, and thus, they don’t validate properly when checked against a domain name that includes the dot. In addition, some servers may not expect to receive the trailing dot when provided.

However, the hostname with trailing dot is critical to DNS resolution; doing a lookup with the trailing dot will properly only resolve the appropriate FQDN, whereas a lookup without a trailing dot will search the system’s search domain list. Thus, it’s important to keep the original host around for use only in those cases where it’s appropriate (i.e., when doing DNS lookup to establish the actual TCP connection across which we’re going to send HTTP requests).

is_verified: bool = False#

Whether this connection verifies the host’s certificate.

request(method, url, body=None, headers=None, *, chunked=False, preload_content=True, decode_content=True, enforce_content_length=True)#

Send a complete request to the server.

Parameters:
Return type:

None

request_chunked(method, url, body=None, headers=None)#

Alternative to the common request method, which sends the body with chunked encoding and not as one block

Parameters:
Return type:

None

set_tunnel(host, port=None, headers=None, scheme='http')#

Set up host and port for HTTP CONNECT tunnelling.

In a connection that uses HTTP CONNECT tunneling, the host passed to the constructor is used as a proxy server that relays all communication to the endpoint passed to set_tunnel. This done by sending an HTTP CONNECT request to the proxy server when the connection is established.

This method must be called before the HTTP connection has been established.

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

Parameters:
Return type:

None

class urllib3.connection.HTTPSConnection(host, port=None, *, timeout=_TYPE_DEFAULT.token, source_address=None, blocksize=8192, socket_options=[(6, 1, 1)], proxy=None, proxy_config=None, cert_reqs=None, assert_hostname=None, assert_fingerprint=None, server_hostname=None, ssl_context=None, ca_certs=None, ca_cert_dir=None, ca_cert_data=None, ssl_minimum_version=None, ssl_maximum_version=None, ssl_version=None, cert_file=None, key_file=None, key_password=None)#

Bases: HTTPConnection

Many of the parameters to this constructor are passed to the underlying SSL socket by means of urllib3.util.ssl_wrap_socket().

Parameters:
  • host (str) –

  • port (int | None) –

  • timeout (_TYPE_TIMEOUT) –

  • source_address (tuple[str, int] | None) –

  • blocksize (int) –

  • socket_options (Sequence[Tuple[int, int, int | bytes]] | None) –

  • proxy (Url | None) –

  • proxy_config (ProxyConfig | None) –

  • cert_reqs (int | str | None) –

  • assert_hostname (None | str | Literal[False]) –

  • assert_fingerprint (str | None) –

  • server_hostname (str | None) –

  • ssl_context (ssl.SSLContext | None) –

  • ca_certs (str | None) –

  • ca_cert_dir (str | None) –

  • ca_cert_data (None | str | bytes) –

  • ssl_minimum_version (int | None) –

  • ssl_maximum_version (int | None) –

  • ssl_version (int | str | None) –

  • cert_file (str | None) –

  • key_file (str | None) –

  • key_password (str | None) –

connect()#

Connect to the host and port specified in __init__.

Return type:

None

set_cert(key_file=None, cert_file=None, cert_reqs=None, key_password=None, ca_certs=None, assert_hostname=None, assert_fingerprint=None, ca_cert_dir=None, ca_cert_data=None)#

This method should only be called once, before the connection is used.

Parameters:
  • key_file (str | None) –

  • cert_file (str | None) –

  • cert_reqs (int | str | None) –

  • key_password (str | None) –

  • ca_certs (str | None) –

  • assert_hostname (None | str | Literal[False]) –

  • assert_fingerprint (str | None) –

  • ca_cert_dir (str | None) –

  • ca_cert_data (None | str | bytes) –

Return type:

None

class urllib3.connection.ProxyConfig(ssl_context, use_forwarding_for_https, assert_hostname, assert_fingerprint)#

Bases: NamedTuple

Parameters:
  • ssl_context (ssl.SSLContext | None) –

  • use_forwarding_for_https (bool) –

  • assert_hostname (None | str | Literal[False]) –

  • assert_fingerprint (str | None) –

assert_fingerprint: str | None#

Alias for field number 3

assert_hostname: None | str | Literal[False]#

Alias for field number 2

ssl_context: ssl.SSLContext | None#

Alias for field number 0

use_forwarding_for_https: bool#

Alias for field number 1