zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
U
¡ý°d€Hã@södZddlZddlZddlZddlmZddlmZmZm    Z    m
Z
m Z z ddl Z Wne k
rlddlZ YnXGdd„dƒZGdd    „d    ƒZd
d „Zd d „Zddd„ZGdd„deƒZGdd„de    jeƒZdd„Zdd„Zdd„Zd dd„Zdd„ZdS)!z­
requests.cookies
~~~~~~~~~~~~~~~~
 
Compatibility code to be able to use `cookielib.CookieJar` with requests.
 
requests.utils imports from here, so be careful with imports.
éNé)Úto_native_string)ÚMorselÚMutableMappingÚ    cookielibÚurlparseÚ
urlunparsec@sŽeZdZdZdd„Zdd„Zdd„Zdd    „Zd
d „Zd d „Z    dd„Z
ddd„Z dd„Z dd„Z dd„Zedd„ƒZedd„ƒZedd„ƒZdS) Ú MockRequestaòWraps a `requests.Request` to mimic a `urllib2.Request`.
 
    The code in `cookielib.CookieJar` expects this interface in order to correctly
    manage cookie policies, i.e., determine whether a cookie can be set, given the
    domains of the request and the cookie.
 
    The original request object is read-only. The client is responsible for collecting
    the new headers via `get_new_headers()` and interpreting them appropriately. You
    probably want `get_cookie_header`, defined below.
    cCs ||_i|_t|jjƒj|_dS©N)Ú_rÚ _new_headersrÚurlÚschemeÚtype)ÚselfÚrequest©rúGd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\requests/cookies.pyÚ__init__#szMockRequest.__init__cCs|jSr
)r©rrrrÚget_type(szMockRequest.get_typecCst|jjƒjSr
)rr r ÚnetlocrrrrÚget_host+szMockRequest.get_hostcCs| ¡Sr
©rrrrrÚget_origin_req_host.szMockRequest.get_origin_req_hostcCsT|jj d¡s|jjSt|jjddd}t|jjƒ}t|j||j|j    |j
|j gƒS)NÚHostzutf-8)Úencoding) r ÚheadersÚgetr rrrrÚpathÚparamsÚqueryÚfragment)rÚhostÚparsedrrrÚ get_full_url1s úÿzMockRequest.get_full_urlcCsdS©NTrrrrrÚis_unverifiableEszMockRequest.is_unverifiablecCs||jjkp||jkSr
)r rr ©rÚnamerrrÚ
has_headerHszMockRequest.has_headerNcCs|jj ||j ||¡¡Sr
)r rrr )rr)ÚdefaultrrrÚ
get_headerKszMockRequest.get_headercCs tdƒ‚dS)zMcookielib has no legitimate use for this method; add it back if you find one.z=Cookie headers should be added with add_unredirected_header()N)ÚNotImplementedError)rÚkeyÚvalrrrÚ
add_headerNsÿzMockRequest.add_headercCs||j|<dSr
©r ©rr)ÚvaluerrrÚadd_unredirected_headerTsz#MockRequest.add_unredirected_headercCs|jSr
r1rrrrÚget_new_headersWszMockRequest.get_new_headerscCs| ¡Sr
)r'rrrrÚ unverifiableZszMockRequest.unverifiablecCs| ¡Sr
)rrrrrÚorigin_req_host^szMockRequest.origin_req_hostcCs| ¡Sr
rrrrrr#bszMockRequest.host)N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rrrrr%r'r*r,r0r4r5Úpropertyr6r7r#rrrrr    s$ 
 
 
r    c@s(eZdZdZdd„Zdd„Zdd„ZdS)    Ú MockResponsezÀWraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`.
 
    ...what? Basically, expose the parsed HTTP headers from the server response
    the way `cookielib` expects to see them.
    cCs
||_dS)z†Make a MockResponse for `cookielib` to read.
 
        :param headers: a httplib.HTTPMessage or analogous carrying the headers
        N©Ú_headers)rrrrrrnszMockResponse.__init__cCs|jSr
r>rrrrÚinfouszMockResponse.infocCs|j |¡dSr
)r?Ú
getheadersr(rrrrAxszMockResponse.getheadersN)r8r9r:r;rr@rArrrrr=gsr=cCs8t|dƒr|jsdSt|ƒ}t|jjƒ}| ||¡dS)zìExtract the cookies from the response into a CookieJar.
 
    :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
    :param request: our own requests.Request object
    :param response: urllib3.HTTPResponse object
    Ú_original_responseN)ÚhasattrrBr    r=ÚmsgÚextract_cookies)ÚjarrÚresponseÚreqÚresrrrÚextract_cookies_to_jar|s
 rJcCs t|ƒ}| |¡| ¡ d¡S)zj
    Produce an appropriate Cookie header string to be sent with `request`, or None.
 
    :rtype: str
    ÚCookie)r    Úadd_cookie_headerr5r)rFrÚrrrrÚget_cookie_headerŒs
rNcCszg}|D]N}|j|krq|dk    r,||jkr,q|dk    r@||jkr@q| |j|j|jf¡q|D]\}}}| |||¡q\dS)zkUnsets a cookie by name, by default over all domains and paths.
 
    Wraps CookieJar.clear(), is O(n).
    N)r)ÚdomainrÚappendÚclear)Ú    cookiejarr)rOrÚ
clearablesÚcookierrrÚremove_cookie_by_name—s
rUc@seZdZdZdS)ÚCookieConflictErrorz£There are two cookies that meet the criteria specified in the cookie jar.
    Use .get and .set and include domain and path args in order to be more specific.
    N)r8r9r:r;rrrrrVªsrVcsèeZdZdZd3dd„Zdd„Zdd„Zd    d
„Zd d „Zd d„Z    dd„Z
dd„Z dd„Z dd„Z dd„Zd4dd„Z‡fdd„Zdd„Zdd „Zd!d"„Z‡fd#d$„Z‡fd%d&„Zd5d'd(„Zd6d)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Z‡ZS)7ÚRequestsCookieJara±Compatibility class; is a cookielib.CookieJar, but exposes a dict
    interface.
 
    This is the CookieJar we create by default for requests and sessions that
    don't specify one, since some clients may expect response.cookies and
    session.cookies to support dict operations.
 
    Requests does not use the dict interface internally; it's just for
    compatibility with external client code. All requests code should work
    out of the box with externally provided instances of ``CookieJar``, e.g.
    ``LWPCookieJar`` and ``FileCookieJar``.
 
    Unlike a regular CookieJar, this class is pickleable.
 
    .. warning:: dictionary operations that are normally O(1) may be O(n).
    NcCs.z| |||¡WStk
r(|YSXdS)zãDict-like get() that also supports optional domain and path args in
        order to resolve naming collisions from using one cookie jar over
        multiple domains.
 
        .. warning:: operation is O(n), not O(1).
        N)Ú_find_no_duplicatesÚKeyError)rr)r+rOrrrrrÂszRequestsCookieJar.getcKsX|dkr(t||| d¡| d¡ddSt|tƒr<t|ƒ}nt||f|Ž}| |¡|S)z°Dict-like set() that also supports optional domain and path args in
        order to resolve naming collisions from using one cookie jar over
        multiple domains.
        NrOr)rOr)rUrÚ
isinstancerÚmorsel_to_cookieÚ create_cookieÚ
set_cookie)rr)r3ÚkwargsÚcrrrÚsetÎsÿ
 
 
zRequestsCookieJar.setccst|ƒD] }|jVqdS)z”Dict-like iterkeys() that returns an iterator of names of cookies
        from the jar.
 
        .. seealso:: itervalues() and iteritems().
        N)Úiterr)©rrTrrrÚiterkeysás zRequestsCookieJar.iterkeyscCs t| ¡ƒS)zƒDict-like keys() that returns a list of names of cookies from the
        jar.
 
        .. seealso:: values() and items().
        )ÚlistrcrrrrÚkeysêszRequestsCookieJar.keysccst|ƒD] }|jVqdS)z•Dict-like itervalues() that returns an iterator of values of cookies
        from the jar.
 
        .. seealso:: iterkeys() and iteritems().
        N)rar3rbrrrÚ
itervaluesòs zRequestsCookieJar.itervaluescCs t| ¡ƒS)z„Dict-like values() that returns a list of values of cookies from the
        jar.
 
        .. seealso:: keys() and items().
        )rdrfrrrrÚvaluesûszRequestsCookieJar.valuesccs t|ƒD]}|j|jfVqdS)z•Dict-like iteritems() that returns an iterator of name-value tuples
        from the jar.
 
        .. seealso:: iterkeys() and itervalues().
        N)rar)r3rbrrrÚ    iteritemss zRequestsCookieJar.iteritemscCs t| ¡ƒS)zõDict-like items() that returns a list of name-value tuples from the
        jar. Allows client-code to call ``dict(RequestsCookieJar)`` and get a
        vanilla python dict of key value pairs.
 
        .. seealso:: keys() and values().
        )rdrhrrrrÚitems szRequestsCookieJar.itemscCs,g}t|ƒD]}|j|kr | |j¡q |S)z2Utility method to list all the domains in the jar.©rarOrP©rÚdomainsrTrrrÚ list_domainss
 
zRequestsCookieJar.list_domainscCs,g}t|ƒD]}|j|kr | |j¡q |S)z0Utility method to list all the paths in the jar.)rarrP)rÚpathsrTrrrÚ
list_pathss
 
zRequestsCookieJar.list_pathscCs<g}t|ƒD]*}|jdk    r*|j|kr*dS| |j¡q dS)zvReturns True if there are multiple domains in the jar.
        Returns False otherwise.
 
        :rtype: bool
        NTFrjrkrrrÚmultiple_domains%s  z"RequestsCookieJar.multiple_domainscCsFi}t|ƒD]4}|dks"|j|kr |dks4|j|kr |j||j<q |S)z¾Takes as an argument an optional domain and path and returns a plain
        old Python dict of name-value pairs of cookies that meet the
        requirements.
 
        :rtype: dict
        N)rarOrr3r))rrOrÚ
dictionaryrTrrrÚget_dict2s ÿÿzRequestsCookieJar.get_dictcs*ztƒ |¡WStk
r$YdSXdSr&)ÚsuperÚ __contains__rVr(©Ú    __class__rrrtAszRequestsCookieJar.__contains__cCs
| |¡S)zÿDict-like __getitem__() for compatibility with client code. Throws
        exception if there are more than one cookie with name. In that case,
        use the more explicit get() method instead.
 
        .. warning:: operation is O(n), not O(1).
        )rXr(rrrÚ __getitem__GszRequestsCookieJar.__getitem__cCs| ||¡dS)zÓDict-like __setitem__ for compatibility with client code. Throws
        exception if there is already a cookie of that name in the jar. In that
        case, use the more explicit set() method instead.
        N)r`r2rrrÚ __setitem__PszRequestsCookieJar.__setitem__cCst||ƒdS)zlDeletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
        ``remove_cookie_by_name()``.
        N)rUr(rrrÚ __delitem__WszRequestsCookieJar.__delitem__csHt|jdƒr4|j d¡r4|j d¡r4|j dd¡|_tƒj|f|ž|ŽS)NÚ
