API documentation#

Using Sphinx’s sphinx.ext.autodoc plugin, it is possible to auto-generate documentation of a Python module.

Tip

Avoid having in-function-signature type annotations with autodoc, by setting the following options:

# -- Options for autodoc ----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#configuration

# Automatically extract typehints when specified and place them in
# descriptions of the relevant function/method.
autodoc_typehints = "description"

# Don't show class signature with the class' name.
autodoc_class_signature = "separated"

Python HTTP library with thread-safe connection pooling, file post support, user friendly, and more

class urllib3.BaseHTTPResponse(
*,
headers: Mapping[str, str] | Mapping[bytes, bytes] | None = None,
status: int,
version: int,
version_string: str,
reason: str | None,
decode_content: bool,
request_url: str | None,
retries: Retry | None = None,
)[source]#
close() None[source]#

Flush and close the IO object.

This method has no effect if the file is already closed.

get_redirect_location() str | None | Literal[False][source]#

Should we redirect and where to?

Returns:

Truthy redirect location string if we got a redirect status code and valid location. None if redirect status and no location. False if not a redirect status code.

json() Any[source]#

Deserializes the body of the HTTP response as a Python object.

The body of the HTTP response must be encoded using UTF-8, as per RFC 8529 Section 8.1.

To use a custom JSON decoder pass the result of HTTPResponse.data to your custom decoder instead.

If the body of the HTTP response is not decodable to UTF-8, a UnicodeDecodeError will be raised. If the body of the HTTP response is not a valid JSON document, a json.JSONDecodeError will be raised.

Read more here.

Returns:

The body of the HTTP response as a Python object.

class urllib3.HTTPConnectionPool(
host: str,
port: int | None = None,
timeout: Timeout | float | _TYPE_DEFAULT | None = _TYPE_DEFAULT.token,
maxsize: int = 1,
block: bool = False,
headers: Mapping[str, str] | None = None,
retries: Retry | bool | int | None = None,
_proxy: Url | None = None,
_proxy_headers: Mapping[str, str] | None = None,
_proxy_config: ProxyConfig | None = None,
**conn_kw: Any,
)[source]#

Thread-safe connection pool for one host.

Parameters:
  • host – Host used for this HTTP Connection (e.g. “localhost”), passed into http.client.HTTPConnection.

  • port – Port used for this HTTP Connection (None is equivalent to 80), passed into http.client.HTTPConnection.

  • timeout – Socket timeout in seconds for each individual connection. This can be a float or integer, which sets the timeout for the HTTP request, or an instance of urllib3.util.Timeout which gives you more fine-grained control over request timeouts. After the constructor has been parsed, this is always a urllib3.util.Timeout object.

  • maxsize – Number of connections to save that can be reused. More than 1 is useful in multithreaded situations. If block is set to False, more connections will be created but they will not be saved once they’ve been used.

  • block – If set to True, no more than maxsize connections will be used at a time. When no free connections are available, the call will block until a connection has been released. This is a useful side effect for particular multithreaded situations where one does not want to use more than maxsize connections per host to prevent flooding.

  • headers – Headers to include with all requests, unless other headers are given explicitly.

  • retries – Retry configuration to use by default with requests in this pool.

  • _proxy – Parsed proxy URL, should not be used directly, instead, see urllib3.ProxyManager

  • _proxy_headers – A dictionary with proxy headers, should not be used directly, instead, see urllib3.ProxyManager

  • **conn_kw – Additional parameters are used to create fresh urllib3.connection.HTTPConnection, urllib3.connection.HTTPSConnection instances.

ConnectionCls#

alias of HTTPConnection

close() None[source]#

Close all pooled connections and disable the pool.

is_same_host(url: str) bool[source]#

Check if the given url is a member of the same host as this connection pool.

urlopen(
method: str,
url: str,
body: bytes | IO[Any] | Iterable[bytes] | str | None = None,
headers: Mapping[str, str] | None = None,
retries: Retry | bool | int | None = None,
redirect: bool = True,
assert_same_host: bool = True,
timeout: Timeout | float | _TYPE_DEFAULT | None = _TYPE_DEFAULT.token,
pool_timeout: int | None = None,
release_conn: bool | None = None,
chunked: bool = False,
body_pos: int | _TYPE_FAILEDTELL | None = None,
preload_content: bool = True,
decode_content: bool = True,
**response_kw: Any,
) BaseHTTPResponse[source]#

Get a connection from the pool and perform an HTTP request. This is the lowest level call for making a request, so you’ll need to specify all the raw details.

Note

More commonly, it’s appropriate to use a convenience method such as request().

Note

release_conn will only behave as expected if preload_content=False because we want to make preload_content=False the default behaviour someday soon without breaking backwards compatibility.

