Reference

Submodules

urllib3.connection module

class urllib3.connection.DummyConnection

Bases: object

Used to detect a failed ConnectionCls import.

class urllib3.connection.HTTPConnection(*args, **kw)

Bases: http.client.HTTPConnection, object

Based on httplib.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:

  • strict: See the documentation on urllib3.connectionpool.HTTPConnectionPool

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

    Note

    This is ignored for Python 2.6. It is only applied for 2.7 and 3.x

  • 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., []).

connect()

Connect to the host and port specified in __init__.

default_port = 80
default_socket_options = [(6, 1, 1)]

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

host

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 = False

Whether this connection verifies the host’s certificate.

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

socket_options = None

The socket options provided by the user. If no options are provided, we use the default options.

urllib3.connection.HTTPSConnection

alias of urllib3.connection.VerifiedHTTPSConnection

urllib3.connection.UnverifiedHTTPSConnection

alias of urllib3.connection.HTTPSConnection

class urllib3.connection.VerifiedHTTPSConnection(host, port=None, key_file=None, cert_file=None, strict=None, timeout=<object object>, ssl_context=None, **kw)

Bases: urllib3.connection.HTTPSConnection

Based on httplib.HTTPSConnection but wraps the socket with SSL certification.

assert_fingerprint = None
ca_cert_dir = None
ca_certs = None
cert_reqs = None
connect()

Connect to the host and port specified in __init__.

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

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

ssl_version = None

urllib3.connectionpool module

class urllib3.connectionpool.ConnectionPool(host, port=None)

Bases: object

Base class for all connection pools, such as HTTPConnectionPool and HTTPSConnectionPool.

QueueCls

alias of urllib3.util.queue.LifoQueue

close()

Close all pooled connections and disable the pool.

scheme = None
class urllib3.connectionpool.HTTPConnectionPool(host, port=None, strict=False, timeout=<object object>, maxsize=1, block=False, headers=None, retries=None, _proxy=None, _proxy_headers=None, **conn_kw)

Bases: urllib3.connectionpool.ConnectionPool, urllib3.request.RequestMethods

Thread-safe connection pool for one host.

Parameters:
  • host – Host used for this HTTP Connection (e.g. “localhost”), passed into httplib.HTTPConnection.
  • port – Port used for this HTTP Connection (None is equivalent to 80), passed into httplib.HTTPConnection.
  • strict

    Causes BadStatusLine to be raised if the status line can’t be parsed as a valid HTTP/1.0 or 1.1 status line, passed into httplib.HTTPConnection.

    Note

    Only works in Python 2. This parameter is ignored in Python 3.

  • 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.connectionpool.ProxyManager
  • _proxy_headers – A dictionary with proxy headers, should not be used directly, instead, see urllib3.connectionpool.ProxyManager
  • **conn_kw – Additional parameters are used to create fresh urllib3.connection.HTTPConnection, urllib3.connection.HTTPSConnection instances.
ConnectionCls

alias of urllib3.connection.HTTPConnection

ResponseCls

alias of urllib3.response.HTTPResponse

close()

Close all pooled connections and disable the pool.

is_same_host(url)

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

scheme = 'http'
urlopen(method, url, body=None, headers=None, retries=None, redirect=True, assert_same_host=True, timeout=<object object>, pool_timeout=None, release_conn=None, chunked=False, body_pos=None, **response_kw)

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 provided by RequestMethods, 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.)
  • body – Data to send in the request body (useful for creating POST requests, see HTTPConnectionPool.post_url for more convenience).
  • 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.

    Pass None to retry until you receive a response. 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.
  • 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 response_kw.get('preload_content', True).
  • chunked – 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.
  • **response_kw – Additional parameters are passed to urllib3.response.HTTPResponse.from_httplib()
class urllib3.connectionpool.HTTPSConnectionPool(host, port=None, strict=False, timeout=<object object>, maxsize=1, block=False, headers=None, retries=None, _proxy=None, _proxy_headers=None, key_file=None, cert_file=None, cert_reqs=None, ca_certs=None, ssl_version=None, assert_hostname=None, assert_fingerprint=None, ca_cert_dir=None, **conn_kw)

Bases: urllib3.connectionpool.HTTPConnectionPool