startswithú"z\"Ú)rCr3rzÚendswithÚreplacersr])rrTÚargsr^rurrr]]s
ÿ
þ
ýzRequestsCookieJar.set_cookiecs8t|tjƒr(|D]}| t |¡¡qn tƒ |¡dS)zAUpdates this jar with cookies from another CookieJar or dict-likeN)rZrÚ    CookieJarr]ÚcopyrsÚupdate)rÚotherrTrurrr‚fs zRequestsCookieJar.updatecCsdt|ƒD]<}|j|kr|dks(|j|kr|dks:|j|kr|jSqtd|›d|›d|›ƒ‚dS)aÔRequests uses this method internally to get cookie values.
 
        If there are conflicting cookies, _find arbitrarily chooses one.
        See _find_no_duplicates if you want an exception thrown if there are
        conflicting cookies.
 
        :param name: a string containing name of cookie
        :param domain: (optional) string containing domain of cookie
        :param path: (optional) string containing path of cookie
        :return: cookie.value
        Núname=ú    , domain=ú, path=)rar)rOrr3rY)rr)rOrrTrrrÚ_findns 
 zRequestsCookieJar._findcCs‚d}t|ƒD]N}|j|kr |dks,|j|kr |dks>|j|kr |dk    rTtd|›ƒ‚|j}q |rd|Std|›d|›d|›ƒ‚dS)aùBoth ``__get_item__`` and ``get`` call this function: it's never
        used elsewhere in Requests.
 
        :param name: a string containing name of cookie
        :param domain: (optional) string containing domain of cookie
        :param path: (optional) string containing path of cookie
        :raises KeyError: if cookie is not found
        :raises CookieConflictError: if there are multiple cookies
            that match name and optionally domain and path
        :return: cookie.value
        Nz&There are multiple cookies with name, r„r…r†)rar)rOrrVr3rY)rr)rOrÚtoReturnrTrrrrX‚s  