Parameters:
  • method – HTTP request method (such as GET, POST, PUT, etc.)

  • url – The URL to perform the request on.

  • body – Data to send in the request body, either str, bytes, an iterable of str/bytes, or a file-like object.

  • headers – Dictionary of custom headers to send, such as User-Agent, If-None-Match, etc. If None, pool headers are used. If provided, these headers completely replace any pool-specific headers.

  • retries (Retry, False, or an int.) –

    Configure the number of retries to allow before raising a MaxRetryError exception.

    If None (default) will retry 3 times, see Retry.DEFAULT. Pass a Retry object for fine-grained control over different types of retries. Pass an integer number to retry connection errors that many times, but no other types of errors. Pass zero to never retry.

    If False, then retries are disabled and any exception is raised immediately. Also, instead of raising a MaxRetryError on redirects, the redirect response will be returned.

  • redirect – If True, automatically handle redirects (status codes 301, 302, 303, 307, 308). Each redirect counts as a retry. Disabling retries will disable redirect, too.

  • assert_same_host – If True, will make sure that the host of the pool requests is consistent else will raise HostChangedError. When False, you can use the pool on an HTTP proxy and request foreign hosts.

  • timeout – If specified, overrides the default timeout for this one request. It may be a float (in seconds) or an instance of urllib3.util.Timeout.

  • pool_timeout – If set and the pool is set to block=True, then this method will block for pool_timeout seconds and raise EmptyPoolError if no connection is available within the time period.

  • preload_content (bool) – If True, the response’s body will be preloaded into memory.

  • decode_content (bool) – If True, will attempt to decode the body based on the ‘content-encoding’ header.

  • release_conn – If False, then the urlopen call will not release the connection back into the pool once a response is received (but will release if you read the entire contents of the response such as when preload_content=True). This is useful if you’re not preloading the response’s content immediately. You will need to call r.release_conn() on the response r to return the connection back into the pool. If None, it takes the value of preload_content which defaults to True.

  • chunked (bool) – If True, urllib3 will send the body using chunked transfer encoding. Otherwise, urllib3 will send the body using the standard content-length form. Defaults to False.

  • body_pos (int) – Position to seek to in file-like body in the event of a retry or redirect. Typically this won’t need to be set because urllib3 will auto-populate the value when needed.

class urllib3.HTTPHeaderDict(
headers: ValidHTTPHeaderSource | None = None,
**kwargs: str,
)[source]#
Parameters:
  • headers – An iterable of field-value pairs. Must not contain multiple field names when compared case-insensitively.

  • kwargs – Additional field-value pairs to pass in to dict.update.

A dict like container for storing HTTP Headers.

Field names are stored and compared case-insensitively in compliance with RFC 7230. Iteration provides the first case-sensitive key seen for each case-insensitive pair.

Using __setitem__ syntax overwrites fields that compare equal case-insensitively in order to maintain dict’s api. For fields that compare equal, instead create a new HTTPHeaderDict and use .add in a loop.

If multiple fields that are equal case-insensitively are passed to the constructor or .update, the behavior is undefined and some will be lost.

>>> headers = HTTPHeaderDict()
>>> headers.add('Set-Cookie', 'foo=bar')
>>> headers.add('set-cookie', 'baz=quxx')
>>> headers['content-length'] = '7'
>>> headers['SET-cookie']
'foo=bar, baz=quxx'
>>> headers['Content-Length']
'7'
add(
key: str,
val: str,
*,
combine: bool = False,
) None[source]#

Adds a (name, value) pair, doesn’t overwrite the value if it already exists.

If this is called with combine=True, instead of adding a new header value as a distinct item during iteration, this will instead append the value to any existing header value with a comma. If no existing header value exists for the key, then the value will simply be added, ignoring the combine parameter.

>>> headers = HTTPHeaderDict(foo='bar')
>>> headers.add('Foo', 'baz')
>>> headers['foo']
'bar, baz'
>>> list(headers.items())
[('foo', 'bar'), ('foo', 'baz')]
>>> headers.add('foo', 'quz', combine=True)
>>> list(headers.items())
[('foo', 'bar, baz, quz')]
extend(
*args: ValidHTTPHeaderSource,
**kwargs: str,
) None[source]#

Generic import function for any type of header-like object. Adapted version of MutableMapping.update in order to insert items with self.add instead of self.__setitem__

get_all(
key: str,
default: _Sentinel | _DT = _Sentinel.not_passed,
) list[str] | _DT#

Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.

getallmatchingheaders(
key: str,
default: _Sentinel | _DT = _Sentinel.not_passed,
) list[str] | _DT#

Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.

getheaders(
key: str,
default: _Sentinel | _DT = _Sentinel.not_passed,
) list[str] | _DT#

Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.

