packages.urllib3.connectionpool

Module Contents

Classes

ConnectionPool(self,host,port=None) Base class for all connection pools, such as
HTTPConnectionPool(self,host,port=None,strict=False,timeout=None,maxsize=1,block=False,headers=None,retries=None,_proxy=None,_proxy_headers=None,**conn_kw) Thread-safe connection pool for one host.
HTTPSConnectionPool(self,host,port=None,strict=False,timeout=None,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) Same as HTTPConnectionPool, but HTTPS.

Functions

connection_from_url(url,**kw) Given a url, return an ConnectionPool instance of its host.
class ConnectionPool(host, port=None)

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

__init__(host, port=None)
__str__()
__enter__()
__exit__(exc_type, exc_val, exc_tb)
close()

Close all pooled connections and disable the pool.

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

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.
__init__(host, port=None, strict=False, timeout=None, maxsize=1, block=False, headers=None, retries=None, _proxy=None, _proxy_headers=None, **conn_kw)
_new_conn()

Return a fresh HTTPConnection.

_get_conn(timeout=None)

Get a connection. Will return a pooled connection if one is available.

If no connections are available and :prop:`.block` is False, then a fresh connection is returned.

Parameters:timeout – Seconds to wait before giving up and raising urllib3.exceptions.EmptyPoolError if the pool is empty and :prop:`.block` is True.
_put_conn(conn)

Put a connection back into the pool.

Parameters:conn – Connection object for the current host and port as returned by _new_conn() or _get_conn().

If the pool is already full, the connection is closed and discarded because we exceeded maxsize. If connections are discarded frequently, then maxsize should be increased.

If the pool is closed, then the connection will be closed and discarded.

_validate_conn(conn)

Called right before a request is made, after the socket is created.

_prepare_proxy(conn)
_get_timeout(timeout)

Helper that always returns a urllib3.util.Timeout

_raise_timeout(err, url, timeout_value)

Is the error actually a timeout? Will raise a ReadTimeout or pass

_make_request(conn, method, url, timeout=_Default, **httplib_request_kw)

Perform a request on a given urllib connection object taken from our pool.

Parameters:
  • conn – a connection from one of our connection pools
  • timeout – Socket timeout in seconds for the request. This can be a float or integer, which will set the same timeout value for the socket connect and the socket read, or an instance of urllib3.util.Timeout, which gives you more fine-grained control over your timeouts.
_absolute_url(path)
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.

urlopen(method, url, body=None, headers=None, retries=None, redirect=True, assert_same_host=True, timeout=_Default, pool_timeout=None, release_conn=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).
  • **response_kw – Additional parameters are passed to urllib3.response.HTTPResponse.from_httplib()
class HTTPSConnectionPool(host, port=None, strict=False, timeout=None, 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)

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.

__init__(host, port=None, strict=False, timeout=None, 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)
_prepare_conn(conn)

Prepare the connection for urllib3.util.ssl_wrap_socket() and establish the tunnel if proxy is used.

_prepare_proxy(conn)

Establish tunnel connection early, because otherwise httplib would improperly set Host: header to proxy’s IP:port.

_new_conn()

Return a fresh httplib.HTTPSConnection.

_validate_conn(conn)

Called right before a request is made, after the socket is created.

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', '/')