ÿz%RequestsCookieJar._find_no_duplicatescCs|j ¡}| d¡|S)ú4Unlike a normal CookieJar, this class is pickleable.Ú _cookies_lock)Ú__dict__rÚpop©rÚstaterrrÚ __getstate__Ÿs
 
zRequestsCookieJar.__getstate__cCs$|j |¡d|jkr t ¡|_dS)r‰rŠN)r‹r‚Ú    threadingÚRLockrŠrrrrÚ __setstate__¦s 
zRequestsCookieJar.__setstate__cCs"tƒ}| | ¡¡| |¡|S)z(Return a copy of this RequestsCookieJar.)rWÚ
set_policyÚ
get_policyr‚)rÚnew_cjrrrr¬s
zRequestsCookieJar.copycCs|jS)z&Return the CookiePolicy instance used.)Ú_policyrrrrr”³szRequestsCookieJar.get_policy)NNN)NN)NN)NN)r8r9r:r;rr`rcrerfrgrhrirmrorprrrtrwrxryr]r‚r‡rXrr’rr”Ú __classcell__rrrurrW°s2
                
           
 
rWcCsN|dkr dSt|dƒr| ¡St |¡}| ¡|D]}| t |¡¡q4|S)Nr)rCrrQr])rFÚnew_jarrTrrrÚ_copy_cookie_jar¸s
 
r™cKs¢d||ddddddddddiddœ }t|ƒt|ƒ}|rJtd    t|ƒ›ƒ‚| |¡t|d
ƒ|d <t|d ƒ|d <|d  d¡|d<t|dƒ|d<tjf|ŽS)zÊMake a cookie from underspecified parameters.
 
    By default, the pair of `name` and `value` will be set for the domain ''
    and sent on every request (this is sometimes called a "supercookie").
    rNr|ú/FTÚHttpOnly) Úversionr)r3ÚportrOrÚsecureÚexpiresÚdiscardÚcommentÚ comment_urlÚrestÚrfc2109z2create_cookie() got unexpected keyword arguments: rÚport_specifiedrOÚdomain_specifiedÚ.Údomain_initial_dotrÚpath_specified)r`Ú    TypeErrorrdr‚ÚboolrzrrK)r)r3r^ÚresultÚbadargsrrrr\Çs2ó ÿ