getlist(key: str) list[str][source]#
getlist(
key: str,
default: _DT,
) list[str] | _DT

Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.

iget(
key: str,
default: _Sentinel | _DT = _Sentinel.not_passed,
) list[str] | _DT#

Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.

items() a set-like object providing a view on D's items[source]#
iteritems() Iterator[tuple[str, str]][source]#

Iterate over all header lines, including duplicate ones.

itermerged() Iterator[tuple[str, str]][source]#

Iterate over all headers, merging duplicate ones together.

setdefault(
k[,
d,]
) D.get(k,d), also set D[k]=d if k not in D[source]#
class urllib3.HTTPResponse(
body: _TYPE_BODY = '',
headers: Mapping[str, str] | Mapping[bytes, bytes] | None = None,
status: int = 0,
version: int = 0,
version_string: str = 'HTTP/?',
reason: str | None = None,
preload_content: bool = True,
decode_content: bool = True,
original_response: _HttplibHTTPResponse | None = None,
pool: HTTPConnectionPool | None = None,
connection: HTTPConnection | None = None,
msg: _HttplibHTTPMessage | None = None,
retries: Retry | None = None,
enforce_content_length: bool = True,
request_method: str | None = None,
request_url: str | None = None,
auto_close: bool = True,
sock_shutdown: Callable[[int], None] | None = None,
)[source]#

HTTP Response container.

Backwards-compatible with http.client.HTTPResponse but the response body is loaded and decoded on-demand when the data property is accessed. This class is also compatible with the Python standard library’s io module, and can hence be treated as a readable object in the context of that framework.

Extra parameters for behaviour not present in http.client.HTTPResponse:

Parameters:
  • preload_content – If True, the response’s body will be preloaded during construction.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.

  • original_response – When this HTTPResponse wrapper is generated from an http.client.HTTPResponse object, it’s convenient to include the original for debug purposes. It’s otherwise unused.

  • retries – The retries contains the last Retry that was used during the request.

  • enforce_content_length – Enforce content length checking. Body returned by server must match value of Content-Length header, if present. Otherwise, raise error.

close() None[source]#

Flush and close the IO object.

This method has no effect if the file is already closed.

drain_conn() None[source]#

Read and discard any remaining HTTP response data in the response connection.

Unread data in the HTTPResponse connection blocks the connection from being released back to the pool.

fileno() int[source]#

Return underlying file descriptor if one exists.

Raise OSError if the IO object does not use a file descriptor.

flush() None[source]#

Flush write buffers, if applicable.

This is not implemented for read-only and non-blocking streams.

read(
amt: int | None = None,
decode_content: bool | None = None,
cache_content: bool = False,
) bytes[source]#

Similar to http.client.HTTPResponse.read(), but with two additional parameters: decode_content and cache_content.

Parameters:
  • amt – How much of the content to read. If specified, caching is skipped because it doesn’t make sense to cache partial content as the full response.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.

  • cache_content – If True, will save the returned data such that the same result is returned despite of the state of the underlying file object. This is useful if you want the .data property to continue working after having .read() the file object. (Overridden if amt is set.)

read1(
amt: int | None = None,
decode_content: bool | None = None,
) bytes[source]#

Similar to http.client.HTTPResponse.read1 and documented in io.BufferedReader.read1(), but with an additional parameter: decode_content.

Parameters:
  • amt – How much of the content to read.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.

read_chunked(
amt: int | None = None,
decode_content: bool | None = None,
) Generator[bytes, None, None][source]#

Similar to HTTPResponse.read(), but with an additional parameter: decode_content.

Parameters:
  • amt – How much of the content to read. If specified, caching is skipped because it doesn’t make sense to cache partial content as the full response.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.

readable() bool[source]#

Return whether object was opened for reading.

If False, read() will raise OSError.

stream(
amt: int | None = 65536,
decode_content: bool | None = None,
) Generator[bytes, None, None][source]#

A generator wrapper for the read() method. A call will block until amt bytes have been read from the connection or until the connection is closed.

Parameters:
  • amt – How much of the content to read. The generator will return up to much data per iteration, but may return less. This is particularly likely when using compressed data. However, the empty string will never be returned.

  • decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header.

supports_chunked_reads() bool[source]#

Checks if the underlying file-like object looks like a http.client.HTTPResponse object. We do this by testing for the fp attribute. If it is present we assume it returns raw chunks as processed by read_chunked().

tell() int[source]#

Obtain the number of bytes pulled over the wire so far. May differ from the amount of content returned by :meth:urllib3.response.HTTPResponse.read if bytes are encoded on the wire (e.g, compressed).

property url: str | None#

Returns the URL that was the source of this response. If the request that generated this response redirected, this method will return the final redirect location.