Same as HTTPConnectionPool, but HTTPS.

When Python is compiled with the ssl module, then VerifiedHTTPSConnection is used, which can verify certificates, instead of HTTPSConnection.

VerifiedHTTPSConnection 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, and ssl_version 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 urllib3.connection.VerifiedHTTPSConnection

scheme = 'https'
urllib3.connectionpool.connection_from_url(url, **kw)

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.exceptions module

exception urllib3.exceptions.BodyNotHttplibCompatible

Bases: urllib3.exceptions.HTTPError

Body should be httplib.HTTPResponse like (have an fp attribute which returns raw chunks) for read_chunked().

exception urllib3.exceptions.ClosedPoolError(pool, message)

Bases: urllib3.exceptions.PoolError

Raised when a request enters a pool after the pool has been closed.

exception urllib3.exceptions.ConnectTimeoutError

Bases: urllib3.exceptions.TimeoutError

Raised when a socket timeout occurs while connecting to a server

urllib3.exceptions.ConnectionError

Renamed to ProtocolError but aliased for backwards compatibility.

alias of urllib3.exceptions.ProtocolError

exception urllib3.exceptions.DecodeError

Bases: urllib3.exceptions.HTTPError

Raised when automatic decoding based on Content-Type fails.

exception urllib3.exceptions.DependencyWarning

Bases: urllib3.exceptions.HTTPWarning

Warned when an attempt is made to import a module with missing optional dependencies.

exception urllib3.exceptions.EmptyPoolError(pool, message)

Bases: urllib3.exceptions.PoolError

Raised when a pool runs out of connections and no more are allowed.

exception urllib3.exceptions.HTTPError

Bases: Exception

Base exception used by this module.

exception urllib3.exceptions.HTTPWarning

Bases: Warning

Base warning used by this module.

exception urllib3.exceptions.HeaderParsingError(defects, unparsed_data)

Bases: urllib3.exceptions.HTTPError

Raised by assert_header_parsing, but we convert it to a log.warning statement.

exception urllib3.exceptions.HostChangedError(pool, url, retries=3)

Bases: urllib3.exceptions.RequestError

Raised when an existing pool gets a request for a foreign host.

exception urllib3.exceptions.IncompleteRead(partial, expected)

Bases: urllib3.exceptions.HTTPError, http.client.IncompleteRead

Response length doesn’t match expected Content-Length

Subclass of http_client.IncompleteRead to allow int value for partial to avoid creating large objects on streamed reads.

exception urllib3.exceptions.InsecurePlatformWarning

Bases: urllib3.exceptions.SecurityWarning

Warned when certain SSL configuration is not available on a platform.

exception urllib3.exceptions.InsecureRequestWarning

Bases: urllib3.exceptions.SecurityWarning

Warned when making an unverified HTTPS request.

exception urllib3.exceptions.InvalidHeader

Bases: urllib3.exceptions.HTTPError

The header provided was somehow invalid.

exception urllib3.exceptions.LocationParseError(location)

Bases: urllib3.exceptions.LocationValueError

Raised when get_host or similar fails to parse the URL input.

exception urllib3.exceptions.LocationValueError

Bases: ValueError, urllib3.exceptions.HTTPError

Raised when there is something wrong with a given URL input.

exception urllib3.exceptions.MaxRetryError(pool, url, reason=None)

Bases: urllib3.exceptions.RequestError

Raised when the maximum number of retries is exceeded.

Parameters:
  • pool (HTTPConnectionPool) – The connection pool
  • url (string) – The requested Url
  • reason (exceptions.Exception) – The underlying error
exception urllib3.exceptions.NewConnectionError(pool, message)

Bases: urllib3.exceptions.ConnectTimeoutError, urllib3.exceptions.PoolError

Raised when we fail to establish a new connection. Usually ECONNREFUSED.

exception urllib3.exceptions.PoolError(pool, message)

Bases: urllib3.exceptions.HTTPError

Base exception for errors caused within a pool.

exception urllib3.exceptions.ProtocolError

Bases: urllib3.exceptions.HTTPError

Raised when something unexpected happens mid-request/response.

exception urllib3.exceptions.ProxyError

Bases: urllib3.exceptions.HTTPError