r\cCsÈd}|drTztt ¡t|dƒƒ}Wqvtk
rPtd|d›dƒ‚YqvXn"|drvd}t t |d|¡¡}t|dt|dƒd|d    ||j    |d
dd |d idt|d ƒ|j
|dpÂdd S)zBConvert a Morsel object into a Cookie containing the one k/v pair.Nzmax-agez    max-age: z must be integerrŸz%a, %d-%b-%Y %H:%M:%S GMTr¡FrOrr›Úhttponlyržrœr) r¡r¢r rOrŸr)rrr£r¤ržr3rœ) ÚintÚtimeÚ
ValueErrorrªÚcalendarÚtimegmÚstrptimer\r«r.r3)ÚmorselrŸÚ time_templaterrrr[ìs0
 
 
 
ór[TcCsR|dkrtƒ}|dk    rNdd„|Dƒ}|D]$}|s8||kr(| t|||ƒ¡q(|S)aCReturns a CookieJar from a key/value dictionary.
 
    :param cookie_dict: Dict of key/values to insert into CookieJar.
    :param cookiejar: (optional) A cookiejar to add the cookies to.
    :param overwrite: (optional) If False, will not replace cookies
        already in the jar with new ones.
    :rtype: CookieJar
    NcSsg|]
}|j‘qSr)r))Ú.0rTrrrÚ
<listcomp>sz'cookiejar_from_dict.<locals>.<listcomp>)rWr]r\)Ú cookie_dictrRÚ    overwriteÚnames_from_jarr)rrrÚcookiejar_from_dict    s     r¼cCsvt|tjƒstdƒ‚t|tƒr.t||dd}nDt|tjƒrrz| |¡Wn(tk
rp|D]}| |¡q\YnX|S)zÒAdd cookies to cookiejar and returns a merged CookieJar.
 
    :param cookiejar: CookieJar object to add the cookies to.
    :param cookies: Dictionary or CookieJar object to be added.
    :rtype: CookieJar
    z!You can only merge into CookieJarF)rRrº)    rZrr€r±Údictr¼r‚ÚAttributeErrorr])rRÚcookiesÚ cookie_in_jarrrrÚ merge_cookiess 
 rÁ)NN)NT)r;r²rr°Ú_internal_utilsrÚcompatrrrrrrÚ ImportErrorZdummy_threadingr    r=rJrNrUÚ RuntimeErrorrVr€rWr™r\r[r¼rÁrrrrÚ<module>s.      P

%