class urllib3.HTTPSConnectionPool(
host: str,
port: int | None = None,
timeout: _TYPE_TIMEOUT | None = _TYPE_DEFAULT.token,
maxsize: int = 1,
block: bool = False,
headers: Mapping[str, str] | None = None,
retries: Retry | bool | int | None = None,
_proxy: Url | None = None,
_proxy_headers: Mapping[str, str] | None = None,
key_file: str | None = None,
cert_file: str | None = None,
cert_reqs: int | str | None = None,
key_password: str | None = None,
ca_certs: str | None = None,
ssl_version: int | str | None = None,
ssl_minimum_version: ssl.TLSVersion | None = None,
ssl_maximum_version: ssl.TLSVersion | None = None,
assert_hostname: str | Literal[False] | None = None,
assert_fingerprint: str | None = None,
ca_cert_dir: str | None = None,
**conn_kw: Any,
)[source]#

Same as HTTPConnectionPool, but HTTPS.

HTTPSConnection uses one of assert_fingerprint, assert_hostname and host in this order to verify connections. If assert_hostname is False, no verification is done.

The key_file, cert_file, cert_reqs, ca_certs, ca_cert_dir, ssl_version, key_password are only used if ssl is available and are fed into urllib3.util.ssl_wrap_socket() to upgrade the connection socket into an SSL socket.

ConnectionCls#

alias of HTTPSConnection

class urllib3.PoolManager(
num_pools: int = 10,
headers: Mapping[str, str] | None = None,
**connection_pool_kw: Any,
)[source]#

Allows for arbitrary requests while transparently keeping track of necessary connection pools for you.

Parameters:
  • num_pools – Number of connection pools to cache before discarding the least recently used pool.

  • headers – Headers to include with all requests, unless other headers are given explicitly.

  • **connection_pool_kw – Additional parameters are used to create fresh urllib3.connectionpool.ConnectionPool instances.

Example:

import urllib3

http = urllib3.PoolManager(num_pools=2)

resp1 = http.request("GET", "https://google.com/")
resp2 = http.request("GET", "https://google.com/mail")
resp3 = http.request("GET", "https://yahoo.com/")

print(len(http.pools))
# 2
clear() None[source]#

Empty our store of pools and direct them all to close.

This will not affect in-flight connections, but they will not be re-used after completion.

connection_from_context(
request_context: dict[str, Any],
) HTTPConnectionPool[source]#

Get a urllib3.connectionpool.ConnectionPool based on the request context.

request_context must at least contain the scheme key and its value must be a key in key_fn_by_scheme instance variable.

connection_from_host(
host: str | None,
port: int | None = None,
scheme: str | None = 'http',
pool_kwargs: dict[str, Any] | None = None,
) HTTPConnectionPool[source]#

Get a urllib3.connectionpool.ConnectionPool based on the host, port, and scheme.

If port isn’t given, it will be derived from the scheme using urllib3.connectionpool.port_by_scheme. If pool_kwargs is provided, it is merged with the instance’s connection_pool_kw variable and used to create the new connection pool, if one is needed.

connection_from_pool_key(
pool_key: PoolKey,
request_context: dict[str, Any],
) HTTPConnectionPool[source]#

Get a urllib3.connectionpool.ConnectionPool based on the provided pool key.

pool_key should be a namedtuple that only contains immutable objects. At a minimum it must have the scheme, host, and port fields.

connection_from_url(
url: str,
pool_kwargs: dict[str, Any] | None = None,
) HTTPConnectionPool[source]#

Similar to urllib3.connectionpool.connection_from_url().

If pool_kwargs is not provided and a new pool needs to be constructed, self.connection_pool_kw is used to initialize the urllib3.connectionpool.ConnectionPool. If pool_kwargs is provided, it is used instead. Note that if a new pool does not need to be created for the request, the provided pool_kwargs are not used.

urlopen(
method: str,
url: str,
redirect: bool = True,
**kw: Any,
) BaseHTTPResponse[source]#

Same as urllib3.HTTPConnectionPool.urlopen() with custom cross-host redirect logic and only sends the request-uri portion of the url.

The given url parameter must be absolute, such that an appropriate urllib3.connectionpool.ConnectionPool can be chosen for it.

class urllib3.ProxyManager(
proxy_url: str,
num_pools: int = 10,
headers: Mapping[str, str] | None = None,
proxy_headers: Mapping[str, str] | None = None,
proxy_ssl_context: ssl.SSLContext | None = None,
use_forwarding_for_https: bool = False,
proxy_assert_hostname: None | str | Literal[False] = None,
proxy_assert_fingerprint: str | None = None,
**connection_pool_kw: Any,
)[source]#

