1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
U
¡ý°däuã@sldZddlZddlZddlZddlmZddlmZddlm    Z    ddl
m Z ddl m Z dd    lmZmZmZmZdd
lmZmZmZmZdd lmZmZmZmZdd lmZmZdd l m!Z!m"Z"m#Z#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3ej4dkr$ej5Z6nejZ6efdd„Z7efdd„Z8Gdd„dƒZ9Gdd„de9ƒZ:dd„Z;dS)z•
requests.sessions
~~~~~~~~~~~~~~~~~
 
This module provides a Session object to manage and persist settings across
requests (cookies, auth, proxies).
éN)Ú OrderedDict)Ú    timedeltaé)Úto_native_string)Ú HTTPAdapter)Ú_basic_auth_str)ÚMappingÚ    cookielibÚurljoinÚurlparse)ÚRequestsCookieJarÚcookiejar_from_dictÚextract_cookies_to_jarÚ merge_cookies)ÚChunkedEncodingErrorÚContentDecodingErrorÚ InvalidSchemaÚTooManyRedirects)Ú default_hooksÚ dispatch_hook)ÚDEFAULT_REDIRECT_LIMITÚREDIRECT_STATIÚPreparedRequestÚRequest)Úcodes)ÚCaseInsensitiveDict)
Ú DEFAULT_PORTSÚdefault_headersÚget_auth_from_urlÚget_environ_proxiesÚget_netrc_authÚ requote_uriÚresolve_proxiesÚ rewind_bodyÚshould_bypass_proxiesÚto_key_val_listÚwin32cCsp|dkr |S|dkr|St|tƒr,t|tƒs0|S|t|ƒƒ}| t|ƒ¡dd„| ¡Dƒ}|D]
}||=q`|S)zè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`
    NcSsg|]\}}|dkr|‘qS©N©)Ú.0ÚkÚvr(r(úHd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\requests/sessions.pyÚ
<listcomp>Tsz!merge_setting.<locals>.<listcomp>)Ú
isinstancerr%ÚupdateÚitems)Úrequest_settingÚsession_settingÚ
dict_classÚmerged_settingÚ    none_keysÚkeyr(r(r,Ú merge_setting=sÿÿ r7cCs@|dks| d¡gkr|S|dks0| d¡gkr4|St|||ƒS)z©Properly merges both requests and session hooks.
 
    This is necessary because when request_hooks == {'response': []}, the
    merge breaks Session hooks entirely.
    NÚresponse)Úgetr7)Ú request_hooksÚ session_hooksr3r(r(r,Ú merge_hooks[s
r<c@s>eZdZdd„Zdd„Zddd    „Zd
d „Zd d „Zdd„ZdS)ÚSessionRedirectMixincCs(|jr$|jd}| d¡}t|dƒSdS)z7Receives a Response. Returns a redirect URI or ``None``ÚlocationÚlatin1Úutf8N)Ú is_redirectÚheadersÚencoder)ÚselfÚrespr>r(r(r,Úget_redirect_targetks
 
 
 
z(SessionRedirectMixin.get_redirect_targetcCsšt|ƒ}t|ƒ}|j|jkr dS|jdkrL|jdkrL|jdkrL|jdkrLdS|j|jk}|j|jk}t |jd¡df}|s’|j|kr’|j|kr’dS|p˜|S)zFDecide whether Authorization header should be removed when redirectingTÚhttp)éPNÚhttps)i»NFN)r ÚhostnameÚschemeÚportrr9)rDÚold_urlÚnew_urlÚ
old_parsedÚ
new_parsedÚ changed_portÚchanged_schemeÚ default_portr(r(r,Úshould_strip_auths0 ÿþýü  ÿþýz&SessionRedirectMixin.should_strip_authFNTc    
ksJg}
| |¡} t|jƒj} | rF| ¡} |
 |¡|
