Session#

Main classes to add caching features to requests.Session

CachedSession

Session class that extends requests.Session with caching features.

CacheMixin

Mixin class that extends requests.Session with caching features.

class requests_cache.session.CachedSession(cache_name='http_cache', backend=None, expire_after=- 1, urls_expire_after=None, cache_control=False, allowable_codes=(200,), allowable_methods=('GET', 'HEAD'), filter_fn=None, stale_if_error=False, **kwargs)[source]#

Bases: requests_cache.session.CacheMixin, requests.sessions.Session

Session class that extends requests.Session with caching features.

See individual backend classes for additional backend-specific arguments. Also see User Guide for more details and examples on how the following arguments affect cache behavior.

Parameters
  • cache_name (str) – Cache prefix or namespace, depending on backend

  • backend (Union[str, BaseCache, Type[BaseCache], None]) – Cache backend name or instance; name may be one of ['sqlite', 'filesystem', 'mongodb', 'gridfs', 'redis', 'dynamodb', 'memory']

  • serializer – Serializer name or instance; name may be one of ['pickle', 'json', 'yaml', 'bson'].

  • expire_after (Union[None, int, float, str, datetime, timedelta]) – Time after which cached items will expire

  • urls_expire_after (Optional[Dict[str, Union[None, int, float, str, datetime, timedelta]]]) – Expiration times to apply for different URL patterns

  • cache_control (bool) – Use Cache-Control headers to set expiration

  • allowable_codes (Iterable[int]) – Only cache responses with one of these status codes

  • allowable_methods (Iterable[str]) – Cache only responses for one of these HTTP methods

  • match_headers – Match request headers when reading from the cache; may be either a boolean or a list of specific headers to match

  • ignored_parameters – List of request parameters to not match against, and exclude from the cache

  • filter_fn (Optional[Callable[[Union[Response, CachedResponse]], bool]]) – Function that takes a Response object and returns a boolean indicating whether or not that response should be cached. Will be applied to both new and previously cached responses.

  • key_fn – Function for generating custom cache keys based on request info

  • stale_if_error (bool) – Return stale cache data if a new request raises an exception

auth#

Default Authentication tuple or object to attach to Request.

cache_disabled()#

Context manager for temporary disabling the cache

Warning

This method is not thread-safe.

Example

>>> s = CachedSession()
>>> with s.cache_disabled():
...     s.get('http://httpbin.org/ip')
cert#

SSL client certificate default, if String, path to ssl client cert file (.pem). If Tuple, (‘cert’, ‘key’) pair.

close()#

Closes all adapters and as such the session

cookies#

A CookieJar containing all currently outstanding cookies set on this session. By default it is a RequestsCookieJar, but may be any other cookielib.CookieJar compatible object.

delete(url, **kwargs)#

Sends a DELETE request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

get(url, **kwargs)#

Sends a GET request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

get_adapter(url)#

Returns the appropriate connection adapter for the given URL.

Return type

requests.adapters.BaseAdapter

get_redirect_target(resp)#

Receives a Response. Returns a redirect URI or None

head(url, **kwargs)#

Sends a HEAD request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

headers#

A case-insensitive dictionary of headers to be sent on each Request sent from this Session.

hooks#

Event-handling hooks.

max_redirects#

Maximum number of redirects allowed. If the request exceeds this limit, a TooManyRedirects exception is raised. This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is 30.

merge_environment_settings(url, proxies, stream, verify, cert)#

Check the environment and merge it with some settings.

Return type

dict

mount(prefix, adapter)#

Registers a connection adapter to a prefix.

Adapters are sorted in descending order by prefix length.

options(url, **kwargs)#

Sends a OPTIONS request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

params#

Dictionary of querystring data to attach to each Request. The dictionary values may be lists for representing multivalued query parameters.

patch(url, data=None, **kwargs)#

Sends a PATCH request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

post(url, data=None, json=None, **kwargs)#

Sends a POST request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • json – (optional) json to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

prepare_request(request)#

Constructs a PreparedRequest for transmission and returns it. The PreparedRequest has settings merged from the Request instance and those of the Session.

Parameters

requestRequest instance to prepare with this session’s settings.

Return type

requests.PreparedRequest

proxies#

Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {‘http’: ‘foo.bar:3128’, ‘http://host.name’: ‘foo.bar:4012’}) to be used on each Request.

put(url, data=None, **kwargs)#

Sends a PUT request. Returns Response object.

Parameters
  • url – URL for the new Request object.

  • data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.

  • **kwargs – Optional arguments that request takes.

Return type

requests.Response

rebuild_auth(prepared_request, response)#

When being redirected we may want to strip authentication from the request to avoid leaking credentials. This method intelligently removes and reapplies authentication where possible to avoid credential loss.

rebuild_method(prepared_request, response)#

When being redirected we may want to change the method of the request based on certain specs or browser behavior.

rebuild_proxies(prepared_request, proxies)#

This method re-evaluates the proxy configuration by considering the environment variables. If we are redirected to a URL covered by NO_PROXY, we strip the proxy configuration. Otherwise, we set missing proxy keys for this URL (in case they were stripped by a previous redirect).

This method also replaces the Proxy-Authorization header where necessary.

Return type

dict

request(method, url, *args, expire_after=None, **kwargs)#

This method prepares and sends a request while automatically performing any necessary caching operations. This will be called by any other method-specific requests functions (get, post, etc.). This does not include prepared requests, which will still be cached via send().

See requests.Session.request() for parameters. Additional parameters:

Parameters

expire_after (Union[None, int, float, str, datetime, timedelta]) – Expiration time to set only for this request; see details below. Overrides CachedSession.expire_after. Accepts all the same values as CachedSession.expire_after. Use -1 to disable expiration.

Return type

Union[Response, CachedResponse]

Returns

Either a new or cached response

Order of operations: For reference, a request will pass through the following methods:

  1. requests.get()/requests.Session.get() or other method-specific functions (optional)

  2. CachedSession.request()

  3. requests.Session.request()

  4. CachedSession.send()

  5. BaseCache.get_response()

  6. requests.Session.send() (if not previously cached)

  7. BaseCache.save_response() (if not previously cached)

Parameters
  • method (str) –

  • url (str) –

resolve_redirects(resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs)#

Receives a Response. Returns a generator of Responses or Requests.

send(request, expire_after=None, **kwargs)#

Send a prepared request, with caching. See request() for notes on behavior, and see requests.Session.send() for parameters. Additional parameters:

Parameters
Return type

Union[Response, CachedResponse]

should_strip_auth(old_url, new_url)#

Decide whether Authorization header should be removed when redirecting

stream#

Stream response content default.

trust_env#

Trust environment settings for proxy configuration, default authentication and similar.

verify#

SSL Verification default. Defaults to True, requiring requests to verify the TLS certificate at the remote end. If verify is set to False, requests will accept any TLS certificate presented by the server, and will ignore hostname mismatches and/or expired certificates, which will make your application vulnerable to man-in-the-middle (MitM) attacks. Only set this to False for testing.

class requests_cache.session.CacheMixin(cache_name='http_cache', backend=None, expire_after=- 1, urls_expire_after=None, cache_control=False, allowable_codes=(200,), allowable_methods=('GET', 'HEAD'), filter_fn=None, stale_if_error=False, **kwargs)[source]#

Mixin class that extends requests.Session with caching features. See CachedSession for usage details.

Parameters