Behaves just like PoolManager, but sends all requests through the defined proxy, using the CONNECT method for HTTPS URLs.

Parameters:
  • proxy_url – The URL of the proxy to be used.

  • proxy_headers – A dictionary containing headers that will be sent to the proxy. In case of HTTP they are being sent with each request, while in the HTTPS/CONNECT case they are sent only once. Could be used for proxy authentication.

  • proxy_ssl_context – The proxy SSL context is used to establish the TLS connection to the proxy when using HTTPS proxies.

  • use_forwarding_for_https – (Defaults to False) If set to True will forward requests to the HTTPS proxy to be made on behalf of the client instead of creating a TLS tunnel via the CONNECT method. Enabling this flag means that request and response headers and content will be visible from the HTTPS proxy whereas tunneling keeps request and response headers and content private. IP address, target hostname, SNI, and port are always visible to an HTTPS proxy even when this flag is disabled.

  • proxy_assert_hostname – The hostname of the certificate to verify against.

  • proxy_assert_fingerprint – The fingerprint of the certificate to verify against.

Example:

import urllib3

proxy = urllib3.ProxyManager("https://localhost:3128/")

resp1 = proxy.request("GET", "https://google.com/")
resp2 = proxy.request("GET", "https://httpbin.org/")

print(len(proxy.pools))
# 1

resp3 = proxy.request("GET", "https://httpbin.org/")
resp4 = proxy.request("GET", "https://twitter.com/")

print(len(proxy.pools))
# 3
connection_from_host(
host: str | None,
port: int | None = None,
scheme: str | None = 'http',
pool_kwargs: dict[str, Any] | None = None,
) HTTPConnectionPool[source]#

Get a urllib3.connectionpool.ConnectionPool based on the host, port, and scheme.

If port isn’t given, it will be derived from the scheme using urllib3.connectionpool.port_by_scheme. If pool_kwargs is provided, it is merged with the instance’s connection_pool_kw variable and used to create the new connection pool, if one is needed.

urlopen(
method: str,
url: str,
redirect: bool = True,
**kw: Any,
) BaseHTTPResponse[source]#

Same as HTTP(S)ConnectionPool.urlopen, url must be absolute.

class urllib3.Retry(
total: bool | int | None = 10,
connect: int | None = None,
read: int | None = None,
redirect: bool | int | None = None,
status: int | None = None,
other: int | None = None,
allowed_methods: Collection[str] | None = frozenset({'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE'}),
status_forcelist: Collection[int] | None = None,
backoff_factor: float = 0,
backoff_max: float = 120,
raise_on_redirect: bool = True,
raise_on_status: bool = True,
history: tuple[RequestHistory, ...] | None = None,
respect_retry_after_header: bool = True,
remove_headers_on_redirect: Collection[str] = frozenset({'Authorization', 'Cookie', 'Proxy-Authorization'}),
backoff_jitter: float = 0.0,
retry_after_max: int = 21600,
)[source]#

Retry configuration.

Each retry attempt will create a new Retry object with updated values, so they can be safely reused.

Retries can be defined as a default for a pool:

retries = Retry(connect=5, read=2, redirect=5)
http = PoolManager(retries=retries)
response = http.request("GET", "https://example.com/")

Or per-request (which overrides the default for the pool):

response = http.request("GET", "https://example.com/", retries=Retry(10))

Retries can be disabled by passing False:

response = http.request("GET", "https://example.com/", retries=False)

Errors will be wrapped in MaxRetryError unless retries are disabled, in which case the causing exception will be raised.