dd…|_z
|jWn(tt    t
fk
rr|j j ddYnXt |jƒ|jkrštd|j›d|d‚| ¡|  d¡rÊt|jƒ}d     t|jƒ| g¡} t| ƒ}|jd
krî| rî|j| d }n |jrú|j} | ¡} |jst|jt| ƒƒ} nt| ƒ} t| ƒ| _| | |¡|jtjtjfkrrd }|D]}| j  |d¡qVd| _!| j}|  d d¡t"| j#||j ƒt$| j#|j%ƒ|  &| j#¡| '| |¡}| (| |¡| j)dk    oâd|kpâd|k}|ròt*| ƒ| }|r|Vq|j+|f|||||ddœ|    —Ž}t"|j%| |j ƒ| |¡} |VqdS)zBReceives a Response. Returns a generator of Responses or Requests.rNF)Údecode_contentz    Exceeded z  redirects.)r8z//ú:Ú)Úfragment)úContent-Lengthz Content-TypeúTransfer-EncodingÚCookierYrZ)ÚstreamÚtimeoutÚverifyÚcertÚproxiesÚallow_redirects),rFr ÚurlrXÚcopyÚappendÚhistoryÚcontentrrÚ RuntimeErrorÚrawÚreadÚlenÚ max_redirectsrÚcloseÚ
startswithÚjoinrrKÚ_replaceÚgeturlÚnetlocr
r!Úrebuild_methodÚ status_coderÚtemporary_redirectÚpermanent_redirectrBÚpopÚbodyrÚ_cookiesrÚcookiesÚprepare_cookiesÚrebuild_proxiesÚ rebuild_authÚ_body_positionr#Úsend)rDrEÚreqr\r]r^r_r`Úyield_requestsÚadapter_kwargsÚhistrbÚprevious_fragmentÚprepared_requestÚ parsed_rurlÚparsedÚpurged_headersÚheaderrBÚ
rewindabler(r(r,Úresolve_redirectsŸsŠ
 
 
 ÿ
 
 
 þ     ÿÿùø 
z&SessionRedirectMixin.resolve_redirectscCsR|j}|j}d|kr*| |jj|¡r*|d=|jr8t|ƒnd}|dk    rN| |¡dS)zé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.
        Ú AuthorizationN)rBrbrTÚrequestÚ    trust_envr Ú prepare_auth)rDr„r8rBrbÚnew_authr(r(r,r|s ÿz!SessionRedirectMixin.rebuild_authcCsz|j}t|jƒj}t|||jƒ}d|kr.|d=zt||ƒ\}}Wntk
r^d\}}YnX|rv|rvt||ƒ|d<|S)a°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.
 
        :rtype: dict
        zProxy-Authorization)NN)    rBr rbrKr"rrÚKeyErrorr)rDr„r`rBrKÚ new_proxiesÚusernameÚpasswordr(r(r,r{/s  z$SessionRedirectMixin.rebuild_proxiescCsX|j}|jtjkr|dkrd}|jtjkr6|dkr6d}|jtjkrN|dkrNd}||_dS)z‚When being redirected we may want to change the method of the request
        based on certain specs or browser behavior.
        ÚHEADÚGETÚPOSTN)ÚmethodrsrÚ    see_otherÚfoundÚmoved)rDr„r8r—r(r(r,rrLsz#SessionRedirectMixin.rebuild_method)FNTNNF)    Ú__name__Ú
__module__Ú __qualname__rFrTrŠr|r{rrr(r(r(r,r=js$÷
|r=c@sÌeZdZdZdddddddd    d
d d d g Zdd„Zdd„Zdd„Zdd„Zd6dd„Z    dd„Z
dd„Z dd„Z d7d d!„Z d8d"d#„Zd9d$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„ZdS):ÚSessiona€A Requests session.
 
    Provides cookie persistence, connection-pooling, and configuration.
 
    Basic Usage::
 
      >>> import requests
      >>> s = requests.Session()
      >>> s.get('https://httpbin.org/get')
      <Response [200]>
 
    Or as a context manager::
 
      >>> with requests.Session() as s:
      ...     s.get('https://httpbin.org/get')
      <Response [200]>
    rBryÚauthr`ÚhooksÚparamsr^r_Úadaptersr\rrkcCsrtƒ|_d|_i|_tƒ|_i|_d|_d|_d|_    t
|_ d|_ t iƒ|_tƒ|_| dtƒ¡| dtƒ¡dS)NFTzhttps://zhttp://)rrBrŸr`rr r¡r\r^r_rrkrr ryrr¢Úmountr©rDr(r(r,Ú__init__…s
 
zSession.__init__cCs|Sr'r(r¤r(r(r,Ú    __enter__ÃszSession.__enter__cGs | ¡dSr')rl)rDÚargsr(r(r,Ú__exit__ÆszSession.__exit__c Cs²|jpi}t|tjƒst|ƒ}tttƒ|jƒ|ƒ}|j}|jrR|sR|jsRt    |j
ƒ}t ƒ}|j |j  ¡|j
|j|j|jt|j|jtdt|j|jƒt||jƒ|t|j|jƒd
|S)a‹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.
        :rtype: requests.PreparedRequest
        )r3)