Raised when the connection to a proxy fails.

exception urllib3.exceptions.ProxySchemeUnknown(scheme)

Bases: AssertionError, ValueError

ProxyManager does not support the supplied scheme

exception urllib3.exceptions.ReadTimeoutError(pool, url, message)

Bases: urllib3.exceptions.TimeoutError, urllib3.exceptions.RequestError

Raised when a socket timeout occurs while receiving data from a server

exception urllib3.exceptions.RequestError(pool, url, message)

Bases: urllib3.exceptions.PoolError

Base exception for PoolErrors that have associated URLs.

exception urllib3.exceptions.ResponseError

Bases: urllib3.exceptions.HTTPError

Used as a container for an error reason supplied in a MaxRetryError.

GENERIC_ERROR = 'too many error responses'
SPECIFIC_ERROR = 'too many {status_code} error responses'
exception urllib3.exceptions.ResponseNotChunked

Bases: urllib3.exceptions.ProtocolError, ValueError

Response needs to be chunked in order to read it as chunks.

exception urllib3.exceptions.SNIMissingWarning

Bases: urllib3.exceptions.HTTPWarning

Warned when making a HTTPS request without SNI available.

exception urllib3.exceptions.SSLError

Bases: urllib3.exceptions.HTTPError

Raised when SSL certificate fails in an HTTPS connection.

exception urllib3.exceptions.SecurityWarning

Bases: urllib3.exceptions.HTTPWarning

Warned when performing security reducing actions

exception urllib3.exceptions.SubjectAltNameWarning

Bases: urllib3.exceptions.SecurityWarning

Warned when connecting to a host with a certificate missing a SAN.

exception urllib3.exceptions.SystemTimeWarning

Bases: urllib3.exceptions.SecurityWarning

Warned when system time is suspected to be wrong

exception urllib3.exceptions.TimeoutError

Bases: urllib3.exceptions.HTTPError

Raised when a socket timeout error occurs.

Catching this error will catch both ReadTimeoutErrors and ConnectTimeoutErrors.

exception urllib3.exceptions.TimeoutStateError

Bases: urllib3.exceptions.HTTPError

Raised when passing an invalid state to a timeout

exception urllib3.exceptions.UnrewindableBodyError

Bases: urllib3.exceptions.HTTPError

urllib3 encountered an error when trying to rewind a body

urllib3.fields module

class urllib3.fields.RequestField(name, data, filename=None, headers=None)

Bases: object

A data container for request body parameters.

Parameters:
  • name – The name of this request field.
  • data – The data/value body.
  • filename – An optional filename of the request field.
  • headers – An optional dict-like object of headers to initially use for the field.
classmethod from_tuples(fieldname, value)

A RequestField factory from old-style tuple parameters.

Supports constructing RequestField from parameter of key/value strings AND key/filetuple. A filetuple is a (filename, data, MIME type) tuple where the MIME type is optional. For example:

'foo': 'bar',
'fakefile': ('foofile.txt', 'contents of foofile'),
'realfile': ('barfile.txt', open('realfile').read()),
'typedfile': ('bazfile.bin', open('bazfile').read(), 'image/jpeg'),
'nonamefile': 'contents of nonamefile field',

Field names and filenames must be unicode.

make_multipart(content_disposition=None, content_type=None, content_location=None)

Makes this request field into a multipart request field.

This method overrides “Content-Disposition”, “Content-Type” and “Content-Location” headers to the request parameter.

Parameters:
  • content_type – The ‘Content-Type’ of the request body.
  • content_location – The ‘Content-Location’ of the request body.
render_headers()

Renders the headers for this request field.

urllib3.fields.format_header_param(name, value)

Helper function to format and quote a single header parameter.

Particularly useful for header parameters which might contain non-ASCII values, like file names. This follows RFC 2231, as suggested by RFC 2388 Section 4.4.

Parameters:
  • name – The name of the parameter, a string expected to be ASCII only.
  • value – The value of the parameter, provided as a unicode string.
urllib3.fields.guess_content_type(filename, default='application/octet-stream')

Guess the “Content-Type” of a file.

Parameters:
  • filename – The filename to guess the “Content-Type” of using mimetypes.
  • default – If no “Content-Type” can be guessed, default to default.

