5This module provides a Session object to manage and persist settings across
6requests (cookies, auth, proxies).
11from collections
import OrderedDict
12from datetime
import timedelta
14from ._internal_utils
import to_native_string
15from .adapters
import HTTPAdapter
16from .auth
import _basic_auth_str
17from .compat
import Mapping, cookielib, urljoin, urlparse
21 extract_cookies_to_jar,
24from .exceptions
import (
30from .hooks
import default_hooks, dispatch_hook
34 DEFAULT_REDIRECT_LIMIT,
39from .status_codes
import codes
40from .structures
import CaseInsensitiveDict
50 should_bypass_proxies,
61def merge_setting(request_setting, session_setting, dict_class=OrderedDict):
62 """Determines appropriate setting for a given request, taking into account
63 the explicit setting on that request, and the setting in the session. If a
64 setting is a dictionary, they will be merged together using `dict_class`
67 if session_setting
is None:
68 return request_setting
70 if request_setting
is None:
71 return session_setting
77 return request_setting
79 merged_setting =
dict_class(to_key_val_list(session_setting))
86 del merged_setting[key]
91def merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict):
92 """Properly merges both requests and session hooks.
94 This is necessary because when request_hooks == {'response': []}, the
95 merge breaks Session hooks entirely.
103 return merge_setting(request_hooks, session_hooks, dict_class)
108 """Receives a Response. Returns a redirect URI or ``None``"""
124 return to_native_string(location,
"utf8")
128 """Decide whether Authorization header should be removed when redirecting"""
129 old_parsed = urlparse(old_url)
130 new_parsed = urlparse(new_url)
157 return changed_port
or changed_scheme
168 yield_requests=False,
171 """Receives a Response. Returns a generator of Responses or Requests."""
176 previous_fragment = urlparse(
req.url).fragment
187 except (ChunkedEncodingError, ContentDecodingError, RuntimeError):
192 f
"Exceeded {self.max_redirects} redirects.", response=resp
204 parsed = urlparse(url)
215 url = urljoin(
resp.url, requote_uri(url))
217 url = requote_uri(url)
229 purged_headers = (
"Content-Length",
"Content-Type",
"Transfer-Encoding")
230 for header
in purged_headers:
252 "Content-Length" in headers
or "Transfer-Encoding" in headers
257 rewind_body(prepared_request)
260 req = prepared_request
273 allow_redirects=
False,
284 """When being redirected we may want to strip authentication from the
285 request to avoid leaking credentials. This method intelligently removes
286 and reapplies authentication where possible to avoid credential loss.
296 del headers[
"Authorization"]
299 new_auth = get_netrc_auth(url)
if self.trust_env
else None
300 if new_auth
is not None:
304 """This method re-evaluates the proxy configuration by considering the
305 environment variables. If we are redirected to a URL covered by
306 NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
307 proxy keys for this URL (in case they were stripped by a previous
310 This method also replaces the Proxy-Authorization header where
317 new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env)
319 if "Proxy-Authorization" in headers:
320 del headers[
"Proxy-Authorization"]
323 username, password = get_auth_from_url(new_proxies[scheme])
325 username, password =
None,
None
330 headers[
"Proxy-Authorization"] = _basic_auth_str(username, password)
335 """When being redirected we may want to change the method of the request
336 based on certain specs or browser behavior.
358 """A Requests session.
360 Provides cookie persistence, connection-pooling, and configuration.
365 >>> s = requests.Session()
366 >>> s.get('https://httpbin.org/get')
369 Or as a context manager::
371 >>> with requests.Session() as s:
372 ... s.get('https://httpbin.org/get')
460 """Constructs a :class:`PreparedRequest <PreparedRequest>` for
461 transmission and returns it. The :class:`PreparedRequest` has settings
462 merged from the :class:`Request <Request>` instance and those of the
465 :param request: :class:`Request` instance to prepare with this
467 :rtype: requests.PreparedRequest
473 cookies = cookiejar_from_dict(cookies)
476 merged_cookies = merge_cookies(
497 cookies=merged_cookies,
513 allow_redirects=True,
521 """Constructs a :class:`Request <Request>`, prepares it and sends it.
522 Returns :class:`Response <Response>` object.
524 :param method: method for the new :class:`Request` object.
525 :param url: URL for the new :class:`Request` object.
526 :param params: (optional) Dictionary or bytes to be sent in the query
527 string for the :class:`Request`.
528 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
529 object to send in the body of the :class:`Request`.
530 :param json: (optional) json to send in the body of the
532 :param headers: (optional) Dictionary of HTTP Headers to send with the
534 :param cookies: (optional) Dict or CookieJar object to send with the
536 :param files: (optional) Dictionary of ``'filename': file-like-objects``
537 for multipart encoding upload.
538 :param auth: (optional) Auth tuple or callable to enable
539 Basic/Digest/Custom HTTP Auth.
540 :param timeout: (optional) How long to wait for the server to send
541 data before giving up, as a float, or a :ref:`(connect timeout,
542 read timeout) <timeouts>` tuple.
543 :type timeout: float or tuple
544 :param allow_redirects: (optional) Set to True by default.
545 :type allow_redirects: bool
546 :param proxies: (optional) Dictionary mapping protocol or protocol and
547 hostname to the URL of the proxy.
548 :param stream: (optional) whether to immediately download the response
549 content. Defaults to ``False``.
550 :param verify: (optional) Either a boolean, in which case it controls whether we verify
551 the server's TLS certificate, or a string, in which case it must be a path
552 to a CA bundle to use. Defaults to ``True``. When set to
553 ``False``, requests will accept any TLS certificate presented by
554 the server, and will ignore hostname mismatches and/or expired
555 certificates, which will make your application vulnerable to
556 man-in-the-middle (MitM) attacks. Setting verify to ``False``
557 may be useful during local development or testing.
558 :param cert: (optional) if String, path to ssl client cert file (.pem).
559 If Tuple, ('cert', 'key') pair.
560 :rtype: requests.Response
577 proxies = proxies
or {}
580 prep.url, proxies, stream, verify, cert
586 "allow_redirects": allow_redirects,
589 resp = self.
send(prep, **send_kwargs)
593 def get(self, url, **kwargs):
594 r"""Sends a GET request. Returns :class:`Response` object.
596 :param url: URL for the new :class:`Request` object.
597 :param \*\*kwargs: Optional arguments that ``request`` takes.
598 :rtype: requests.Response
602 return self.
request(
"GET", url, **kwargs)
604 def options(self, url, **kwargs):
605 r"""Sends a OPTIONS request. Returns :class:`Response` object.
607 :param url: URL for the new :class:`Request` object.
608 :param \*\*kwargs: Optional arguments that ``request`` takes.
609 :rtype: requests.Response
613 return self.
request(
"OPTIONS", url, **kwargs)
615 def head(self, url, **kwargs):
616 r"""Sends a HEAD request. Returns :class:`Response` object.
618 :param url: URL for the new :class:`Request` object.
619 :param \*\*kwargs: Optional arguments that ``request`` takes.
620 :rtype: requests.Response
624 return self.
request(
"HEAD", url, **kwargs)
626 def post(self, url, data=None, json=None, **kwargs):
627 r"""Sends a POST request. Returns :class:`Response` object.
629 :param url: URL for the new :class:`Request` object.
630 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
631 object to send in the body of the :class:`Request`.
632 :param json: (optional) json to send in the body of the :class:`Request`.
633 :param \*\*kwargs: Optional arguments that ``request`` takes.
634 :rtype: requests.Response
637 return self.
request(
"POST", url, data=data, json=json, **kwargs)
639 def put(self, url, data=None, **kwargs):
640 r"""Sends a PUT request. Returns :class:`Response` object.
642 :param url: URL for the new :class:`Request` object.
643 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
644 object to send in the body of the :class:`Request`.
645 :param \*\*kwargs: Optional arguments that ``request`` takes.
646 :rtype: requests.Response
649 return self.
request(
"PUT", url, data=data, **kwargs)
651 def patch(self, url, data=None, **kwargs):
652 r"""Sends a PATCH request. Returns :class:`Response` object.
654 :param url: URL for the new :class:`Request` object.
655 :param data: (optional) Dictionary, list of tuples, bytes, or file-like
656 object to send in the body of the :class:`Request`.
657 :param \*\*kwargs: Optional arguments that ``request`` takes.
658 :rtype: requests.Response
661 return self.
request(
"PATCH", url, data=data, **kwargs)
663 def delete(self, url, **kwargs):
664 r"""Sends a DELETE request. Returns :class:`Response` object.
666 :param url: URL for the new :class:`Request` object.
667 :param \*\*kwargs: Optional arguments that ``request`` takes.
668 :rtype: requests.Response
671 return self.
request(
"DELETE", url, **kwargs)
673 def send(self, request, **kwargs):
674 """Send a given PreparedRequest.
676 :rtype: requests.Response
683 if "proxies" not in kwargs:
689 raise ValueError(
"You can only send PreparedRequests.")
692 allow_redirects =
kwargs.pop(
"allow_redirects",
True)
710 r = dispatch_hook(
"response", hooks, r, **kwargs)
725 history = [resp
for resp
in gen]
738 if not allow_redirects:
743 except StopIteration:
753 Check the environment and merge it with some settings.
760 no_proxy =
proxies.get(
"no_proxy")
if proxies
is not None else None
761 env_proxies = get_environ_proxies(url, no_proxy=no_proxy)
767 if verify
is True or verify
is None:
780 return {
"proxies": proxies,
"stream": stream,
"verify": verify,
"cert": cert}
784 Returns the appropriate connection adapter for the given URL.
786 :rtype: requests.adapters.BaseAdapter
788 for (prefix, adapter)
in self.
adapters.items():
794 raise InvalidSchema(f
"No connection adapters were found for {url!r}")
797 """Closes all adapters and as such the session"""
802 """Registers a connection adapter to a prefix.
804 Adapters are sorted in descending order by prefix length.
809 for key
in keys_to_move:
823 Returns a :class:`Session` for context-management.
825 .. deprecated:: 1.0.0
827 This method has been deprecated since version 1.0.0 and is only kept for
828 backwards compatibility. New code should use :class:`~requests.sessions.Session`
829 to create a session. This may be removed at a future date.
should_strip_auth(self, old_url, new_url)
rebuild_method(self, prepared_request, response)
resolve_redirects(self, resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs)
get_redirect_target(self, resp)
rebuild_proxies(self, prepared_request, proxies)
rebuild_auth(self, prepared_request, response)
prepare_request(self, request)
mount(self, prefix, adapter)
merge_environment_settings(self, url, proxies, stream, verify, cert)
request(self, method, url, 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, json=None)
send(self, request, **kwargs)
__setstate__(self, state)
merge_setting(request_setting, session_setting, dict_class=OrderedDict)
merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict)