VYm @sEdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z m Z mZmZddlmZmZmZmZdd lmZmZmZdd lmZmZdd lmZdd lmZm Z dd l!m"Z"m#Z#m$Z$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+ddlm,Z,m-Z-m.Z.m/Z/m0Z0m1Z1ddl2m3Z3ddlm4Z4dZ5ej6dkry ej7Z8Wne9k rej:Z8YnXn ejZ8e ddZ;e ddZ<Gddde=Z>Gddde>Z?ddZ@dS) z requests.session ~~~~~~~~~~~~~~~~ This module provides a Session object to manage and persist settings across requests (cookies, auth, proxies). N)Mapping) timedelta)_basic_auth_str) cookielibis_py3 OrderedDicturljoinurlparse)cookiejar_from_dictextract_cookies_to_jarRequestsCookieJar merge_cookies)RequestPreparedRequestDEFAULT_REDIRECT_LIMIT) default_hooks dispatch_hook)to_native_string)to_key_val_listdefault_headers)TooManyRedirects InvalidSchemaChunkedEncodingErrorContentDecodingError)RecentlyUsedContainer)CaseInsensitiveDict) HTTPAdapter) requote_uriget_environ_proxiesget_netrc_authshould_bypass_proxiesget_auth_from_url rewind_body)codes)REDIRECT_STATIiWindowscCs|dkr|S|dkr |St|to;t|tsB|S|t|}|jt|dd|jD}x|D] }||=qW|S)zDetermines 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` NcSs(g|]\}}|dkr|qS)N).0kvr'r'5/tmp/pip-build-04bmskau/requests/requests/sessions.py Ms z!merge_setting..) isinstancerrupdateitems)request_settingsession_setting dict_classmerged_setting none_keyskeyr'r'r+ merge_setting5s    r6cCsZ|dks!|jdgkr%|S|dksF|jdgkrJ|St|||S)zProperly merges both requests and session hooks. This is necessary because when request_hooks == {'response': []}, the merge breaks Session hooks entirely. Nresponse)getr6) request_hooks session_hooksr2r'r'r+ merge_hooksTs !!r;c@s[eZdZddZdddddddZdd Zd d Zd d ZdS)SessionRedirectMixincCs<|jr8|jd}tr+|jd}t|dSdS)z7Receives a Response. Returns a redirect URI or ``None``locationlatin1utf8N) is_redirectheadersrencoder)selfrespr=r'r'r+get_redirect_targetes    z(SessionRedirectMixin.get_redirect_targetFNTcksg} |j|} x| r|j} | j|| dd|_y |jWn.tttfk r|jj ddYnXt |j|j krt d|j d||j | jdrt|j} dt| j| f} t| } | j} | js4t|jt| } n t| } t| | _|jr}|j| jkr}| j|j|j<|j| ||jtjtjfkrd}x!|D]}| jj|dqWd| _ | j}y |d =Wnt!k rYnXt"| j#||jt$| j#|j%| j&| j#|j'| |}|j(| || j)dk od |kpd |k}|rt*| | }|j+|d |d|d|d|d|dd|}t"|j%| |j|j|} |VqWdS)z6Receives a Response. Returns a generator of Responses.rNdecode_contentFzExceeded %s redirects.r7z//z%s:%sContent-Length Content-TypeTransfer-EncodingCookiestreamtimeoutverifycertproxiesallow_redirects)rGrHrI),rEcopyappendhistorycontentrr RuntimeErrorrawreadlen max_redirectsrclose startswithr urlrschemegeturlnetlocr ris_permanent_redirectredirect_cacherebuild_method status_coder$temporary_redirectpermanent_redirectrApopbodyKeyErrorr _cookiesrcookiesprepare_cookiesrebuild_proxies rebuild_authZ_body_positionr#send)rCrDreqrKrLrMrNrOadapter_kwargshistr\prepared_request parsed_rurlparsedpurged_headersheaderrAZ rewindabler'r'r+resolve_redirectstsr                z&SessionRedirectMixin.resolve_redirectscCs|j}|j}d|krUt|jj}t|}|j|jkrU|d=|jrjt|nd}|dk r|j|dS)zWhen 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. AuthorizationN)rAr\r requesthostname trust_envr prepare_auth)rCrrr7rAr\original_parsedredirect_parsednew_authr'r'r+rms      z!SessionRedirectMixin.rebuild_authc Cs*|dk r|ni}|j}|j}t|j}|j}|jd}t|d|}|jr| rt|d|} | j|| jd} | r|j || d|kr|d=yt ||\} } Wnt k rd\} } YnX| r&| r&t | | |d<|S)aThis 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. :rtype: dict Nno_proxyallzProxy-Authorization)NN) rAr\r r]rQr8r!r{r setdefaultr"rhr) rCrrrOrAr\r] new_proxiesrZ bypass_proxyenviron_proxiesproxyusernamepasswordr'r'r+rls*       z$SessionRedirectMixin.rebuild_proxiescCs|j}|jtjkr-|dkr-d}|jtjkrQ|dkrQd}|jtjkru|dkrud}||_dS)zWhen being redirected we may want to change the method of the request based on certain specs or browser behavior. HEADGETPOSTN)methodrcr$ see_otherfoundmoved)rCrrr7rr'r'r+rb s z#SessionRedirectMixin.rebuild_method)__name__ __module__ __qualname__rErwrmrlrbr'r'r'r+r<cs  j  )r<c@s]eZdZdZdddddddd d d d d dg ZddZddZddZddZddddddddddddddddZ ddZ ddZ dd Z ddd!d"Z dd#d$Zdd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6ZdS)7Sessiona~A Requests session. Provides cookie persistence, connection-pooling, and configuration. Basic Usage:: >>> import requests >>> s = requests.Session() >>> s.get('http://httpbin.org/get') Or as a context manager:: >>> with requests.Session() as s: >>> s.get('http://httpbin.org/get') rArjauthrOhooksparamsrMrNprefetchadaptersrKr{rYcCst|_d|_i|_t|_i|_d|_d|_d|_ t |_ d|_ t i|_t|_|jdt|jdttt|_dS)NFTzhttps://zhttp://)rrArrOrrrrKrMrNrrYr{r rjrrmountrrREDIRECT_CACHE_SIZEra)rCr'r'r+__init__Ps           zSession.__init__cCs|S)Nr')rCr'r'r+ __enter__szSession.__enter__cGs|jdS)N)rZ)rCargsr'r'r+__exit__szSession.__exit__cCs$|jp i}t|tjs-t|}ttt|j|}|j}|jr}| r}|j r}t |j }t }|j d|j jd|j d|jd|jd|jdt|j|jdtdt|j|jd t||jd |d t|j|j |S) aConstructs a :class:`PreparedRequest ` for transmission and returns it. The :class:`PreparedRequest` has settings merged from the :class:`Request ` instance and those of the :class:`Session`. :param request: :class:`Request` instance to prepare with this session's settings. :rtype: requests.PreparedRequest rr\filesdatajsonrAr2rrrjr)rjr-r CookieJarr rr rr{r r\rpreparerupperrrrr6rArrr;r)rCryrjmerged_cookiesrpr'r'r+prepare_requests*         zSession.prepare_requestNTcCstd|jd|d|d|d|p-id|d|p?id|d |d | }|j|}| poi} |j|j| | ||}d | d | i}|j||j||}|S) aConstructs a :class:`Request `, prepares it and sends it. Returns :class:`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, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json 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) How long to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) ` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Set to True by default. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol or protocol and hostname to the URL of the proxy. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :rtype: requests.Response rr\rArrrrrrjrrLrP)rrrmerge_environment_settingsr\r.rn)rCrr\rrrArjrrrLrPrOrrKrMrNrroprepsettings send_kwargsrDr'r'r+rys(4       zSession.requestcKs#|jdd|jd||S)zSends a GET request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rPTr)rry)rCr\kwargsr'r'r+r8 sz Session.getcKs#|jdd|jd||S)zSends a OPTIONS request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rPTOPTIONS)rry)rCr\rr'r'r+optionsszSession.optionscKs#|jdd|jd||S)zSends a HEAD request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rPFr)rry)rCr\rr'r'r+head sz Session.headcKs|jd|d|d||S)aSends 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 json: (optional) json to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response rrr)ry)rCr\rrrr'r'r+post+s z Session.postcKs|jd|d||S)aYSends 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. :rtype: requests.Response PUTr)ry)rCr\rrr'r'r+put7s z Session.putcKs|jd|d||S)a[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. :rtype: requests.Response PATCHr)ry)rCr\rrr'r'r+patchBs z Session.patchcKs|jd||S)zSends a DELETE request. Returns :class:`Response` object. :param url: URL for the new :class:`Request` object. :param \*\*kwargs: Optional arguments that ``request`` takes. :rtype: requests.Response DELETE)ry)rCr\rr'r'r+deleteMszSession.deletecKs|jd|j|jd|j|jd|j|jd|jt|trgtd|jdd}|j d}|j }|rt }xQ|j |j kr|j|j |j j |j }||krP||_ qW|jd|j }t} |j||} t| } td | | _td || |} | jrx*| jD]} t|j| j| jqkWt|j|| j|j| ||} |rd d | Dng}|r|jd | |j} || _|s| j| S)zR Send a given PreparedRequest. :rtype: requests.Response rKrMrNrOz#You can only send PreparedRequests.rPTr\secondsr7cSsg|] }|qSr'r')r(rDr'r'r+r,s z Session.send..r)rrKrMrNrOr-r ValueErrorrfr8rsetr\raadd get_adapterpreferred_clockrnrelapsedrrSr rjryrVrwinsertrT)rCryrrPrKr checked_urlsnew_urladapterstartrrrDgenrSr'r'r+rnWsH          z Session.sendc Cs |jr|dk r$|jdnd}t|d|}x*|jD]\}} |j|| qIW|dks|dkrtjjdptjjd}t||j}t||j }t||j }t||j }d|d|d|d |iS) z^ Check the environment and merge it with some settings. :rtype: dict NrTREQUESTS_CA_BUNDLECURL_CA_BUNDLErMrOrKrN) r{r8rr/rosenvironr6rOrKrMrN) rCr\rOrKrMrNr env_proxiesr)r*r'r'r+rs !z"Session.merge_environment_settingscCsMx6|jjD]%\}}|jj|r|SqWtd|dS)z~ Returns the appropriate connection adapter for the given URL. :rtype: requests.adapters.BaseAdapter z*No connection adapters were found for '%s'N)rr/lowerr[r)rCr\prefixrr'r'r+rszSession.get_adaptercCs(x!|jjD]}|jqWdS)z+Closes all adapters and as such the sessionN)rvaluesrZ)rCr*r'r'r+rZsz Session.closecsW||j<fdd|jD}x'|D]}|jj||j|.N)rrf)rCrr keys_to_mover5r')rr+rs  z Session.mountcs9tfddjD}tj|d<|S)Nc3s'|]}|t|dfVqdS)N)getattr)r(attr)rCr'r+ sz'Session.__getstate__..ra)dict __attrs__ra)rCstater')rCr+ __getstate__s"zSession.__getstate__cCs||jdi}x*|jD]\}}t|||qWtt|_x'|jD]\}}||j| s@   ("".