urllib3.filepost module

urllib3.filepost.choose_boundary()

Our embarrassingly-simple replacement for mimetools.choose_boundary.

urllib3.filepost.encode_multipart_formdata(fields, boundary=None)

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

Parameters:
urllib3.filepost.iter_field_objects(fields)

Iterate over fields.

Supports list of (k, v) tuples and dicts, and lists of RequestField.

urllib3.filepost.iter_fields(fields)

Deprecated since version 1.6.

Iterate over fields.

The addition of RequestField makes this function obsolete. Instead, use iter_field_objects(), which returns RequestField objects.

Supports list of (k, v) tuples and dicts.

urllib3.poolmanager module

class urllib3.poolmanager.PoolManager(num_pools=10, headers=None, **connection_pool_kw)

Bases: urllib3.request.RequestMethods

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:

>>> manager = PoolManager(num_pools=2)
>>> r = manager.request('GET', 'http://google.com/')
>>> r = manager.request('GET', 'http://google.com/mail')
>>> r = manager.request('GET', 'http://yahoo.com/')
>>> len(manager.pools)
2
clear()

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)

Get a 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, port=None, scheme='http', pool_kwargs=None)

Get a 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, request_context=None)

Get a 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, pool_kwargs=None)

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.

proxy = None
urlopen(method, url, redirect=True, **kw)

Same as urllib3.connectionpool.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.poolmanager.ProxyManager(proxy_url, num_pools=10, headers=None, proxy_headers=None, **connection_pool_kw)

Bases: urllib3.poolmanager.PoolManager

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.
Example:
>>> proxy = urllib3.ProxyManager('http://localhost:3128/')
>>> r1 = proxy.request('GET', 'http://google.com/')
>>> r2 = proxy.request('GET', 'http://httpbin.org/')
>>> len(proxy.pools)
1
>>> r3 = proxy.request('GET', 'https://httpbin.org/')
>>> r4 = proxy.request('GET', 'https://twitter.com/')
>>> len(proxy.pools)
3
connection_from_host(host, port=None, scheme='http', pool_kwargs=None)

Get a 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, url, redirect=True, **kw)

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

urllib3.poolmanager.proxy_from_url(url, **kw)

urllib3.request module

class urllib3.request.RequestMethods(headers=None)

Bases: object

Convenience mixin for classes who implement a urlopen() method, such as HTTPConnectionPool and PoolManager.

Provides behavior for making common types of HTTP request methods and decides which type of request field encoding to use.

Specifically,

request_encode_url() is for sending requests whose fields are encoded in the URL (such as GET, HEAD, DELETE).

request_encode_body() is for sending requests whose fields are encoded in the body of the request using multipart or www-form-urlencoded (such as for POST, PUT, PATCH).

request() is for making any kind of request, it will look up the appropriate encoding format and use one of the above two methods to make the request.

Initializer parameters:

Parameters:headers – Headers to include with all requests, unless other headers are given explicitly.
request(method, url, fields=None, headers=None, **urlopen_kw)

Make a request using urlopen() with the appropriate encoding of fields based on the method used.

This is a convenience method that requires the least amount of manual effort. It can be used in most situations, while still having the option to drop down to more specific methods when necessary, such as request_encode_url(), request_encode_body(), or even the lowest level urlopen().

request_encode_body(method, url, fields=None, headers=None, encode_multipart=True, multipart_boundary=None, **urlopen_kw)

Make a request using urlopen() with the fields encoded in the body. This is useful for request methods like POST, PUT, PATCH, etc.

When encode_multipart=True (default), then urllib3.filepost.encode_multipart_formdata() is used to encode the payload with the appropriate content type. Otherwise urllib.urlencode() is used with the ‘application/x-www-form-urlencoded’ content type.

Multipart encoding must be used when posting files, and it’s reasonably safe to use it in other times too. However, it may break request signing, such as with OAuth.

Supports an optional fields parameter of key/value strings AND key/filetuple. A filetuple is a (filename, data, MIME type) tuple where the MIME type is optional. For example:

fields = {
    'foo': 'bar',
    'fakefile': ('foofile.txt', 'contents of foofile'),
    'realfile': ('barfile.txt', open('realfile').read()),
    'typedfile': ('bazfile.bin', open('bazfile').read(),
                  'image/jpeg'),
    'nonamefile': 'contents of nonamefile field',
}