Parameters:
  • total (int) –

    Total number of retries to allow. Takes precedence over other counts.

    Set to None to remove this constraint and fall back on other counts.

    Set to 0 to fail on the first retry.

    Set to False to disable and imply raise_on_redirect=False.

  • connect (int) –

    How many connection-related errors to retry on.

    These are errors raised before the request is sent to the remote server, which we assume has not triggered the server to process the request.

    Set to 0 to fail on the first retry of this type.

  • read (int) –

    How many times to retry on read errors.

    These errors are raised after the request was sent to the server, so the request may have side-effects.

    Set to 0 to fail on the first retry of this type.

  • redirect (int) –

    How many redirects to perform. Limit this to avoid infinite redirect loops.

    A redirect is a HTTP response with a status code 301, 302, 303, 307 or 308.

    Set to 0 to fail on the first retry of this type.

    Set to False to disable and imply raise_on_redirect=False.

  • status (int) –

    How many times to retry on bad status codes.

    These are retries made on responses, where status code matches status_forcelist.

    Set to 0 to fail on the first retry of this type.

  • other (int) –

    How many times to retry on other errors.

    Other errors are errors that are not connect, read, redirect or status errors. These errors might be raised after the request was sent to the server, so the request might have side-effects.

    Set to 0 to fail on the first retry of this type.

    If total is not set, it’s a good idea to set this to 0 to account for unexpected edge cases and avoid infinite retry loops.

  • allowed_methods (Collection) –

    Set of uppercased HTTP method verbs that we should retry on.

    By default, we only retry on methods which are considered to be idempotent (multiple requests with the same parameters end with the same state). See Retry.DEFAULT_ALLOWED_METHODS.

    Set to a None value to retry on any verb.

  • status_forcelist (Collection) –

    A set of integer HTTP status codes that we should force a retry on. A retry is initiated if the request method is in allowed_methods and the response status code is in status_forcelist.

    By default, this is disabled with None.

  • backoff_factor (float) –

    A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a second try without a delay). urllib3 will sleep for:

    {backoff factor} * (2 ** ({number of previous retries}))
    

    seconds. If backoff_jitter is non-zero, this sleep is extended by:

    random.uniform(0, {backoff jitter})
    

    seconds. For example, if the backoff_factor is 0.1, then Retry.sleep() will sleep for [0.0s, 0.2s, 0.4s, 0.8s, …] between retries. No backoff will ever be longer than backoff_max.

    By default, backoff is disabled (factor set to 0).

  • raise_on_redirect (bool) – Whether, if the number of redirects is exhausted, to raise a MaxRetryError, or to return a response with a response code in the 3xx range.

  • raise_on_status (bool) – Similar meaning to raise_on_redirect: whether we should raise an exception, or return a response, if status falls in status_forcelist range and retries have been exhausted.

  • history (tuple) – The history of the request encountered during each call to increment(). The list is in the order the requests occurred. Each list item is of class RequestHistory.

  • respect_retry_after_header (bool) – Whether to respect Retry-After header on status codes defined as Retry.RETRY_AFTER_STATUS_CODES or not.

  • remove_headers_on_redirect (Collection) – Sequence of headers to remove from the request when a response indicating a redirect is returned before firing off the redirected request.

  • retry_after_max (int) – Number of seconds to allow as the maximum for Retry-After headers. Defaults to Retry.DEFAULT_RETRY_AFTER_MAX. Any Retry-After headers larger than this value will be limited to this value.

DEFAULT_ALLOWED_METHODS = frozenset({'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PUT', 'TRACE'})#

Default methods to be used for allowed_methods

DEFAULT_BACKOFF_MAX = 120#

Default maximum backoff time.

DEFAULT_REMOVE_HEADERS_ON_REDIRECT = frozenset({'Authorization', 'Cookie', 'Proxy-Authorization'})#

Default headers to be used for remove_headers_on_redirect

DEFAULT_RETRY_AFTER_MAX: Final[int] = 21600#

Default maximum allowed value for Retry-After headers in seconds

RETRY_AFTER_STATUS_CODES = frozenset({413, 429, 503})#

Default status codes to be used for status_forcelist

classmethod from_int(
retries: Retry | bool | int | None,
redirect: bool | int | None = True,
default: Retry | bool | int | None = None,
) Retry[source]#

Backwards-compatibility for the old retries format.

get_backoff_time() float[source]#

Formula for computing the current backoff

Return type:

float

get_retry_after(
response: BaseHTTPResponse,
) float | None[source]#

Get the value of Retry-After in seconds.

increment(
method: str | None = None,
url: str | None = None,
response: BaseHTTPResponse | None = None,
error: Exception | None = None,
_pool: ConnectionPool | None = None,
_stacktrace: TracebackType | None = None,
) Self[source]#

Return a new Retry object with incremented retry counters.

Parameters:
  • response (BaseHTTPResponse) – A response object, or None, if the server did not return a response.

  • error (Exception) – An error encountered during the request, or None if the response was received successfully.

Returns:

A new Retry object.

is_exhausted() bool[source]#

Are we out of retries?

is_retry(
method: str,
status_code: int,
has_retry_after: bool = False,
) bool[source]#

Is this method/status code retryable? (Based on allowlists and control variables such as the number of total retries to allow, whether to respect the Retry-After header, whether this header is present, and whether the returned status code is on the list of status codes to be retried upon on the presence of the aforementioned header)

sleep(
response: BaseHTTPResponse | None = None,
) None[source]#

Sleep between retry attempts.

This method will respect a server’s Retry-After response header and sleep the duration of the time requested. If that is not present, it will use an exponential backoff. By default, the backoff factor is 0 and this method will return immediately.

class urllib3.Timeout(
total: float | _TYPE_DEFAULT | None = None,
connect: float | _TYPE_DEFAULT | None = _TYPE_DEFAULT.token,
read: float | _TYPE_DEFAULT | None = _TYPE_DEFAULT.token,
)[source]#

