Pool Manager¶
- class urllib3.PoolManager(num_pools=10, headers=None, **connection_pool_kw)¶
Bases:
RequestMethodsAllows 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.ConnectionPoolinstances.
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
urllib3.connectionpool.ConnectionPoolbased on the request context.request_contextmust at least contain theschemekey and its value must be a key inkey_fn_by_schemeinstance variable.
- connection_from_host(host, port=None, scheme='http', pool_kwargs=None)¶
Get a
urllib3.connectionpool.ConnectionPoolbased on the host, port, and scheme.If
portisn’t given, it will be derived from theschemeusingurllib3.connectionpool.port_by_scheme. Ifpool_kwargsis provided, it is merged with the instance’sconnection_pool_kwvariable and used to create the new connection pool, if one is needed.
- connection_from_pool_key(pool_key, request_context=None)¶
Get a
urllib3.connectionpool.ConnectionPoolbased on the provided pool key.pool_keyshould be a namedtuple that only contains immutable objects. At a minimum it must have thescheme,host, andportfields.
- connection_from_url(url, pool_kwargs=None)¶
Similar to
urllib3.connectionpool.connection_from_url().If
pool_kwargsis not provided and a new pool needs to be constructed,self.connection_pool_kwis used to initialize theurllib3.connectionpool.ConnectionPool. Ifpool_kwargsis provided, it is used instead. Note that if a new pool does not need to be created for the request, the providedpool_kwargsare not used.
- proxy = None¶
- proxy_config = None¶
- request(method, url, fields=None, headers=None, **urlopen_kw)¶
Make a request using
urlopen()with the appropriate encoding offieldsbased on themethodused.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 levelurlopen().
- request_encode_body(method, url, fields=None, headers=None, encode_multipart=True, multipart_boundary=None, **urlopen_kw)¶
Make a request using
urlopen()with thefieldsencoded in the body. This is useful for request methods like POST, PUT, PATCH, etc.When
encode_multipart=True(default), thenurllib3.encode_multipart_formdata()is used to encode the payload with the appropriate content type. Otherwiseurllib.parse.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
fieldsparameter 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
headersare 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 themultipart_boundaryparameter.
- request_encode_url(method, url, fields=None, headers=None, **urlopen_kw)¶
Make a request using
urlopen()with thefieldsencoded in the url. This is useful for request methods like GET, HEAD, DELETE, etc.
- urlopen(method, url, redirect=True, **kw)¶
Same as
urllib3.HTTPConnectionPool.urlopen()with custom cross-host redirect logic and only sends the request-uri portion of theurl.The given
urlparameter must be absolute, such that an appropriateurllib3.connectionpool.ConnectionPoolcan be chosen for it.
- class urllib3.ProxyManager(proxy_url, num_pools=10, headers=None, proxy_headers=None, proxy_ssl_context=None, use_forwarding_for_https=False, **connection_pool_kw)¶
Bases:
PoolManagerBehaves 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.
- 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
urllib3.connectionpool.ConnectionPoolbased on the host, port, and scheme.If
portisn’t given, it will be derived from theschemeusingurllib3.connectionpool.port_by_scheme. Ifpool_kwargsis provided, it is merged with the instance’sconnection_pool_kwvariable and used to create the new connection pool, if one is needed.
- urlopen(method, url, redirect=True, **kw)¶
Same as HTTP(S)ConnectionPool.urlopen,
urlmust be absolute.
- class urllib3.poolmanager.PoolKey(key_scheme, key_host, key_port, key_timeout, key_retries, key_strict, key_block, key_source_address, key_key_file, key_key_password, key_cert_file, key_cert_reqs, key_ca_certs, key_ssl_version, key_ca_cert_dir, key_ssl_context, key_maxsize, key_headers, key__proxy, key__proxy_headers, key__proxy_config, key_socket_options, key__socks_options, key_assert_hostname, key_assert_fingerprint, key_server_hostname)¶
Bases:
tupleThe namedtuple class used to construct keys for the connection pool. All custom key schemes should include the fields in this key at a minimum.
- key__proxy¶
Alias for field number 18
- key__proxy_config¶
Alias for field number 20
- key__proxy_headers¶
Alias for field number 19
- key__socks_options¶
Alias for field number 22
- key_assert_fingerprint¶
Alias for field number 24
- key_assert_hostname¶
Alias for field number 23
- key_block¶
Alias for field number 6
- key_ca_cert_dir¶
Alias for field number 14
- key_ca_certs¶
Alias for field number 12
- key_cert_file¶
Alias for field number 10
- key_cert_reqs¶
Alias for field number 11
- key_headers¶
Alias for field number 17
- key_host¶
Alias for field number 1
- key_key_file¶
Alias for field number 8
- key_key_password¶
Alias for field number 9
- key_maxsize¶
Alias for field number 16
- key_port¶
Alias for field number 2
- key_retries¶
Alias for field number 4
- key_scheme¶
Alias for field number 0
- key_server_hostname¶
Alias for field number 25
- key_socket_options¶
Alias for field number 21
- key_source_address¶
Alias for field number 7
- key_ssl_context¶
Alias for field number 15
- key_ssl_version¶
Alias for field number 13
- key_strict¶
Alias for field number 5
- key_timeout¶
Alias for field number 3