When uploading a file, providing a filename (the first parameter of the tuple) is optional but recommended to best mimic behavior of browsers.

Note that if headers are supplied, the ‘Content-Type’ header will be overwritten because it depends on the dynamic random boundary string which is used to compose the body of the request. The random boundary string can be explicitly set with the multipart_boundary parameter.

request_encode_url(method, url, fields=None, headers=None, **urlopen_kw)

Make a request using urlopen() with the fields encoded in the url. This is useful for request methods like GET, HEAD, DELETE, etc.

urlopen(method, url, body=None, headers=None, encode_multipart=True, multipart_boundary=None, **kw)

urllib3.response module

class urllib3.response.DeflateDecoder

Bases: object

decompress(data)
class urllib3.response.GzipDecoder

Bases: object

decompress(data)
class urllib3.response.GzipDecoderState

Bases: object

FIRST_MEMBER = 0
OTHER_MEMBERS = 1
SWALLOW_DATA = 2
class urllib3.response.HTTPResponse(body='', headers=None, status=0, version=0, reason=None, strict=0, preload_content=True, decode_content=True, original_response=None, pool=None, connection=None, msg=None, retries=None, enforce_content_length=False, request_method=None, request_url=None)

Bases: io.IOBase

HTTP Response container.

Backwards-compatible to httplib’s 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 httplib.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 httplib.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.
CONTENT_DECODERS = ['gzip', 'deflate']
REDIRECT_STATUSES = [301, 302, 303, 307, 308]
close()

Flush and close the IO object.

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

closed
connection
data
fileno()

Returns underlying file descriptor if one exists.

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

flush()

Flush write buffers, if applicable.

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

classmethod from_httplib(r, **response_kw)

Given an httplib.HTTPResponse instance r, return a corresponding urllib3.response.HTTPResponse object.

Remaining parameters are passed to the HTTPResponse constructor, along with original_response=r.

get_redirect_location()

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.
getheader(name, default=None)
getheaders()
geturl()

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.

info()
isclosed()
read(amt=None, decode_content=None, cache_content=False)

Similar to httplib.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.)
read_chunked(amt=None, decode_content=None)

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()

Return whether object was opened for reading.

If False, read() will raise OSError.

readinto(b)
release_conn()
stream(amt=65536, decode_content=None)

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()

Checks if the underlying file-like object looks like a httplib.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()

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

Module contents

urllib3 - Thread-safe connection pooling and re-using.

class urllib3.HTTPConnectionPool(host, port=None, strict=False, timeout=<object object>, maxsize=1, block=False, headers=None, retries=None, _proxy=None, _proxy_headers=None, **conn_kw)

Bases: urllib3.connectionpool.ConnectionPool, urllib3.request.RequestMethods

Thread-safe connection pool for one host.

Parameters:
  • host – Host used for this HTTP Connection (e.g. “localhost”), passed into httplib.HTTPConnection.
  • port – Port used for this HTTP Connection (None is equivalent to 80), passed into httplib.HTTPConnection.
  • strict

    Causes BadStatusLine to be raised if the status line can’t be parsed as a valid HTTP/1.0 or 1.1 status line, passed into httplib.HTTPConnection.

    Note

    Only works in Python 2. This parameter is ignored in Python 3.

  • 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.connectionpool.ProxyManager
  • _proxy_headers – A dictionary with proxy headers, should not be used directly, instead, see urllib3.connectionpool.ProxyManager
  • **conn_kw – Additional parameters are used to create fresh urllib3.connection.HTTPConnection, urllib3.connection.HTTPSConnection instances.
ConnectionCls

alias of urllib3.connection.HTTPConnection

ResponseCls

alias of urllib3.response.HTTPResponse

close()

Close all pooled connections and disable the pool.

is_same_host(url)

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