Timeout configuration.

Timeouts can be defined as a default for a pool:

import urllib3

timeout = urllib3.util.Timeout(connect=2.0, read=7.0)

http = urllib3.PoolManager(timeout=timeout)

resp = http.request("GET", "https://example.com/")

print(resp.status)

Or per-request (which overrides the default for the pool):

response = http.request("GET", "https://example.com/", timeout=Timeout(10))

Timeouts can be disabled by setting all the parameters to None:

no_timeout = Timeout(connect=None, read=None)
response = http.request("GET", "https://example.com/", timeout=no_timeout)
Parameters:
  • total (int, float, or None) –

    This combines the connect and read timeouts into one; the read timeout will be set to the time leftover from the connect attempt. In the event that both a connect timeout and a total are specified, or a read timeout and a total are specified, the shorter timeout will be applied.

    Defaults to None.

  • connect (int, float, or None) – The maximum amount of time (in seconds) to wait for a connection attempt to a server to succeed. Omitting the parameter will default the connect timeout to the system default, probably the global default timeout in socket.py. None will set an infinite timeout for connection attempts.

  • read (int, float, or None) –

    The maximum amount of time (in seconds) to wait between consecutive read operations for a response from the server. Omitting the parameter will default the read timeout to the system default, probably the global default timeout in socket.py. None will set an infinite timeout.

Note

Many factors can affect the total amount of time for urllib3 to return an HTTP response.

For example, Python’s DNS resolver does not obey the timeout specified on the socket. Other factors that can affect total request time include high CPU load, high swap, the program running at a low priority level, or other behaviors.

In addition, the read and total timeouts only measure the time between read operations on the socket connecting the client and the server, not the total amount of time for the request to return a complete response. For most requests, the timeout is raised because the server has not sent the first byte in the specified time. This is not always the case; if a server streams one byte every fifteen seconds, a timeout of 20 seconds will not trigger, even though the request will take several minutes to complete.

DEFAULT_TIMEOUT: float | _TYPE_DEFAULT | None = -1#

A sentinel object representing the default timeout value

clone() Timeout[source]#

Create a copy of the timeout object

Timeout properties are stored per-pool but each request needs a fresh Timeout object to ensure each one has its own start/stop configured.

Returns:

a copy of the timeout object

Return type:

Timeout

property connect_timeout: float | _TYPE_DEFAULT | None#

Get the value to use when setting a connection timeout.

This will be a positive float or integer, the value None (never timeout), or the default system timeout.

Returns:

Connect timeout.

Return type:

int, float, Timeout.DEFAULT_TIMEOUT or None

classmethod from_float(
timeout: float | _TYPE_DEFAULT | None,
) Timeout[source]#

Create a new Timeout from a legacy timeout value.

The timeout value used by httplib.py sets the same timeout on the connect(), and recv() socket requests. This creates a Timeout object that sets the individual timeouts to the timeout value passed to this function.

Parameters:

timeout (integer, float, urllib3.util.Timeout.DEFAULT_TIMEOUT, or None) – The legacy timeout value.

Returns:

Timeout object

Return type:

Timeout

get_connect_duration() float[source]#

Gets the time elapsed since the call to start_connect().

Returns:

Elapsed time in seconds.

Return type:

float

Raises:

urllib3.exceptions.TimeoutStateError – if you attempt to get duration for a timer that hasn’t been started.

property read_timeout: float | None#

Get the value for the read timeout.

This assumes some time has elapsed in the connection timeout and computes the read timeout appropriately.

If self.total is set, the read timeout is dependent on the amount of time taken by the connect timeout. If the connection time has not been established, a TimeoutStateError will be raised.

Returns:

Value to use for the read timeout.

Return type:

int, float or None

Raises:

urllib3.exceptions.TimeoutStateError – If start_connect() has not yet been called on this object.

start_connect() float[source]#

Start the timeout clock, used during a connect() attempt

Raises:

urllib3.exceptions.TimeoutStateError – if you attempt to start a timer that has been started already.

urllib3.add_stderr_logger(
level: int = 10,
) StreamHandler[TextIO][source]#

Helper for quickly adding a StreamHandler to the logger. Useful for debugging.

Returns the handler after adding it.

urllib3.connection_from_url(
url: str,
**kw: Any,
) HTTPConnectionPool[source]#

Given a url, return an ConnectionPool instance of its host.

This is a shortcut for not having to parse out the scheme, host, and port of the url before creating an ConnectionPool instance.

Parameters:
  • url – Absolute URL string that must include the scheme. Port is optional.

  • **kw – Passes additional parameters to the constructor of the appropriate ConnectionPool. Useful for specifying things like timeout, maxsize, headers, etc.

