Coverage for requests.sessions : 55%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*-
requests.session ~~~~~~~~~~~~~~~~
This module provides a Session object to manage and persist settings across requests (cookies, auth, proxies).
"""
cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies) TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)
requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies, get_auth_from_url )
# formerly defined here, reexposed here for backward compatibility
""" Determines appropriate setting for a given request, taking into account the explicit setting on that request, and the setting in the session. If a setting is a dictionary, they will be merged together using `dict_class` """
# Bypass if not a dictionary (e.g. verify) isinstance(session_setting, Mapping) and isinstance(request_setting, Mapping) ): return request_setting
# Remove keys that are set to None. del merged_setting[k]
""" Properly merges both requests and session hooks.
This is necessary because when request_hooks == {'response': []}, the merge breaks Session hooks entirely. """
if request_hooks is None or request_hooks.get('response') == []: return session_hooks
return merge_setting(request_hooks, session_hooks, dict_class)
verify=True, cert=None, proxies=None): """Receives a Response. Returns a generator of Responses."""
prepared_request = req.copy()
try: resp.content # Consume socket so it can be released except (ChunkedEncodingError, ContentDecodingError, RuntimeError): resp.raw.read(decode_content=False)
if i >= self.max_redirects: raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)
# Release the connection back into the pool. resp.close()
url = resp.headers['location'] method = req.method
# Handle redirection without scheme (see: RFC 1808 Section 4) if url.startswith('//'): parsed_rurl = urlparse(resp.url) url = '%s:%s' % (parsed_rurl.scheme, url)
# The scheme should be lower case... parsed = urlparse(url) url = parsed.geturl()
# Facilitate non-RFC2616-compliant 'location' headers # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource') # Compliant with RFC3986, we percent encode the url. if not urlparse(url).netloc: url = urljoin(resp.url, requote_uri(url)) else: url = requote_uri(url)
prepared_request.url = to_native_string(url)
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4 if (resp.status_code == codes.see_other and method != 'HEAD'): method = 'GET'
# Do what the browsers do, despite standards... # First, turn 302s into GETs. if resp.status_code == codes.found and method != 'HEAD': method = 'GET'
# Second, if a POST is responded to with a 301, turn it into a GET. # This bizarre behaviour is explained in Issue 1704. if resp.status_code == codes.moved and method == 'POST': method = 'GET'
prepared_request.method = method
# https://github.com/kennethreitz/requests/issues/1084 if resp.status_code not in (codes.temporary, codes.resume): if 'Content-Length' in prepared_request.headers: del prepared_request.headers['Content-Length']
prepared_request.body = None
headers = prepared_request.headers try: del headers['Cookie'] except KeyError: pass
extract_cookies_to_jar(prepared_request._cookies, prepared_request, resp.raw) prepared_request._cookies.update(self.cookies) prepared_request.prepare_cookies(prepared_request._cookies)
# Rebuild auth and proxy information. proxies = self.rebuild_proxies(prepared_request, proxies) self.rebuild_auth(prepared_request, resp)
# Override the original request. req = prepared_request
resp = self.send( req, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies, allow_redirects=False, )
extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)
i += 1 yield resp
""" 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. """ headers = prepared_request.headers url = prepared_request.url
if 'Authorization' in headers: # If we get redirected to a new host, we should strip out any # authentication headers. original_parsed = urlparse(response.request.url) redirect_parsed = urlparse(url)
if (original_parsed.hostname != redirect_parsed.hostname): del headers['Authorization']
# .netrc might have more auth for us on our new host. new_auth = get_netrc_auth(url) if self.trust_env else None if new_auth is not None: prepared_request.prepare_auth(new_auth)
return
""" 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. """ headers = prepared_request.headers url = prepared_request.url scheme = urlparse(url).scheme new_proxies = proxies.copy() if proxies is not None else {}
if self.trust_env and not should_bypass_proxies(url): environ_proxies = get_environ_proxies(url)
proxy = environ_proxies.get(scheme)
if proxy: new_proxies.setdefault(scheme, environ_proxies[scheme])
if 'Proxy-Authorization' in headers: del headers['Proxy-Authorization']
try: username, password = get_auth_from_url(new_proxies[scheme]) except KeyError: username, password = None, None
if username and password: headers['Proxy-Authorization'] = _basic_auth_str(username, password)
return new_proxies
"""A Requests session.
Provides cookie persistence, connection-pooling, and configuration.
Basic Usage::
>>> import requests >>> s = requests.Session() >>> s.get('http://httpbin.org/get') 200 """
'headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks', 'params', 'verify', 'cert', 'prefetch', 'adapters', 'stream', 'trust_env', 'max_redirects']
#: A case-insensitive dictionary of headers to be sent on each #: :class:`Request <Request>` sent from this #: :class:`Session <Session>`.
#: Default Authentication tuple or object to attach to #: :class:`Request <Request>`.
#: Dictionary mapping protocol to the URL of the proxy (e.g. #: {'http': 'foo.bar:3128'}) to be used on each #: :class:`Request <Request>`.
#: Event-handling hooks.
#: Dictionary of querystring data to attach to each #: :class:`Request <Request>`. The dictionary values may be lists for #: representing multivalued query parameters.
#: Stream response content default.
#: SSL Verification default.
#: SSL certificate default.
#: Maximum number of redirects allowed. If the request exceeds this #: limit, a :class:`TooManyRedirects` exception is raised.
#: Should we trust the environment?
#: A CookieJar containing all currently outstanding cookies set on this #: session. By default it is a #: :class:`RequestsCookieJar <requests.cookies.RequestsCookieJar>`, but #: may be any other ``cookielib.CookieJar`` compatible object.
# Default connection adapters.
return self
self.close()
"""Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it. The :class:`PreparedRequest` has settings merged from the :class:`Request <Request>` instance and those of the :class:`Session`.
:param request: :class:`Request` instance to prepare with this session's settings. """
# Bootstrap CookieJar.
# Merge with session cookies merge_cookies(RequestsCookieJar(), self.cookies), cookies)
# Set environment's basic authentication if not explicitly set.
method=request.method.upper(), url=request.url, files=request.files, data=request.data, headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict), params=merge_setting(request.params, self.params), auth=merge_setting(auth, self.auth), cookies=merged_cookies, hooks=merge_hooks(request.hooks, self.hooks), )
params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None): """Constructs a :class:`Request <Request>`, prepares it and sends it. Returns :class:`Response <Response>` object.
:param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload. :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) Float describing the timeout of the request in seconds. :param allow_redirects: (optional) Boolean. Set to True by default. :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. """
# Create the Request. method = method.upper(), url = url, headers = headers, files = files, data = data or {}, params = params or {}, auth = auth, cookies = cookies, hooks = hooks, )
# Gather clues from the surrounding environment. # Set environment's proxies. proxies.setdefault(k, v)
# Look for configuration.
# Curl compatibility.
# Merge all the kwargs.
# Send the request. 'stream': stream, 'timeout': timeout, 'verify': verify, 'cert': cert, 'proxies': proxies, 'allow_redirects': allow_redirects, }
"""Sends a GET request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. """
kwargs.setdefault('allow_redirects', True) return self.request('GET', url, **kwargs)
"""Sends a OPTIONS request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. """
kwargs.setdefault('allow_redirects', True) return self.request('OPTIONS', url, **kwargs)
"""Sends a HEAD request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. """
kwargs.setdefault('allow_redirects', False) return self.request('HEAD', url, **kwargs)
"""Sends a POST request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. """
return self.request('POST', url, data=data, **kwargs)
"""Sends a PUT request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. """
return self.request('PUT', url, data=data, **kwargs)
"""Sends a PATCH request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. """
return self.request('PATCH', url, data=data, **kwargs)
"""Sends a DELETE request. Returns :class:`Response` object.
:param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. """
return self.request('DELETE', url, **kwargs)
"""Send a given PreparedRequest.""" # Set defaults that the hooks can utilize to ensure they always have # the correct parameters to reproduce the previous request.
# It's possible that users might accidentally send a Request object. # Guard against that specific failure case. raise ValueError('You can only send PreparedRequests.')
# Set up variables needed for resolve_redirects and dispatching of hooks
# Get the appropriate adapter to use
# Start time (approximately) of the request
# Send the request
# Total elapsed time of the request (approximately)
# Response manipulation hooks
# Persist cookies
# If the hooks create history then we want those cookies too for resp in r.history: extract_cookies_to_jar(self.cookies, resp.request, resp.raw)
# Redirect resolving generator. stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
# Resolve redirects if allowed.
# Shuffle things around if there's history. # Insert the first (original) request at the start history.insert(0, r) # Get the last request made r = history.pop() r.history = history
"""Returns the appropriate connnection adapter for the given URL."""
# Nothing matches :-/ raise InvalidSchema("No connection adapters were found for '%s'" % url)
"""Closes all adapters and as such the session""" for v in self.adapters.values(): v.close()
"""Registers a connection adapter to a prefix.
Adapters are sorted in descending order by key length."""
self.adapters[key] = self.adapters.pop(key)
return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)
for attr, value in state.items(): setattr(self, attr, value)
"""Returns a :class:`Session` for context-management."""
return Session() |