r—rbÚfilesÚdataÚjsonrBr¡rŸryr )ryr.r    Ú    CookieJarr rr rŸrr rbrÚpreparer—Úupperr©rªr«r7rBrr¡r<r )rDrŒryÚmerged_cookiesrŸÚpr(r(r,Úprepare_requestÉs8
 
  ÿ
ÿ 
 ôzSession.prepare_requestNTc Cstt| ¡||||pi||pi||| d
}| |¡}| p8i} | |j| | ||¡}|    |
dœ}| |¡|j|f|Ž}|S)aw    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, list of tuples, 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) <timeouts>` 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``. When 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. Setting verify to ``False``
            may be useful during local development or testing.
        :param cert: (optional) if String, path to ssl client cert file (.pem).
            If Tuple, ('cert', 'key') pair.
        :rtype: requests.Response
        )
r—rbrBr©rªr«r¡rŸryr )r]ra)rr®r±Úmerge_environment_settingsrbr/r~)rDr—rbr¡rªrBryr©rŸr]rar`r r\r^r_r«rÚprepÚsettingsÚ send_kwargsrEr(r(r,rŒôs6=ö
ÿþ
zSession.requestcKs| dd¡|jd|f|ŽS)zåSends 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
        raTr•©Ú
setdefaultrŒ©rDrbÚkwargsr(r(r,r9Os z Session.getcKs| dd¡|jd|f|ŽS)zéSends 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
        raTÚOPTIONSr¶r¸r(r(r,ÚoptionsZs zSession.optionscKs| dd¡|jd|f|ŽS)zæSends 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
        raFr”r¶r¸r(r(r,Úheades z Session.headcKs|jd|f||dœ|—ŽS)aÈSends a POST request. Returns :class:`Response` object.
 
        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, 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
        r–)rªr«©rŒ)rDrbrªr«r¹r(r(r,Úpostps z Session.postcKs|jd|fd|i|—ŽS)auSends a PUT request. Returns :class:`Response` object.
 
        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, 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ªr½©rDrbrªr¹r(r(r,Úput}s
z Session.putcKs|jd|fd|i|—ŽS)awSends a PATCH request. Returns :class:`Response` object.
 
        :param url: URL for the new :class:`Request` object.
        :param data: (optional) Dictionary, list of tuples, 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ªr½rÀr(r(r,Úpatch‰s
z Session.patchcKs|jd|f|ŽS)zèSends 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
        ÚDELETEr½r¸r(r(r,Údelete•szSession.deletec KsŠ| d|j¡| d|j¡| d|j¡d|krFt||j|jƒ|d<t|tƒrXt    dƒ‚| 
dd¡}|  d¡}|j }|j |jd}tƒ}|j|f|Ž}tƒ|}    t|    d    |_td
||f|Ž}|jrà|jD]}
t|j|
j|
jƒqÈt|j||jƒ|r|j||f|Ž} d d „| Dƒ} ng} | r:|  d |¡|  
¡}| |_|szz"t|j||fddi|—Žƒ|_Wntk
rxYnX|s†|j|S)zISend a given PreparedRequest.
 
        :rtype: requests.Response
        r\r^r_r`z#You can only send PreparedRequests.raT)rb)Úsecondsr8cSsg|]}|‘qSr(r()r)rEr(r(r,r-Ósz Session.send.<locals>.<listcomp>rr€)r·r\r^r_r"r`rr.rÚ