Example:

>>> conn = connection_from_url('http://google.com/')
>>> r = conn.request('GET', '/')
urllib3.disable_warnings(
category: type[Warning] = <class 'urllib3.exceptions.HTTPWarning'>,
) None[source]#

Helper for quickly disabling all urllib3 warnings.

urllib3.encode_multipart_formdata(
fields: Sequence[tuple[str, str | bytes | tuple[str, str | bytes] | tuple[str, str | bytes, str]] | RequestField] | Mapping[str, str | bytes | tuple[str, str | bytes] | tuple[str, str | bytes, str]],
boundary: str | None = None,
) tuple[bytes, str][source]#

Encode a dictionary of fields using the multipart/form-data MIME format.

Parameters:
  • fields – Dictionary of fields or list of (key, RequestField). Values are processed by urllib3.fields.RequestField.from_tuples().

  • boundary – If not specified, then a random boundary will be generated using urllib3.filepost.choose_boundary().

urllib3.make_headers(
keep_alive: bool | None = None,
accept_encoding: bool | list[str] | str | None = None,
user_agent: str | None = None,
basic_auth: str | None = None,
proxy_basic_auth: str | None = None,
disable_cache: bool | None = None,
) dict[str, str][source]#

Shortcuts for generating request headers.

Parameters:
  • keep_alive – If True, adds ‘connection: keep-alive’ header.

  • accept_encoding – Can be a boolean, list, or string. True translates to ‘gzip,deflate’. If the dependencies for Brotli (either the brotli or brotlicffi package) and/or Zstandard (the backports.zstd package for Python before 3.14) algorithms are installed, then their encodings are included in the string (‘br’ and ‘zstd’, respectively). List will get joined by comma. String will be used as provided.

  • user_agent – String representing the user-agent you want, such as “python-urllib3/0.6”

  • basic_auth – Colon-separated username:password string for ‘authorization: basic …’ auth header.

  • proxy_basic_auth – Colon-separated username:password string for ‘proxy-authorization: basic …’ auth header.

  • disable_cache – If True, adds ‘cache-control: no-cache’ header.

Example:

import urllib3

print(urllib3.util.make_headers(keep_alive=True, user_agent="Batman/1.0"))
# {'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
print(urllib3.util.make_headers(accept_encoding=True))
# {'accept-encoding': 'gzip,deflate'}
urllib3.request(
method: str,
url: str,
*,
body: bytes | IO[Any] | Iterable[bytes] | str | None = None,
fields: Sequence[tuple[str, str | bytes | tuple[str, str | bytes] | tuple[str, str | bytes, str]] | RequestField] | Mapping[str, str | bytes | tuple[str, str | bytes] | tuple[str, str | bytes, str]] | None = None,
headers: Mapping[str, str] | None = None,
preload_content: bool | None = True,
decode_content: bool | None = True,
redirect: bool | None = True,
retries: Retry | bool | int | None = None,
timeout: Timeout | float | int | None = 3,
json: Any | None = None,
) BaseHTTPResponse[source]#

A convenience, top-level request method. It uses a module-global PoolManager instance. Therefore, its side effects could be shared across dependencies relying on it. To avoid side effects create a new PoolManager instance and use it instead. The method does not accept low-level **urlopen_kw keyword arguments.

Parameters:
  • method – HTTP request method (such as GET, POST, PUT, etc.)

  • url – The URL to perform the request on.

  • body – Data to send in the request body, either str, bytes, an iterable of str/bytes, or a file-like object.

  • fields – Data to encode and send in the request body.

  • headers – Dictionary of custom headers to send, such as User-Agent, If-None-Match, etc.

  • preload_content (bool) – If True, the response’s body will be preloaded into memory.

  • decode_content (bool) – If True, will attempt to decode the body based on the ‘content-encoding’ header.

  • redirect – If True, automatically handle redirects (status codes 301, 302, 303, 307, 308). Each redirect counts as a retry. Disabling retries will disable redirect, too.

  • retries (Retry, False, or an int.) –

    Configure the number of retries to allow before raising a MaxRetryError exception.

    If None (default) will retry 3 times, see Retry.DEFAULT. Pass a Retry object for fine-grained control over different types of retries. Pass an integer number to retry connection errors that many times, but no other types of errors. Pass zero to never retry.

    If False, then retries are disabled and any exception is raised immediately. Also, instead of raising a MaxRetryError on redirects, the redirect response will be returned.

  • timeout – If specified, overrides the default timeout for this one request. It may be a float (in seconds) or an instance of urllib3.util.Timeout.

  • json – Data to encode and send as JSON with UTF-encoded in the request body. The "Content-Type" header will be set to "application/json" unless specified otherwise.