A connection pool is a container for a collection of connections to a specific host.
If you need to make requests to the same host repeatedly, then you should use a HTTPConnectionPool.
>>> from urllib3 import HTTPConnectionPool
>>> pool = HTTPConnectionPool('ajax.googleapis.com', maxsize=1)
>>> r = pool.request('GET', '/ajax/services/search/web',
... fields={'q': 'urllib3', 'v': '1.0'})
>>> r.status
200
>>> r.headers['content-type']
'text/javascript; charset=utf-8'
>>> 'data: ' + r.data # Content of the response
'data: ...'
>>> r = pool.request('GET', '/ajax/services/search/web',
... fields={'q': 'python', 'v': '1.0'})
>>> 'data: ' + r.data # Content of the response
'data: ...'
>>> pool.num_connections
1
>>> pool.num_requests
2
By default, the pool will cache just one connection. If you’re planning on using such a pool in a multithreaded environment, you should set the maxsize of the pool to a higher number, such as the number of threads. You can also control many other variables like timeout, blocking, and default headers.
A ConnectionPool can be used as a context manager to automatically clear the pool after usage.
>>> from urllib3 import HTTPConnectionPool
>>> with HTTPConnectionPool('ajax.googleapis.com', maxsize=1) as pool:
... r = pool.request('GET', '/ajax/services/search/web',
... fields={'q': 'urllib3', 'v': '1.0'})
... print(pool.pool)
...
<queue.LifoQueue object at 0x7f67367dfcf8>
>>> print(pool.pool)
None
There are various helper functions provided for instantiating these ConnectionPools more easily:
- 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.connectionpool comes with two connection pools:
- class urllib3.connectionpool.HTTPConnectionPool(host, port=None, strict=False, timeout=<object object at 0x7f42c1d23240>, 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.
- ConnectionCls¶
alias of HTTPConnection
- QueueCls¶
alias of LifoQueue
- 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.
- 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 mimick 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, **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, retries=None, redirect=True, assert_same_host=True, timeout=<object object at 0x7f42c1d23440>, 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 urllib3.connectionpool.HTTPSConnectionPool(host, port=None, strict=False, timeout=<object object at 0x7f42c1d23240>, 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, **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 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.
All of these pools inherit from a common base class:
- class urllib3.connectionpool.ConnectionPool(host, port=None)¶
Base class for all connection pools, such as HTTPConnectionPool and HTTPSConnectionPool.
A pool manager is an abstraction for a collection of ConnectionPools.
If you need to make requests to multiple hosts, then you can use a PoolManager, which takes care of maintaining your pools so you don’t have to.
>>> from urllib3 import PoolManager
>>> manager = PoolManager(10)
>>> r = manager.request('GET', 'http://example.com')
>>> r.headers['server']
'ECS (iad/182A)'
>>> r = manager.request('GET', 'http://httpbin.org/')
>>> r.headers['server']
'gunicorn/18.0'
>>> r = manager.request('POST', 'http://httpbin.org/headers')
>>> r = manager.request('HEAD', 'http://httpbin.org/cookies')
>>> len(manager.pools)
2
>>> conn = manager.connection_from_host('httpbin.org')
>>> conn.num_requests
3
The API of a PoolManager object is similar to that of a ConnectionPool, so they can be passed around interchangeably.
The PoolManager uses a Least Recently Used (LRU) policy for discarding old pools. That is, if you set the PoolManager num_pools to 10, then after making requests to 11 or more different hosts, the least recently used pools will be cleaned up eventually.
Cleanup of stale pools does not happen immediately but can be forced when used as a context manager.
>>> from urllib3 import PoolManager
>>> with PoolManager(10) as manager:
... r = manager.request('GET', 'http://example.com')
... r = manager.request('GET', 'http://httpbin.org/')
... len(manager.pools)
...
2
>>> len(manager.pools)
0
You can read more about the implementation and the various adjustable variables within RecentlyUsedContainer.
- class urllib3.poolmanager.PoolManager(num_pools=10, headers=None, **connection_pool_kw)¶
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_host(host, port=None, scheme='http')¶
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.
- connection_from_url(url)¶
Similar to urllib3.connectionpool.connection_from_url() but doesn’t pass any additional parameters to the urllib3.connectionpool.ConnectionPool constructor.
Additional parameters are taken from the PoolManager constructor.
- 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 mimick 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, **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, 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.
ProxyManager is an HTTP proxy-aware subclass of PoolManager. It produces a single HTTPConnectionPool instance for all HTTP connections and individual per-server:port HTTPSConnectionPool instances for tunnelled HTTPS connections.
- class urllib3.poolmanager.ProxyManager(proxy_url, num_pools=10, headers=None, proxy_headers=None, **connection_pool_kw)¶
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 contaning 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
Very important fact: By default, urllib3 does not verify HTTPS requests.
The historic reason for this is that we rely on httplib for some of the HTTP protocol implementation, and httplib does not verify requests out of the box. This is not a good reason, but here we are.
Luckily, it’s not too hard to enable verified HTTPS requests and there are a few ways to do it.
First we need to make sure your Python installation has SSL enabled. Easiest way to check is to simply open a Python shell and type import ssl:
>>> import ssl
Traceback (most recent call last):
...
ImportError: No module named _ssl
If you got an ImportError, then your Python is not compiled with SSL support and you’ll need to re-install it. Read this StackOverflow thread for details.
Otherwise, if ssl imported cleanly, then we’re ready to setup our certificates: Using Certifi with urllib3.
If you’re using Google App Engine, you’ll need to add ssl as a library dependency to your yaml file, like this:
libraries:
- name: ssl
version: latest
If it’s still not working, you may need to enable billing on your account to enable using sockets.
Certifi is a package which ships with Mozilla’s root certificates for easy programmatic access.
Install the Python certifi package:
$ pip install certifi
Setup your pool to require a certificate and provide the certifi bundle:
import urllib3
import certifi
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=certifi.where(), # Path to the Certifi bundle.
)
# You're ready to make verified HTTPS requests.
try:
r = http.request('GET', 'https://example.com/')
except urllib3.exceptions.SSLError as e:
# Handle incorrect certificate error.
...
Make sure to update your certifi package regularly to get the latest root certificates.
Your system’s root certificates may be more up-to-date than maintaining your own, but the trick is finding where they live. Different operating systems have them in different places.
For example, on most Linux distributions they’re at /etc/ssl/certs/ca-certificates.crt. On Windows and OS X? It’s not so simple.
Once you find your root certificate file:
import urllib3
ca_certs = "/etc/ssl/certs/ca-certificates.crt" # Or wherever it lives.
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=ca_certs, # Path to your certificate bundle.
)
# You're ready to make verified HTTPS requests.
try:
r = http.request('GET', 'https://example.com/')
except urllib3.exceptions.SSLError as e:
# Handle incorrect certificate error.
...
By default, we use the standard library’s ssl module. Unfortunately, there are several limitations which are addressed by PyOpenSSL:
To use the Python OpenSSL bindings instead, you’ll need to install the required packages:
$ pip install pyopenssl ndg-httpsclient pyasn1
Once the packages are installed, you can tell urllib3 to switch the ssl backend to PyOpenSSL with inject_into_urllib3():
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
Now you can continue using urllib3 as you normally would.
For more details, check the pyopenssl module.
New in version 1.9.
Unverified HTTPS requests will trigger a warning via Python’s warnings module:
urllib3/connectionpool.py:736: InsecureRequestWarning: Unverified HTTPS
request is being made. Adding certificate verification is strongly advised.
See: https://urllib3.readthedocs.org/en/latest/security.html
This would be a great time to enable HTTPS verification: Using Certifi with urllib3.
If you know what you’re doing and would like to disable this and other warnings, you can use disable_warnings():
import urllib3
urllib3.disable_warnings()
Making unverified HTTPS requests is strongly discouraged. ˙ ͜ʟ˙
Alternatively, if you are using Python’s logging module, you can capture the warnings to your own log:
logging.captureWarnings(True)
Capturing the warnings to your own log is much preferred over simply disabling the warnings.
New in version 1.11.
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
If you encounter this warning, it is strongly recommended you upgrade to a newer Python version, or that you use pyOpenSSL as described in the OpenSSL / PyOpenSSL section.
If you know what you are doing and would like to disable this and other warnings, please consult the InsecureRequestWarning section for instructions on how to handle the warnings.
Useful methods for working with httplib, completely decoupled from code specific to urllib3.
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: |
|
---|
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.
A sentinel object representing the default timeout value
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 |
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 |
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 |
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. |
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 the timeout clock, used during a connect() attempt
Raises urllib3.exceptions.TimeoutStateError: | |
---|---|
if you attempt to start a timer that has been started already. |
Retrieve the current time. This function is mocked out in unit testing.
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: |
|
---|
Maximum backoff time.
Backwards-compatibility for the old retries format.
Formula for computing the current backoff
Return type: | float |
---|
Return a new Retry object with incremented retry counters.
Parameters: |
|
---|---|
Returns: | A new Retry object. |
Are we out of retries?
Is this method/status code retryable? (Based on method/codes whitelists)
Sleep between retry attempts using an exponential backoff.
By default, the backoff factor is 0 and this method will return immediately.
Datastructure for representing an HTTP URL. Used as a return value for parse_url().
For backwards-compatibility with urlparse. We’re nice like that.
Network location including host and port
Absolute path including the query string.
Convert self into a url
This function should more or less round-trip with parse_url(). The returned url may not be exactly the same as the url inputted to parse_url(), but it should be equivalent by the RFC (e.g., urls with a blank port will have : removed).
Example:
>>> U = parse_url('http://google.com/mail/')
>>> U.url
'http://google.com/mail/'
>>> Url('http', 'username:password', 'host.com', 80,
... '/path', 'query', 'fragment').url
'http://username:password@host.com:80/path?query#fragment'
Deprecated. Use parse_url() instead.
Given a url, return a parsed Url namedtuple. Best-effort is performed to parse incomplete urls. Fields not provided will be None.
Partly backwards-compatible with urlparse.
Example:
>>> parse_url('http://google.com/mail/')
Url(scheme='http', host='google.com', port=None, path='/mail/', ...)
>>> parse_url('google.com:80')
Url(scheme=None, host='google.com', port=80, path=None, ...)
>>> parse_url('/foo?bar')
Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...)
Given a string and an iterable of delimiters, split on the first found delimiter. Return two split parts and the matched delimiter.
If not found, then the first part is the full input string.
Example:
>>> split_first('foo/bar?baz', '?/=')
('foo', 'bar?baz', '/')
>>> split_first('foo/bar?baz', '123')
('foo/bar?baz', '', None)
Scales linearly with number of delims. Not ideal for large number of delims.
Our embarassingly-simple replacement for mimetools.choose_boundary.
Encode a dictionary of fields using the multipart/form-data MIME format.
Parameters: |
|
---|
Iterate over fields.
Supports list of (k, v) tuples and dicts, and lists of RequestField.
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.
A data container for request body parameters.
Parameters: |
|
---|
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.
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: |
|
---|
Renders the headers for this request field.
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: |
|
---|
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. |
---|
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().
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 mimick 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.
Make a request using urlopen() with the fields encoded in the url. This is useful for request methods like GET, HEAD, DELETE, etc.
Shortcuts for generating request headers.
Parameters: |
|
---|
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'}
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: |
|
---|
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.
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. |
---|
Similar to httplib.HTTPResponse.read(), but with two additional parameters: decode_content and cache_content.
Parameters: |
|
---|
Similar to HTTPResponse.read(), but with an additional parameter: decode_content.
Parameters: | decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header. |
---|
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: |
|
---|
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).
Checks if given fingerprint matches the supplied certificate.
Parameters: |
|
---|
All arguments have the same meaning as ssl_wrap_socket.
By default, this function does a lot of the same work that ssl.create_default_context does on Python 3.4+. It:
If you wish to enable SSLv3, you can do:
from urllib3.util import ssl_
context = ssl_.create_urllib3_context()
context.options &= ~ssl_.OP_NO_SSLv3
You can do the same to enable compression (substituting COMPRESSION for SSLv3 in the last line above).
Parameters: |
|
---|---|
Returns: | Constructed SSLContext object with specified options |
Return type: | SSLContext |
Resolves the argument to a numeric constant, which can be passed to the wrap_socket function/method from the ssl module. Defaults to ssl.CERT_NONE. If given a string it is assumed to be the name of the constant in the ssl module or its abbrevation. (So you can specify REQUIRED instead of CERT_REQUIRED. If it’s neither None nor a string we assume it is already the numeric constant which can directly be passed to wrap_socket.
like resolve_cert_reqs
All arguments except for server_hostname and ssl_context have the same meaning as they do when using ssl.wrap_socket().
Parameters: |
|
---|
These datastructures are used to implement the behaviour of various urllib3 components in a decoupled and application-agnostic design.
Provides a thread-safe dict-like container which maintains up to maxsize keys while throwing away the least-recently-used keys beyond maxsize.
Parameters: |
|
---|
alias of OrderedDict
Parameters: |
|
---|
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'
Adds a (name, value) pair, doesn’t overwrite the value if it already exists.
>>> headers = HTTPHeaderDict(foo='bar')
>>> headers.add('Foo', 'baz')
>>> headers['foo']
'bar, baz'
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__
Read headers from a Python 2 httplib message object.
Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.
Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.
Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.
Returns a list of all the values for the named field. Returns an empty list if the key doesn’t exist.
Iterate over all header lines, including duplicate ones.
Iterate over all headers, merging duplicate ones together.
If key is not found, d is returned if given, otherwise KeyError is raised.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
These modules implement various extra features, that may not be ready for prime time.
SSL with SNI-support for Python 2. Follow these instructions if you would like to verify SSL certificates in Python 2. Note, the default libraries do not do certificate checking; you need to do additional work to validate certificates yourself.
This needs the following packages installed:
You can install them with the following command:
pip install pyopenssl ndg-httpsclient pyasn1
To activate certificate checking, call inject_into_urllib3() from your Python code before you begin making HTTP requests. This can be done in a sitecustomize module, or at any other time before your application begins using urllib3, like this:
try:
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
except ImportError:
pass
Now you can use urllib3 as you normally would, and it will support SNI when the required modules are installed.
Activating this module also has the positive side effect of disabling SSL/TLS compression in Python 2 (see CRIME attack).
If you want to configure the default list of supported cipher suites, you can set the urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST variable.
var DEFAULT_SSL_CIPHER_LIST: | |
---|---|
The list of supported SSL/TLS cipher suites. |
Very important fact: By default, urllib3 does not verify HTTPS requests.
The historic reason for this is that we rely on httplib for some of the HTTP protocol implementation, and httplib does not verify requests out of the box. This is not a good reason, but here we are.
Luckily, it’s not too hard to enable verified HTTPS requests and there are a few ways to do it.
First we need to make sure your Python installation has SSL enabled. Easiest way to check is to simply open a Python shell and type import ssl:
>>> import ssl
Traceback (most recent call last):
...
ImportError: No module named _ssl
If you got an ImportError, then your Python is not compiled with SSL support and you’ll need to re-install it. Read this StackOverflow thread for details.
Otherwise, if ssl imported cleanly, then we’re ready to setup our certificates: Using Certifi with urllib3.
If you’re using Google App Engine, you’ll need to add ssl as a library dependency to your yaml file, like this:
libraries:
- name: ssl
version: latest
If it’s still not working, you may need to enable billing on your account to enable using sockets.
Certifi is a package which ships with Mozilla’s root certificates for easy programmatic access.
Install the Python certifi package:
$ pip install certifi
Setup your pool to require a certificate and provide the certifi bundle:
import urllib3
import certifi
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=certifi.where(), # Path to the Certifi bundle.
)
# You're ready to make verified HTTPS requests.
try:
r = http.request('GET', 'https://example.com/')
except urllib3.exceptions.SSLError as e:
# Handle incorrect certificate error.
...
Make sure to update your certifi package regularly to get the latest root certificates.
Your system’s root certificates may be more up-to-date than maintaining your own, but the trick is finding where they live. Different operating systems have them in different places.
For example, on most Linux distributions they’re at /etc/ssl/certs/ca-certificates.crt. On Windows and OS X? It’s not so simple.
Once you find your root certificate file:
import urllib3
ca_certs = "/etc/ssl/certs/ca-certificates.crt" # Or wherever it lives.
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED', # Force certificate check.
ca_certs=ca_certs, # Path to your certificate bundle.
)
# You're ready to make verified HTTPS requests.
try:
r = http.request('GET', 'https://example.com/')
except urllib3.exceptions.SSLError as e:
# Handle incorrect certificate error.
...
By default, we use the standard library’s ssl module. Unfortunately, there are several limitations which are addressed by PyOpenSSL:
To use the Python OpenSSL bindings instead, you’ll need to install the required packages:
$ pip install pyopenssl ndg-httpsclient pyasn1
Once the packages are installed, you can tell urllib3 to switch the ssl backend to PyOpenSSL with inject_into_urllib3():
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
Now you can continue using urllib3 as you normally would.
For more details, check the pyopenssl module.
New in version 1.9.
Unverified HTTPS requests will trigger a warning via Python’s warnings module:
urllib3/connectionpool.py:736: InsecureRequestWarning: Unverified HTTPS
request is being made. Adding certificate verification is strongly advised.
See: https://urllib3.readthedocs.org/en/latest/security.html
This would be a great time to enable HTTPS verification: Using Certifi with urllib3.
If you know what you’re doing and would like to disable this and other warnings, you can use disable_warnings():
import urllib3
urllib3.disable_warnings()
Making unverified HTTPS requests is strongly discouraged. ˙ ͜ʟ˙
Alternatively, if you are using Python’s logging module, you can capture the warnings to your own log:
logging.captureWarnings(True)
Capturing the warnings to your own log is much preferred over simply disabling the warnings.
New in version 1.11.
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
If you encounter this warning, it is strongly recommended you upgrade to a newer Python version, or that you use pyOpenSSL as described in the OpenSSL / PyOpenSSL section.
If you know what you are doing and would like to disable this and other warnings, please consult the InsecureRequestWarning section for instructions on how to handle the warnings.
pip install urllib3 or fetch the latest source from github.com/shazow/urllib3.
>>> import urllib3
>>> http = urllib3.PoolManager()
>>> r = http.request('GET', 'http://example.com/')
>>> r.status
200
>>> r.headers['server']
'ECS (iad/182A)'
>>> 'data: ' + r.data
'data: ...'
By default, urllib3 does not verify your HTTPS requests. You’ll need to supply a root certificate bundle, or use certifi
>>> import urllib3, certifi
>>> http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
>>> r = http.request('GET', 'https://insecure.com/')
Traceback (most recent call last):
...
SSLError: hostname 'insecure.com' doesn't match 'svn.nmap.org'
For more on making secure SSL/TLS HTTPS requests, read the Security section.
urllib3’s responses respect the io framework from Python’s standard library, allowing use of these standard objects for purposes like buffering:
>>> http = urllib3.PoolManager()
>>> r = http.urlopen('GET','http://example.com/', preload_content=False)
>>> b = io.BufferedReader(r, 2048)
>>> firstpart = b.read(100)
>>> # ... your internet connection fails momentarily ...
>>> secondpart = b.read()
urllib3 tries to strike a fine balance between power, extendability, and sanity. To achieve this, the codebase is a collection of small reusable utilities and abstractions composed together in a few helpful layers.
The highest level is the PoolManager(...).
The PoolManager will take care of reusing connections for you whenever you request the same host. This should cover most scenarios without significant loss of efficiency, but you can always drop down to a lower level component for more granular control.
>>> import urllib3
>>> http = urllib3.PoolManager(10)
>>> r1 = http.request('GET', 'http://example.com/')
>>> r2 = http.request('GET', 'http://httpbin.org/')
>>> r3 = http.request('GET', 'http://httpbin.org/get')
>>> len(http.pools)
2
A PoolManager is a proxy for a collection of ConnectionPool objects. They both inherit from RequestMethods to make sure that their API is similar, so that instances of either can be passed around interchangeably.
The ProxyManager is an HTTP proxy-aware subclass of PoolManager. It produces a single HTTPConnectionPool instance for all HTTP connections and individual per-server:port HTTPSConnectionPool instances for tunnelled HTTPS connections:
>>> 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
The next layer is the ConnectionPool(...).
The HTTPConnectionPool and HTTPSConnectionPool classes allow you to define a pool of connections to a single host and make requests against this pool with automatic connection reusing and thread safety.
When the ssl module is available, then HTTPSConnectionPool objects can be configured to check SSL certificates against specific provided certificate authorities.
>>> import urllib3
>>> conn = urllib3.connection_from_url('http://httpbin.org/')
>>> r1 = conn.request('GET', 'http://httpbin.org/')
>>> r2 = conn.request('GET', '/user-agent')
>>> r3 = conn.request('GET', 'http://example.com')
Traceback (most recent call last):
...
urllib3.exceptions.HostChangedError: HTTPConnectionPool(host='httpbin.org', port=None): Tried to open a foreign host with url: http://example.com
Again, a ConnectionPool is a pool of connections to a specific host. Trying to access a different host through the same pool will raise a HostChangedError exception unless you specify assert_same_host=False. Do this at your own risk as the outcome is completely dependent on the behaviour of the host server.
If you need to access multiple hosts and don’t want to manage your own collection of ConnectionPool objects, then you should use a PoolManager.
A ConnectionPool is composed of a collection of httplib.HTTPConnection objects.
A timeout can be set to abort socket operations on individual connections after the specified duration. The timeout can be defined as a float or an instance of Timeout which gives more granular configuration over how much time is allowed for different stages of the request. This can be set for the entire pool or per-request.
>>> from urllib3 import PoolManager, Timeout
>>> # Manager with 3 seconds combined timeout.
>>> http = PoolManager(timeout=3.0)
>>> r = http.request('GET', 'http://httpbin.org/delay/1')
>>> # Manager with 2 second timeout for the read phase, no limit for the rest.
>>> http = PoolManager(timeout=Timeout(read=2.0))
>>> r = http.request('GET', 'http://httpbin.org/delay/1')
>>> # Manager with no timeout but a request with a timeout of 1 seconds for
>>> # the connect phase and 2 seconds for the read phase.
>>> http = PoolManager()
>>> r = http.request('GET', 'http://httpbin.org/delay/1', timeout=Timeout(connect=1.0, read=2.0))
>>> # Same Manager but request with a 5 second total timeout.
>>> r = http.request('GET', 'http://httpbin.org/delay/1', timeout=Timeout(total=5.0))
See the Timeout definition for more details.
Retries can be configured by passing an instance of Retry, or disabled by passing False, to the retries parameter.
Redirects are also considered to be a subset of retries but can be configured or disabled individually.
>>> from urllib3 import PoolManager, Retry
>>> # Allow 3 retries total for all requests in this pool. These are the same:
>>> http = PoolManager(retries=3)
>>> http = PoolManager(retries=Retry(3))
>>> http = PoolManager(retries=Retry(total=3))
>>> r = http.request('GET', 'http://httpbin.org/redirect/2')
>>> # r.status -> 200
>>> # Disable redirects for this request.
>>> r = http.request('GET', 'http://httpbin.org/redirect/2', retries=Retry(3, redirect=False))
>>> # r.status -> 302
>>> # No total limit, but only do 5 connect retries, for this request.
>>> r = http.request('GET', 'http://httpbin.org/', retries=Retry(connect=5))
See the Retry definition for more details.
You may also stream your response and get data as they come (e.g. when using transfer-encoding: chunked). In this case, method stream() will return generator.
>>> from urllib3 import PoolManager
>>> http = urllib3.PoolManager()
>>> r = http.request("GET", "http://httpbin.org/stream/3")
>>> r.getheader("transfer-encoding")
'chunked'
>>> for chunk in r.stream():
... print chunk
{"url": "http://httpbin.org/stream/3", ..., "id": 0, ...}
{"url": "http://httpbin.org/stream/3", ..., "id": 1, ...}
{"url": "http://httpbin.org/stream/3", ..., "id": 2, ...}
>>> r.closed
True
Completely consuming the stream will auto-close the response and release the connection back to the pool. If you’re only partially consuming the consuming a stream, make sure to manually call r.close() on the response.
At the very core, just like its predecessors, urllib3 is built on top of httplib – the lowest level HTTP library included in the Python standard library.
To aid the limited functionality of the httplib module, urllib3 provides various helper methods which are used with the higher level components but can also be used independently.
Useful methods for working with httplib, completely decoupled from code specific to urllib3.
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: |
|
---|
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.
A sentinel object representing the default timeout value
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 |
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 |
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 |
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. |
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 the timeout clock, used during a connect() attempt
Raises urllib3.exceptions.TimeoutStateError: | |
---|---|
if you attempt to start a timer that has been started already. |
Retrieve the current time. This function is mocked out in unit testing.
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: |
|
---|
Maximum backoff time.
Backwards-compatibility for the old retries format.
Formula for computing the current backoff
Return type: | float |
---|
Return a new Retry object with incremented retry counters.
Parameters: |
|
---|---|
Returns: | A new Retry object. |
Are we out of retries?
Is this method/status code retryable? (Based on method/codes whitelists)
Sleep between retry attempts using an exponential backoff.
By default, the backoff factor is 0 and this method will return immediately.
Datastructure for representing an HTTP URL. Used as a return value for parse_url().
For backwards-compatibility with urlparse. We’re nice like that.
Network location including host and port
Absolute path including the query string.
Convert self into a url
This function should more or less round-trip with parse_url(). The returned url may not be exactly the same as the url inputted to parse_url(), but it should be equivalent by the RFC (e.g., urls with a blank port will have : removed).
Example:
>>> U = parse_url('http://google.com/mail/')
>>> U.url
'http://google.com/mail/'
>>> Url('http', 'username:password', 'host.com', 80,
... '/path', 'query', 'fragment').url
'http://username:password@host.com:80/path?query#fragment'
Deprecated. Use parse_url() instead.
Given a url, return a parsed Url namedtuple. Best-effort is performed to parse incomplete urls. Fields not provided will be None.
Partly backwards-compatible with urlparse.
Example:
>>> parse_url('http://google.com/mail/')
Url(scheme='http', host='google.com', port=None, path='/mail/', ...)
>>> parse_url('google.com:80')
Url(scheme=None, host='google.com', port=80, path=None, ...)
>>> parse_url('/foo?bar')
Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...)
Given a string and an iterable of delimiters, split on the first found delimiter. Return two split parts and the matched delimiter.
If not found, then the first part is the full input string.
Example:
>>> split_first('foo/bar?baz', '?/=')
('foo', 'bar?baz', '/')
>>> split_first('foo/bar?baz', '123')
('foo/bar?baz', '', None)
Scales linearly with number of delims. Not ideal for large number of delims.
Our embarassingly-simple replacement for mimetools.choose_boundary.
Encode a dictionary of fields using the multipart/form-data MIME format.
Parameters: |
|
---|
Iterate over fields.
Supports list of (k, v) tuples and dicts, and lists of RequestField.
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.
A data container for request body parameters.
Parameters: |
|
---|
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.
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: |
|
---|
Renders the headers for this request field.
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: |
|
---|
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. |
---|
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().
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 mimick 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.
Make a request using urlopen() with the fields encoded in the url. This is useful for request methods like GET, HEAD, DELETE, etc.
Shortcuts for generating request headers.
Parameters: |
|
---|
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'}
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: |
|
---|
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.
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. |
---|
Similar to httplib.HTTPResponse.read(), but with two additional parameters: decode_content and cache_content.
Parameters: |
|
---|
Similar to HTTPResponse.read(), but with an additional parameter: decode_content.
Parameters: | decode_content – If True, will attempt to decode the body based on the ‘content-encoding’ header. |
---|
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: |
|
---|
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).
Checks if given fingerprint matches the supplied certificate.
Parameters: |
|
---|
All arguments have the same meaning as ssl_wrap_socket.
By default, this function does a lot of the same work that ssl.create_default_context does on Python 3.4+. It:
If you wish to enable SSLv3, you can do:
from urllib3.util import ssl_
context = ssl_.create_urllib3_context()
context.options &= ~ssl_.OP_NO_SSLv3
You can do the same to enable compression (substituting COMPRESSION for SSLv3 in the last line above).
Parameters: |
|
---|---|
Returns: | Constructed SSLContext object with specified options |
Return type: | SSLContext |
Resolves the argument to a numeric constant, which can be passed to the wrap_socket function/method from the ssl module. Defaults to ssl.CERT_NONE. If given a string it is assumed to be the name of the constant in the ssl module or its abbrevation. (So you can specify REQUIRED instead of CERT_REQUIRED. If it’s neither None nor a string we assume it is already the numeric constant which can directly be passed to wrap_socket.
like resolve_cert_reqs
All arguments except for server_hostname and ssl_context have the same meaning as they do when using ssl.wrap_socket().
Parameters: |
|
---|
Custom exceptions defined by urllib3
Raised when a request enters a pool after the pool has been closed.
Raised when a socket timeout occurs while connecting to a server
Renamed to ProtocolError but aliased for backwards compatibility.
alias of ProtocolError
Raised when automatic decoding based on Content-Type fails.
Raised when a pool runs out of connections and no more are allowed.
Base exception used by this module.
Base warning used by this module.
Raised when an existing pool gets a request for a foreign host.
Warned when certain SSL configuration is not available on a platform.
Warned when making an unverified HTTPS request.
Raised when get_host or similar fails to parse the URL input.
Raised when there is something wrong with a given URL input.
Raised when the maximum number of retries is exceeded.
Parameters: |
|
---|
Base exception for errors caused within a pool.
Raised when something unexpected happens mid-request/response.
Raised when the connection to a proxy fails.
Raised when a socket timeout occurs while receiving data from a server
Base exception for PoolErrors that have associated URLs.
Used as a container for an error reason supplied in a MaxRetryError.
Response needs to be chunked in order to read it as chunks.
Raised when SSL certificate fails in an HTTPS connection.
Warned when perfoming security reducing actions
Warned when system time is suspected to be wrong
Raised when a socket timeout error occurs.
Catching this error will catch both ReadTimeoutErrors and ConnectTimeoutErrors.
Raised when passing an invalid state to a timeout
These modules implement various extra features, that may not be ready for prime time.
These modules implement various extra features, that may not be ready for prime time.
SSL with SNI-support for Python 2. Follow these instructions if you would like to verify SSL certificates in Python 2. Note, the default libraries do not do certificate checking; you need to do additional work to validate certificates yourself.
This needs the following packages installed:
You can install them with the following command:
pip install pyopenssl ndg-httpsclient pyasn1
To activate certificate checking, call inject_into_urllib3() from your Python code before you begin making HTTP requests. This can be done in a sitecustomize module, or at any other time before your application begins using urllib3, like this:
try:
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
except ImportError:
pass
Now you can use urllib3 as you normally would, and it will support SNI when the required modules are installed.
Activating this module also has the positive side effect of disabling SSL/TLS compression in Python 2 (see CRIME attack).
If you want to configure the default list of supported cipher suites, you can set the urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST variable.
var DEFAULT_SSL_CIPHER_LIST: | |
---|---|
The list of supported SSL/TLS cipher suites. |
Please consider sponsoring urllib3 development, especially if your company benefits from this library.
Project Grant: A grant for contiguous full-time development has the biggest impact for progress. Periods of 3 to 10 days allow a contributor to tackle substantial complex issues which are otherwise left to linger until somebody can’t afford to not fix them.
Contact @shazow to arrange a grant for a core contributor.
One-off: Development will continue regardless of funding, but donations help move things further along quicker as the maintainer can allocate more time off to work on urllib3 specifically.
Sponsor with Credit Card Sponsor with BitcoinRecurring: You’re welcome to support the maintainer on Gittip.