ValueErrorrvr9r Ú get_adapterrbÚpreferred_clockr~rÚelapsedrrerryrŒrhrŠÚinsertÚnextÚ_nextÚ StopIterationrf) rDrŒr¹rar\r ÚadapterÚstartÚrrÊrEÚgenrer(r(r,r~ŸsL
 
 
 
 ÿ
z Session.sendc
Cs°|jrr|dk    r| d¡nd}t||d}| ¡D]\}}    | ||    ¡q0|dksV|dkrrtj d¡pptj d¡pp|}t||jƒ}t||j    ƒ}t||j
ƒ}t||j ƒ}||||dœS)z^
        Check the environment and merge it with some settings.
 
        :rtype: dict
        NÚno_proxy)rÓTÚREQUESTS_CA_BUNDLEÚCURL_CA_BUNDLE)r`r\r^r_) rr9rr0r·ÚosÚenvironr7r`r\r^r_)
rDrbr`r\r^r_rÓÚ env_proxiesr*r+r(r(r,r²ís   
ÿý    z"Session.merge_environment_settingscCs@|j ¡D]"\}}| ¡ | ¡¡r
|Sq
td|›ƒ‚dS)z~
        Returns the appropriate connection adapter for the given URL.
 
        :rtype: requests.adapters.BaseAdapter
        z&No connection adapters were found for N)r¢r0Úlowerrmr)rDrbÚprefixrÏr(r(r,rÈ s
zSession.get_adaptercCs|j ¡D] }| ¡q
dS)z+Closes all adapters and as such the sessionN)r¢Úvaluesrl)rDr+r(r(r,rlsz Session.closecs>||jˆ<‡fdd„|jDƒ}|D]}|j |¡|j|<q"dS)zwRegisters a connection adapter to a prefix.
 
        Adapters are sorted in descending order by prefix length.
        cs g|]}t|ƒtˆƒkr|‘qSr()rj)r)r*©rÚr(r,r-%sz!Session.mount.<locals>.<listcomp>N)r¢rv)rDrÚrÏÚ keys_to_mover6r(rÜr,r£s
z Session.mountcs‡fdd„ˆjDƒ}|S)Ncsi|]}|tˆ|dƒ“qSr')Úgetattr)r)Úattrr¤r(r,Ú
<dictcomp>+sz(Session.__getstate__.<locals>.<dictcomp>)Ú    __attrs__)rDÚstater(r¤r,Ú __getstate__*szSession.__getstate__cCs"| ¡D]\}}t|||ƒqdSr')r0Úsetattr)rDrârßÚvaluer(r(r,Ú __setstate__.szSession.__setstate__)NNNNNNNTNNNNNN)NN)N)N)r›rœrÚ__doc__rár¥r¦r¨r±rŒr9r»r¼r¾rÁrÃrÅr~r²rÈrlr£rãrær(r(r(r,ržcs^ô>/ï
[  
 
 
 
N ržcCstƒS)aZ
    Returns a :class:`Session` for context-management.
 
    .. deprecated:: 1.0.0
 
        This method has been deprecated since version 1.0.0 and is only kept for
        backwards compatibility. New code should use :class:`~requests.sessions.Session`
        to create a session. This may be removed at a future date.
 
    :rtype: Session
    )ržr(r(r(r,Úsession3s rè)<rçrÖÚsysÚtimeÚ collectionsrÚdatetimerÚ_internal_utilsrr¢rrŸrÚcompatrr    r
r ryr r rrÚ
exceptionsrrrrr rrÚmodelsrrrrÚ status_codesrÚ
structuresrÚutilsrrrrr r!r"r#r$r%ÚplatformÚ perf_counterrÉr7r<r=ržrèr(r(r(r,Ú<module>s8       0   zS