Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sessions.py 30KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. """
  2. requests.sessions
  3. ~~~~~~~~~~~~~~~~~
  4. This module provides a Session object to manage and persist settings across
  5. requests (cookies, auth, proxies).
  6. """
  7. import os
  8. import sys
  9. import time
  10. from collections import OrderedDict
  11. from datetime import timedelta
  12. from ._internal_utils import to_native_string
  13. from .adapters import HTTPAdapter
  14. from .auth import _basic_auth_str
  15. from .compat import Mapping, cookielib, urljoin, urlparse
  16. from .cookies import (
  17. RequestsCookieJar,
  18. cookiejar_from_dict,
  19. extract_cookies_to_jar,
  20. merge_cookies,
  21. )
  22. from .exceptions import (
  23. ChunkedEncodingError,
  24. ContentDecodingError,
  25. InvalidSchema,
  26. TooManyRedirects,
  27. )
  28. from .hooks import default_hooks, dispatch_hook
  29. # formerly defined here, reexposed here for backward compatibility
  30. from .models import ( # noqa: F401
  31. DEFAULT_REDIRECT_LIMIT,
  32. REDIRECT_STATI,
  33. PreparedRequest,
  34. Request,
  35. )
  36. from .status_codes import codes
  37. from .structures import CaseInsensitiveDict
  38. from .utils import ( # noqa: F401
  39. DEFAULT_PORTS,
  40. default_headers,
  41. get_auth_from_url,
  42. get_environ_proxies,
  43. get_netrc_auth,
  44. requote_uri,
  45. resolve_proxies,
  46. rewind_body,
  47. should_bypass_proxies,
  48. to_key_val_list,
  49. )
  50. # Preferred clock, based on which one is more accurate on a given system.
  51. if sys.platform == "win32":
  52. preferred_clock = time.perf_counter
  53. else:
  54. preferred_clock = time.time
  55. def merge_setting(request_setting, session_setting, dict_class=OrderedDict):
  56. """Determines appropriate setting for a given request, taking into account
  57. the explicit setting on that request, and the setting in the session. If a
  58. setting is a dictionary, they will be merged together using `dict_class`
  59. """
  60. if session_setting is None:
  61. return request_setting
  62. if request_setting is None:
  63. return session_setting
  64. # Bypass if not a dictionary (e.g. verify)
  65. if not (
  66. isinstance(session_setting, Mapping) and isinstance(request_setting, Mapping)
  67. ):
  68. return request_setting
  69. merged_setting = dict_class(to_key_val_list(session_setting))
  70. merged_setting.update(to_key_val_list(request_setting))
  71. # Remove keys that are set to None. Extract keys first to avoid altering
  72. # the dictionary during iteration.
  73. none_keys = [k for (k, v) in merged_setting.items() if v is None]
  74. for key in none_keys:
  75. del merged_setting[key]
  76. return merged_setting
  77. def merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict):
  78. """Properly merges both requests and session hooks.
  79. This is necessary because when request_hooks == {'response': []}, the
  80. merge breaks Session hooks entirely.
  81. """
  82. if session_hooks is None or session_hooks.get("response") == []:
  83. return request_hooks
  84. if request_hooks is None or request_hooks.get("response") == []:
  85. return session_hooks
  86. return merge_setting(request_hooks, session_hooks, dict_class)
  87. class SessionRedirectMixin:
  88. def get_redirect_target(self, resp):
  89. """Receives a Response. Returns a redirect URI or ``None``"""
  90. # Due to the nature of how requests processes redirects this method will
  91. # be called at least once upon the original response and at least twice
  92. # on each subsequent redirect response (if any).
  93. # If a custom mixin is used to handle this logic, it may be advantageous
  94. # to cache the redirect location onto the response object as a private
  95. # attribute.
  96. if resp.is_redirect:
  97. location = resp.headers["location"]
  98. # Currently the underlying http module on py3 decode headers
  99. # in latin1, but empirical evidence suggests that latin1 is very
  100. # rarely used with non-ASCII characters in HTTP headers.
  101. # It is more likely to get UTF8 header rather than latin1.
  102. # This causes incorrect handling of UTF8 encoded location headers.
  103. # To solve this, we re-encode the location in latin1.
  104. location = location.encode("latin1")
  105. return to_native_string(location, "utf8")
  106. return None
  107. def should_strip_auth(self, old_url, new_url):
  108. """Decide whether Authorization header should be removed when redirecting"""
  109. old_parsed = urlparse(old_url)
  110. new_parsed = urlparse(new_url)
  111. if old_parsed.hostname != new_parsed.hostname:
  112. return True
  113. # Special case: allow http -> https redirect when using the standard
  114. # ports. This isn't specified by RFC 7235, but is kept to avoid
  115. # breaking backwards compatibility with older versions of requests
  116. # that allowed any redirects on the same host.
  117. if (
  118. old_parsed.scheme == "http"
  119. and old_parsed.port in (80, None)
  120. and new_parsed.scheme == "https"
  121. and new_parsed.port in (443, None)
  122. ):
  123. return False
  124. # Handle default port usage corresponding to scheme.
  125. changed_port = old_parsed.port != new_parsed.port
  126. changed_scheme = old_parsed.scheme != new_parsed.scheme
  127. default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)
  128. if (
  129. not changed_scheme
  130. and old_parsed.port in default_port
  131. and new_parsed.port in default_port
  132. ):
  133. return False
  134. # Standard case: root URI must match
  135. return changed_port or changed_scheme
  136. def resolve_redirects(
  137. self,
  138. resp,
  139. req,
  140. stream=False,
  141. timeout=None,
  142. verify=True,
  143. cert=None,
  144. proxies=None,
  145. yield_requests=False,
  146. **adapter_kwargs,
  147. ):
  148. """Receives a Response. Returns a generator of Responses or Requests."""
  149. hist = [] # keep track of history
  150. url = self.get_redirect_target(resp)
  151. previous_fragment = urlparse(req.url).fragment
  152. while url:
  153. prepared_request = req.copy()
  154. # Update history and keep track of redirects.
  155. # resp.history must ignore the original request in this loop
  156. hist.append(resp)
  157. resp.history = hist[1:]
  158. try:
  159. resp.content # Consume socket so it can be released
  160. except (ChunkedEncodingError, ContentDecodingError, RuntimeError):
  161. resp.raw.read(decode_content=False)
  162. if len(resp.history) >= self.max_redirects:
  163. raise TooManyRedirects(
  164. f"Exceeded {self.max_redirects} redirects.", response=resp
  165. )
  166. # Release the connection back into the pool.
  167. resp.close()
  168. # Handle redirection without scheme (see: RFC 1808 Section 4)
  169. if url.startswith("//"):
  170. parsed_rurl = urlparse(resp.url)
  171. url = ":".join([to_native_string(parsed_rurl.scheme), url])
  172. # Normalize url case and attach previous fragment if needed (RFC 7231 7.1.2)
  173. parsed = urlparse(url)
  174. if parsed.fragment == "" and previous_fragment:
  175. parsed = parsed._replace(fragment=previous_fragment)
  176. elif parsed.fragment:
  177. previous_fragment = parsed.fragment
  178. url = parsed.geturl()
  179. # Facilitate relative 'location' headers, as allowed by RFC 7231.
  180. # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
  181. # Compliant with RFC3986, we percent encode the url.
  182. if not parsed.netloc:
  183. url = urljoin(resp.url, requote_uri(url))
  184. else:
  185. url = requote_uri(url)
  186. prepared_request.url = to_native_string(url)
  187. self.rebuild_method(prepared_request, resp)
  188. # https://github.com/psf/requests/issues/1084
  189. if resp.status_code not in (
  190. codes.temporary_redirect,
  191. codes.permanent_redirect,
  192. ):
  193. # https://github.com/psf/requests/issues/3490
  194. purged_headers = ("Content-Length", "Content-Type", "Transfer-Encoding")
  195. for header in purged_headers:
  196. prepared_request.headers.pop(header, None)
  197. prepared_request.body = None
  198. headers = prepared_request.headers
  199. headers.pop("Cookie", None)
  200. # Extract any cookies sent on the response to the cookiejar
  201. # in the new request. Because we've mutated our copied prepared
  202. # request, use the old one that we haven't yet touched.
  203. extract_cookies_to_jar(prepared_request._cookies, req, resp.raw)
  204. merge_cookies(prepared_request._cookies, self.cookies)
  205. prepared_request.prepare_cookies(prepared_request._cookies)
  206. # Rebuild auth and proxy information.
  207. proxies = self.rebuild_proxies(prepared_request, proxies)
  208. self.rebuild_auth(prepared_request, resp)
  209. # A failed tell() sets `_body_position` to `object()`. This non-None
  210. # value ensures `rewindable` will be True, allowing us to raise an
  211. # UnrewindableBodyError, instead of hanging the connection.
  212. rewindable = prepared_request._body_position is not None and (
  213. "Content-Length" in headers or "Transfer-Encoding" in headers
  214. )
  215. # Attempt to rewind consumed file-like object.
  216. if rewindable:
  217. rewind_body(prepared_request)
  218. # Override the original request.
  219. req = prepared_request
  220. if yield_requests:
  221. yield req
  222. else:
  223. resp = self.send(
  224. req,
  225. stream=stream,
  226. timeout=timeout,
  227. verify=verify,
  228. cert=cert,
  229. proxies=proxies,
  230. allow_redirects=False,
  231. **adapter_kwargs,
  232. )
  233. extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)
  234. # extract redirect url, if any, for the next loop
  235. url = self.get_redirect_target(resp)
  236. yield resp
  237. def rebuild_auth(self, prepared_request, response):
  238. """When being redirected we may want to strip authentication from the
  239. request to avoid leaking credentials. This method intelligently removes
  240. and reapplies authentication where possible to avoid credential loss.
  241. """
  242. headers = prepared_request.headers
  243. url = prepared_request.url
  244. if "Authorization" in headers and self.should_strip_auth(
  245. response.request.url, url
  246. ):
  247. # If we get redirected to a new host, we should strip out any
  248. # authentication headers.
  249. del headers["Authorization"]
  250. # .netrc might have more auth for us on our new host.
  251. new_auth = get_netrc_auth(url) if self.trust_env else None
  252. if new_auth is not None:
  253. prepared_request.prepare_auth(new_auth)
  254. def rebuild_proxies(self, prepared_request, proxies):
  255. """This method re-evaluates the proxy configuration by considering the
  256. environment variables. If we are redirected to a URL covered by
  257. NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
  258. proxy keys for this URL (in case they were stripped by a previous
  259. redirect).
  260. This method also replaces the Proxy-Authorization header where
  261. necessary.
  262. :rtype: dict
  263. """
  264. headers = prepared_request.headers
  265. scheme = urlparse(prepared_request.url).scheme
  266. new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env)
  267. if "Proxy-Authorization" in headers:
  268. del headers["Proxy-Authorization"]
  269. try:
  270. username, password = get_auth_from_url(new_proxies[scheme])
  271. except KeyError:
  272. username, password = None, None
  273. # urllib3 handles proxy authorization for us in the standard adapter.
  274. # Avoid appending this to TLS tunneled requests where it may be leaked.
  275. if not scheme.startswith('https') and username and password:
  276. headers["Proxy-Authorization"] = _basic_auth_str(username, password)
  277. return new_proxies
  278. def rebuild_method(self, prepared_request, response):
  279. """When being redirected we may want to change the method of the request
  280. based on certain specs or browser behavior.
  281. """
  282. method = prepared_request.method
  283. # https://tools.ietf.org/html/rfc7231#section-6.4.4
  284. if response.status_code == codes.see_other and method != "HEAD":
  285. method = "GET"
  286. # Do what the browsers do, despite standards...
  287. # First, turn 302s into GETs.
  288. if response.status_code == codes.found and method != "HEAD":
  289. method = "GET"
  290. # Second, if a POST is responded to with a 301, turn it into a GET.
  291. # This bizarre behaviour is explained in Issue 1704.
  292. if response.status_code == codes.moved and method == "POST":
  293. method = "GET"
  294. prepared_request.method = method
  295. class Session(SessionRedirectMixin):
  296. """A Requests session.
  297. Provides cookie persistence, connection-pooling, and configuration.
  298. Basic Usage::
  299. >>> import requests
  300. >>> s = requests.Session()
  301. >>> s.get('https://httpbin.org/get')
  302. <Response [200]>
  303. Or as a context manager::
  304. >>> with requests.Session() as s:
  305. ... s.get('https://httpbin.org/get')
  306. <Response [200]>
  307. """
  308. __attrs__ = [
  309. "headers",
  310. "cookies",
  311. "auth",
  312. "proxies",
  313. "hooks",
  314. "params",
  315. "verify",
  316. "cert",
  317. "adapters",
  318. "stream",
  319. "trust_env",
  320. "max_redirects",
  321. ]
  322. def __init__(self):
  323. #: A case-insensitive dictionary of headers to be sent on each
  324. #: :class:`Request <Request>` sent from this
  325. #: :class:`Session <Session>`.
  326. self.headers = default_headers()
  327. #: Default Authentication tuple or object to attach to
  328. #: :class:`Request <Request>`.
  329. self.auth = None
  330. #: Dictionary mapping protocol or protocol and host to the URL of the proxy
  331. #: (e.g. {'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}) to
  332. #: be used on each :class:`Request <Request>`.
  333. self.proxies = {}
  334. #: Event-handling hooks.
  335. self.hooks = default_hooks()
  336. #: Dictionary of querystring data to attach to each
  337. #: :class:`Request <Request>`. The dictionary values may be lists for
  338. #: representing multivalued query parameters.
  339. self.params = {}
  340. #: Stream response content default.
  341. self.stream = False
  342. #: SSL Verification default.
  343. #: Defaults to `True`, requiring requests to verify the TLS certificate at the
  344. #: remote end.
  345. #: If verify is set to `False`, requests will accept any TLS certificate
  346. #: presented by the server, and will ignore hostname mismatches and/or
  347. #: expired certificates, which will make your application vulnerable to
  348. #: man-in-the-middle (MitM) attacks.
  349. #: Only set this to `False` for testing.
  350. self.verify = True
  351. #: SSL client certificate default, if String, path to ssl client
  352. #: cert file (.pem). If Tuple, ('cert', 'key') pair.
  353. self.cert = None
  354. #: Maximum number of redirects allowed. If the request exceeds this
  355. #: limit, a :class:`TooManyRedirects` exception is raised.
  356. #: This defaults to requests.models.DEFAULT_REDIRECT_LIMIT, which is
  357. #: 30.
  358. self.max_redirects = DEFAULT_REDIRECT_LIMIT
  359. #: Trust environment settings for proxy configuration, default
  360. #: authentication and similar.
  361. self.trust_env = True
  362. #: A CookieJar containing all currently outstanding cookies set on this
  363. #: session. By default it is a
  364. #: :class:`RequestsCookieJar <requests.cookies.RequestsCookieJar>`, but
  365. #: may be any other ``cookielib.CookieJar`` compatible object.
  366. self.cookies = cookiejar_from_dict({})
  367. # Default connection adapters.
  368. self.adapters = OrderedDict()
  369. self.mount("https://", HTTPAdapter())
  370. self.mount("http://", HTTPAdapter())
  371. def __enter__(self):
  372. return self
  373. def __exit__(self, *args):
  374. self.close()
  375. def prepare_request(self, request):
  376. """Constructs a :class:`PreparedRequest <PreparedRequest>` for
  377. transmission and returns it. The :class:`PreparedRequest` has settings
  378. merged from the :class:`Request <Request>` instance and those of the
  379. :class:`Session`.
  380. :param request: :class:`Request` instance to prepare with this
  381. session's settings.
  382. :rtype: requests.PreparedRequest
  383. """
  384. cookies = request.cookies or {}
  385. # Bootstrap CookieJar.
  386. if not isinstance(cookies, cookielib.CookieJar):
  387. cookies = cookiejar_from_dict(cookies)
  388. # Merge with session cookies
  389. merged_cookies = merge_cookies(
  390. merge_cookies(RequestsCookieJar(), self.cookies), cookies
  391. )
  392. # Set environment's basic authentication if not explicitly set.
  393. auth = request.auth
  394. if self.trust_env and not auth and not self.auth:
  395. auth = get_netrc_auth(request.url)
  396. p = PreparedRequest()
  397. p.prepare(
  398. method=request.method.upper(),
  399. url=request.url,
  400. files=request.files,
  401. data=request.data,
  402. json=request.json,
  403. headers=merge_setting(
  404. request.headers, self.headers, dict_class=CaseInsensitiveDict
  405. ),
  406. params=merge_setting(request.params, self.params),
  407. auth=merge_setting(auth, self.auth),
  408. cookies=merged_cookies,
  409. hooks=merge_hooks(request.hooks, self.hooks),
  410. )
  411. return p
  412. def request(
  413. self,
  414. method,
  415. url,
  416. params=None,
  417. data=None,
  418. headers=None,
  419. cookies=None,
  420. files=None,
  421. auth=None,
  422. timeout=None,
  423. allow_redirects=True,
  424. proxies=None,
  425. hooks=None,
  426. stream=None,
  427. verify=None,
  428. cert=None,
  429. json=None,
  430. ):
  431. """Constructs a :class:`Request <Request>`, prepares it and sends it.
  432. Returns :class:`Response <Response>` object.
  433. :param method: method for the new :class:`Request` object.
  434. :param url: URL for the new :class:`Request` object.
  435. :param params: (optional) Dictionary or bytes to be sent in the query
  436. string for the :class:`Request`.
  437. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  438. object to send in the body of the :class:`Request`.
  439. :param json: (optional) json to send in the body of the
  440. :class:`Request`.
  441. :param headers: (optional) Dictionary of HTTP Headers to send with the
  442. :class:`Request`.
  443. :param cookies: (optional) Dict or CookieJar object to send with the
  444. :class:`Request`.
  445. :param files: (optional) Dictionary of ``'filename': file-like-objects``
  446. for multipart encoding upload.
  447. :param auth: (optional) Auth tuple or callable to enable
  448. Basic/Digest/Custom HTTP Auth.
  449. :param timeout: (optional) How long to wait for the server to send
  450. data before giving up, as a float, or a :ref:`(connect timeout,
  451. read timeout) <timeouts>` tuple.
  452. :type timeout: float or tuple
  453. :param allow_redirects: (optional) Set to True by default.
  454. :type allow_redirects: bool
  455. :param proxies: (optional) Dictionary mapping protocol or protocol and
  456. hostname to the URL of the proxy.
  457. :param stream: (optional) whether to immediately download the response
  458. content. Defaults to ``False``.
  459. :param verify: (optional) Either a boolean, in which case it controls whether we verify
  460. the server's TLS certificate, or a string, in which case it must be a path
  461. to a CA bundle to use. Defaults to ``True``. When set to
  462. ``False``, requests will accept any TLS certificate presented by
  463. the server, and will ignore hostname mismatches and/or expired
  464. certificates, which will make your application vulnerable to
  465. man-in-the-middle (MitM) attacks. Setting verify to ``False``
  466. may be useful during local development or testing.
  467. :param cert: (optional) if String, path to ssl client cert file (.pem).
  468. If Tuple, ('cert', 'key') pair.
  469. :rtype: requests.Response
  470. """
  471. # Create the Request.
  472. req = Request(
  473. method=method.upper(),
  474. url=url,
  475. headers=headers,
  476. files=files,
  477. data=data or {},
  478. json=json,
  479. params=params or {},
  480. auth=auth,
  481. cookies=cookies,
  482. hooks=hooks,
  483. )
  484. prep = self.prepare_request(req)
  485. proxies = proxies or {}
  486. settings = self.merge_environment_settings(
  487. prep.url, proxies, stream, verify, cert
  488. )
  489. # Send the request.
  490. send_kwargs = {
  491. "timeout": timeout,
  492. "allow_redirects": allow_redirects,
  493. }
  494. send_kwargs.update(settings)
  495. resp = self.send(prep, **send_kwargs)
  496. return resp
  497. def get(self, url, **kwargs):
  498. r"""Sends a GET request. Returns :class:`Response` object.
  499. :param url: URL for the new :class:`Request` object.
  500. :param \*\*kwargs: Optional arguments that ``request`` takes.
  501. :rtype: requests.Response
  502. """
  503. kwargs.setdefault("allow_redirects", True)
  504. return self.request("GET", url, **kwargs)
  505. def options(self, url, **kwargs):
  506. r"""Sends a OPTIONS request. Returns :class:`Response` object.
  507. :param url: URL for the new :class:`Request` object.
  508. :param \*\*kwargs: Optional arguments that ``request`` takes.
  509. :rtype: requests.Response
  510. """
  511. kwargs.setdefault("allow_redirects", True)
  512. return self.request("OPTIONS", url, **kwargs)
  513. def head(self, url, **kwargs):
  514. r"""Sends a HEAD request. Returns :class:`Response` object.
  515. :param url: URL for the new :class:`Request` object.
  516. :param \*\*kwargs: Optional arguments that ``request`` takes.
  517. :rtype: requests.Response
  518. """
  519. kwargs.setdefault("allow_redirects", False)
  520. return self.request("HEAD", url, **kwargs)
  521. def post(self, url, data=None, json=None, **kwargs):
  522. r"""Sends a POST request. Returns :class:`Response` object.
  523. :param url: URL for the new :class:`Request` object.
  524. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  525. object to send in the body of the :class:`Request`.
  526. :param json: (optional) json to send in the body of the :class:`Request`.
  527. :param \*\*kwargs: Optional arguments that ``request`` takes.
  528. :rtype: requests.Response
  529. """
  530. return self.request("POST", url, data=data, json=json, **kwargs)
  531. def put(self, url, data=None, **kwargs):
  532. r"""Sends a PUT request. Returns :class:`Response` object.
  533. :param url: URL for the new :class:`Request` object.
  534. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  535. object to send in the body of the :class:`Request`.
  536. :param \*\*kwargs: Optional arguments that ``request`` takes.
  537. :rtype: requests.Response
  538. """
  539. return self.request("PUT", url, data=data, **kwargs)
  540. def patch(self, url, data=None, **kwargs):
  541. r"""Sends a PATCH request. Returns :class:`Response` object.
  542. :param url: URL for the new :class:`Request` object.
  543. :param data: (optional) Dictionary, list of tuples, bytes, or file-like
  544. object to send in the body of the :class:`Request`.
  545. :param \*\*kwargs: Optional arguments that ``request`` takes.
  546. :rtype: requests.Response
  547. """
  548. return self.request("PATCH", url, data=data, **kwargs)
  549. def delete(self, url, **kwargs):
  550. r"""Sends a DELETE request. Returns :class:`Response` object.
  551. :param url: URL for the new :class:`Request` object.
  552. :param \*\*kwargs: Optional arguments that ``request`` takes.
  553. :rtype: requests.Response
  554. """
  555. return self.request("DELETE", url, **kwargs)
  556. def send(self, request, **kwargs):
  557. """Send a given PreparedRequest.
  558. :rtype: requests.Response
  559. """
  560. # Set defaults that the hooks can utilize to ensure they always have
  561. # the correct parameters to reproduce the previous request.
  562. kwargs.setdefault("stream", self.stream)
  563. kwargs.setdefault("verify", self.verify)
  564. kwargs.setdefault("cert", self.cert)
  565. if "proxies" not in kwargs:
  566. kwargs["proxies"] = resolve_proxies(request, self.proxies, self.trust_env)
  567. # It's possible that users might accidentally send a Request object.
  568. # Guard against that specific failure case.
  569. if isinstance(request, Request):
  570. raise ValueError("You can only send PreparedRequests.")
  571. # Set up variables needed for resolve_redirects and dispatching of hooks
  572. allow_redirects = kwargs.pop("allow_redirects", True)
  573. stream = kwargs.get("stream")
  574. hooks = request.hooks
  575. # Get the appropriate adapter to use
  576. adapter = self.get_adapter(url=request.url)
  577. # Start time (approximately) of the request
  578. start = preferred_clock()
  579. # Send the request
  580. r = adapter.send(request, **kwargs)
  581. # Total elapsed time of the request (approximately)
  582. elapsed = preferred_clock() - start
  583. r.elapsed = timedelta(seconds=elapsed)
  584. # Response manipulation hooks
  585. r = dispatch_hook("response", hooks, r, **kwargs)
  586. # Persist cookies
  587. if r.history:
  588. # If the hooks create history then we want those cookies too
  589. for resp in r.history:
  590. extract_cookies_to_jar(self.cookies, resp.request, resp.raw)
  591. extract_cookies_to_jar(self.cookies, request, r.raw)
  592. # Resolve redirects if allowed.
  593. if allow_redirects:
  594. # Redirect resolving generator.
  595. gen = self.resolve_redirects(r, request, **kwargs)
  596. history = [resp for resp in gen]
  597. else:
  598. history = []
  599. # Shuffle things around if there's history.
  600. if history:
  601. # Insert the first (original) request at the start
  602. history.insert(0, r)
  603. # Get the last request made
  604. r = history.pop()
  605. r.history = history
  606. # If redirects aren't being followed, store the response on the Request for Response.next().
  607. if not allow_redirects:
  608. try:
  609. r._next = next(
  610. self.resolve_redirects(r, request, yield_requests=True, **kwargs)
  611. )
  612. except StopIteration:
  613. pass
  614. if not stream:
  615. r.content
  616. return r
  617. def merge_environment_settings(self, url, proxies, stream, verify, cert):
  618. """
  619. Check the environment and merge it with some settings.
  620. :rtype: dict
  621. """
  622. # Gather clues from the surrounding environment.
  623. if self.trust_env:
  624. # Set environment's proxies.
  625. no_proxy = proxies.get("no_proxy") if proxies is not None else None
  626. env_proxies = get_environ_proxies(url, no_proxy=no_proxy)
  627. for (k, v) in env_proxies.items():
  628. proxies.setdefault(k, v)
  629. # Look for requests environment configuration
  630. # and be compatible with cURL.
  631. if verify is True or verify is None:
  632. verify = (
  633. os.environ.get("REQUESTS_CA_BUNDLE")
  634. or os.environ.get("CURL_CA_BUNDLE")
  635. or verify
  636. )
  637. # Merge all the kwargs.
  638. proxies = merge_setting(proxies, self.proxies)
  639. stream = merge_setting(stream, self.stream)
  640. verify = merge_setting(verify, self.verify)
  641. cert = merge_setting(cert, self.cert)
  642. return {"proxies": proxies, "stream": stream, "verify": verify, "cert": cert}
  643. def get_adapter(self, url):
  644. """
  645. Returns the appropriate connection adapter for the given URL.
  646. :rtype: requests.adapters.BaseAdapter
  647. """
  648. for (prefix, adapter) in self.adapters.items():
  649. if url.lower().startswith(prefix.lower()):
  650. return adapter
  651. # Nothing matches :-/
  652. raise InvalidSchema(f"No connection adapters were found for {url!r}")
  653. def close(self):
  654. """Closes all adapters and as such the session"""
  655. for v in self.adapters.values():
  656. v.close()
  657. def mount(self, prefix, adapter):
  658. """Registers a connection adapter to a prefix.
  659. Adapters are sorted in descending order by prefix length.
  660. """
  661. self.adapters[prefix] = adapter
  662. keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
  663. for key in keys_to_move:
  664. self.adapters[key] = self.adapters.pop(key)
  665. def __getstate__(self):
  666. state = {attr: getattr(self, attr, None) for attr in self.__attrs__}
  667. return state
  668. def __setstate__(self, state):
  669. for attr, value in state.items():
  670. setattr(self, attr, value)
  671. def session():
  672. """
  673. Returns a :class:`Session` for context-management.
  674. .. deprecated:: 1.0.0
  675. This method has been deprecated since version 1.0.0 and is only kept for
  676. backwards compatibility. New code should use :class:`~requests.sessions.Session`
  677. to create a session. This may be removed at a future date.
  678. :rtype: Session
  679. """
  680. return Session()