scheme = 'http'
urlopen(method, url, body=None, headers=None, retries=None, redirect=True, assert_same_host=True, timeout=<object object>, pool_timeout=None, release_conn=None, chunked=False, body_pos=None, **response_kw)

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 provided by RequestMethods, 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.)
  • body – Data to send in the request body (useful for creating POST requests, see HTTPConnectionPool.post_url for more convenience).
  • 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.

    Pass None to retry until you receive a response. 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.
  • 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 response_kw.get('preload_content', True).
  • chunked – 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.
  • **response_kw – Additional parameters are passed to urllib3.response.HTTPResponse.from_httplib()
class urllib3.HTTPSConnectionPool(host, port=None, strict=False, timeout=<object object>, maxsize=1, block=False, headers=None, retries=None, _proxy=None, _proxy_headers=None, key_file=None, cert_file=None, cert_reqs=None, ca_certs=None, ssl_version=None, assert_hostname=None, assert_fingerprint=None, ca_cert_dir=None, **conn_kw)

Bases: urllib3.connectionpool.HTTPConnectionPool

Same as HTTPConnectionPool, but HTTPS.

When Python is compiled with the ssl module, then VerifiedHTTPSConnection is used, which can verify certificates, instead of HTTPSConnection.

VerifiedHTTPSConnection 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, and ssl_version 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 urllib3.connection.VerifiedHTTPSConnection

scheme = 'https'
class urllib3.PoolManager(num_pools=10, headers=None, **connection_pool_kw)

Bases: urllib3.request.RequestMethods

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:

>>> manager = PoolManager(num_pools=2)
>>> r = manager.request('GET', 'http://google.com/')
>>> r = manager.request('GET', 'http://google.com/mail')
>>> r = manager.request('GET', 'http://yahoo.com/')
>>> len(manager.pools)
2
clear()

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)

Get a 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, port=None, scheme='http', pool_kwargs=None)

Get a 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, request_context=None)

Get a 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, pool_kwargs=None)

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.

proxy = None
urlopen(method, url, redirect=True, **kw)

Same as urllib3.connectionpool.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, num_pools=10, headers=None, proxy_headers=None, **connection_pool_kw)

Bases: urllib3.poolmanager.PoolManager

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.
Example:
>>> proxy = urllib3.ProxyManager('http://localhost:3128/')
>>> r1 = proxy.request('GET', 'http://google.com/')
>>> r2 = proxy.request('GET', 'http://httpbin.org/')
>>> len(proxy.pools)
1
>>> r3 = proxy.request('GET', 'https://httpbin.org/')
>>> r4 = proxy.request('GET', 'https://twitter.com/')
>>> len(proxy.pools)
3
connection_from_host(host, port=None, scheme='http', pool_kwargs=None)

Get a 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, url, redirect=True, **kw)

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

class urllib3.HTTPResponse(body='', headers=None, status=0, version=0, reason=None, strict=0, preload_content=True, decode_content=True, original_response=None, pool=None, connection=None, msg=None, retries=None, enforce_content_length=False, request_method=None, request_url=None)

Bases: io.IOBase

HTTP Response container.

Backwards-compatible to httplib’s 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 httplib.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 httplib.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.
CONTENT_DECODERS = ['gzip', 'deflate']
REDIRECT_STATUSES = [301, 302, 303, 307, 308]
close()

Flush and close the IO object.

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

closed
connection
data
fileno()

Returns underlying file descriptor if one exists.

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

flush()

Flush write buffers, if applicable.

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

classmethod from_httplib(r, **response_kw)

Given an httplib.HTTPResponse instance r, return a corresponding urllib3.response.HTTPResponse object.

Remaining parameters are passed to the HTTPResponse constructor, along with original_response=r.

get_redirect_location()

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.
getheader(name, default=None)
getheaders()
geturl()

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.

info()
isclosed()
read(amt=None, decode_content=None, cache_content=False)

Similar to httplib.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.)
read_chunked(amt=None, decode_content=None)

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()

Return whether object was opened for reading.

If False, read() will raise OSError.

readinto(b)
release_conn()
stream(amt=65536, decode_content=None)

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()

Checks if the underlying file-like object looks like a httplib.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()

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

class urllib3.Retry(total=10, connect=None, read=None, redirect=None, status=None, method_whitelist=frozenset({'TRACE', 'OPTIONS', 'DELETE', 'HEAD', 'PUT', 'GET'}), status_forcelist=None, backoff_factor=0, raise_on_redirect=True, raise_on_status=True, history=None, respect_retry_after_header=True, remove_headers_on_redirect=frozenset({'Authorization'}))

Bases: object

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', 'http://example.com/')

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

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

Retries can be disabled by passing False:

response = http.request('GET', 'http://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. It’s a good idea to set this to some sensibly-high value to account for unexpected edge cases and avoid infinite retry loops.

    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.

  • method_whitelist (iterable) –

    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_METHOD_WHITELIST.

    Set to a False value to retry on any verb.

  • status_forcelist (iterable) –

    A set of integer HTTP status codes that we should force a retry on. A retry is initiated if the request method is in method_whitelist 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 total retries} - 1))
    

    seconds. If the backoff_factor is 0.1, then sleep() will sleep for [0.0s, 0.2s, 0.4s, …] between retries. It will never be longer than Retry.BACKOFF_MAX.

    By default, backoff is disabled (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 (iterable) – Sequence of headers to remove from the request when a response indicating a redirect is returned before firing off the redirected request.
BACKOFF_MAX = 120

Maximum backoff time.

DEFAULT = Retry(total=3, connect=None, read=None, redirect=None, status=None)
DEFAULT_METHOD_WHITELIST = frozenset({'TRACE', 'OPTIONS', 'DELETE', 'HEAD', 'PUT', 'GET'})
DEFAULT_REDIRECT_HEADERS_BLACKLIST = frozenset({'Authorization'})
RETRY_AFTER_STATUS_CODES = frozenset({503, 413, 429})
classmethod from_int(retries, redirect=True, default=None)

Backwards-compatibility for the old retries format.

get_backoff_time()

Formula for computing the current backoff

Return type:float
get_retry_after(response)

Get the value of Retry-After in seconds.

increment(method=None, url=None, response=None, error=None, _pool=None, _stacktrace=None)

Return a new Retry object with incremented retry counters.

Parameters:
  • response (HTTPResponse) – 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()

Are we out of retries?

is_retry(method, status_code, has_retry_after=False)

Is this method/status code retryable? (Based on whitelists 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)

new(**kw)
parse_retry_after(retry_after)
sleep(response=None)

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.

sleep_for_retry(response=None)
class urllib3.Timeout(total=None, connect=<object object>, read=<object object>)

Bases: object

Timeout configuration.

Timeouts can be defined as a default for a pool:

timeout = Timeout(connect=2.0, read=7.0)
http = PoolManager(timeout=timeout)
response = http.request('GET', 'http://example.com/')

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

response = http.request('GET', 'http://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', 'http://example.com/, timeout=no_timeout)
Parameters:
  • total (integer, 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 (integer, float, or None) – The maximum amount of time 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 (integer, float, or None) –

    The maximum amount of time 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.

If your goal is to cut off any request after a set amount of wall clock time, consider having a second “watcher” thread to cut off a slow request.

DEFAULT_TIMEOUT = <object object>

A sentinel object representing the default timeout value

clone()

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
connect_timeout

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)

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, sentinel default object, or None) – The legacy timeout value.
Returns:Timeout object
Return type:Timeout
get_connect_duration()

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

Returns:Elapsed time.
Return type:float
Raises:urllib3.exceptions.TimeoutStateError – if you attempt to get duration for a timer that hasn’t been started.
read_timeout

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, Timeout.DEFAULT_TIMEOUT or None
Raises:urllib3.exceptions.TimeoutStateError – If start_connect() has not yet been called on this object.
start_connect()

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=10)

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

Returns the handler after adding it.

urllib3.connection_from_url(url, **kw)

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=<class 'urllib3.exceptions.HTTPWarning'>)

Helper for quickly disabling all urllib3 warnings.

urllib3.encode_multipart_formdata(fields, boundary=None)

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

Parameters:
urllib3.get_host(url)

Deprecated. Use parse_url() instead.

urllib3.make_headers(keep_alive=None, accept_encoding=None, user_agent=None, basic_auth=None, proxy_basic_auth=None, disable_cache=None)

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’. 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:

>>> make_headers(keep_alive=True, user_agent="Batman/1.0")
{'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
>>> make_headers(accept_encoding=True)
{'accept-encoding': 'gzip,deflate'}
urllib3.proxy_from_url(url, **kw)