"L i^dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZmZddlmZddlmZddlmZmZmZmZdd lmZmZmZmZmZmZm Z dd lm!Z"dd lm#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)dd l*m+Z+dd l,m-Z-m.Z.m/Z/m0Z0ddl1m2Z2dZ3ejhZ5dddZ6djoejpdeddZ9ejtdk(rdZ;dZ#dZdZ?dZ@ejd ZBd!ZCd"ZDd#ZEd$ZFdHd%ZGd&ZHd'ZId(ZJd)ZKd*ZLd+ZMd,ZNd-ZOePd.ZQd/ZRd0ZSd1ZTd2ZUd3ZVd4ZWejd5ZXd6ZYdId7ZZd8Z[dJd9Z\dKd:Z]d;Z^d<Z_d=jd>Zaead?zZbead@zZcdAZddBZedCZfdDZgdEZhdFZidGZjy)Lz requests.utils ~~~~~~~~~~~~~~ This module provides utility functions that are used within Requests that are also useful for external consumption. N) OrderedDict) make_headers parse_url)certs __version__)_HEADER_VALIDATORS_BYTE_HEADER_VALIDATORS_STRHEADER_VALIDATORSto_native_string)Mapping basestringbytes getproxiesgetproxies_environment integer_types is_urllib3_1)parse_http_list) proxy_bypassproxy_bypass_environmentquotestrunquoteurlparse urlunparsecookiejar_from_dict)FileModeWarning InvalidHeader InvalidURLUnrewindableBodyError)CaseInsensitiveDict)z.netrc_netrcPi)httphttpsz, z,\s*T)accept_encodingzaccept-encodingwin32c ddl} |j|jd}t |j |dd}|j |dd}|r|sy|jd}td|}|D]j}|dk(rd|vry |jdd }|jd d }|jd d}tj||tjsjy y#t$rYywxYw#t tf$rYywxYw)NrFz;Software\Microsoft\Windows\CurrentVersion\Internet Settings ProxyEnable ProxyOverride;z.Tz\.*z.*?)winreg ImportErrorOpenKeyHKEY_CURRENT_USERint QueryValueExOSError ValueErrorsplitfilterreplacerematchI)hostr1internetSettings proxyEnable proxyOverridetests T/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/requests/utils.pyproxy_bypass_registryrEMs%   %~~((N   f112BMRSTUVK"//0@/RSTUM- &++C0 t]3 ! Dy d?<<U+D<<U+D<<T*DxxdBDD) C  $  s#C(AC7( C43C47D D cBtr t|St|S)zReturn True, if the host should be bypassed. Checks proxy settings gathered from the environment, if specified, or the registry. )rrrE)r?s rDrrss " #+D1 1(. .c>t|dr|j}|S)z/Returns an internal sequence dictionary update.items)hasattrrI)ds rDdict_to_sequencerLsq' GGI HrGcd}d}ts!t|tr|jd}t |dr t |}n~t |dr |j }net |drY |j }tj|j}d|jvrtjdt t |drW |j#}t |d r:|8 |j%dd |j#}|j%|xsd|d}t)d||z S#tjt f$rYwxYw#t&$rd}Y?wxYw#t&$r||}YRwxYw) Nrutf-8__len__lenfilenoba%Requests has determined the content-length for this request using the binary size of the file: however, the file has been opened in text mode (i.e. without the 'b' flag in the mode). This may lead to an incorrect content-length. In Requests 3.0, support will be removed for files in text mode.tellseek)r isinstancerencoderJrPrQosfstatst_sizemodewarningswarnrioUnsupportedOperationAttributeErrorrSrTr7max)o total_lengthcurrent_positionrQs rD super_lenresyL Jq#. HHW q)1v E uu H  XXZF88F+33L!&&  2$ q& % vvx q&!l&: %FF1aL#$668LFF+0q1 q,!11 22e''8    X%#$L%% 0 '#/  0s6*D8E(.7E8EE E%$E%( E87E8ctjjd}||f}n dtD} ddlm}m}d}|D]D}tj j|}tj j|sB|}n|yt|} | j} ||j| } | r| drdnd} | | | dfSy#|tf$r|rYywxYw#ttf$rYywxYw)z;Returns the Requests tuple auth for a given url from netrc.NETRCNc3&K|] }d| yw)z~/N).0fs rD z!get_netrc_auth..s9Rs89sr)NetrcParseErrornetrcrrU)rXenvironget NETRC_FILESrnrmpath expanduserexistsrhostnameauthenticatorsr7r2r`) url raise_errors netrc_filenetrc_locationsrmrn netrc_pathrklocrir?r$login_is rDget_netrc_authrs(J%-9[9  0   A''$$Q'Cww~~c"       c]{{ :&55d;F%ay!aw33 )    (   s<A C#C# C#"+CC C#C  C##C54C5ct|dd}|rBt|tr1|ddk7r(|ddk7rtjj |Syyyy)z0Tries to guess the filename of the given object.nameNr<>)getattrrVrrXrrbasename)objrs rDguess_filenamersT 3 %D 4,aCDHPSOww%%ET,trGcRtjj|r|Stjj|\}}|rytjj|sZtjj|\}}|sn5dj ||g}|r tjj|sZt j |s|St j|}||jvr|Stj}tjj ||jdd}tjj|s6t|5}|j|j|ddd|S|S#1swY|SxYw)zReplace nonexistent paths that look like they refer to a member of a zip archive with the location of an extracted copy of the target, or else just return the provided path unchanged. /rN)rXrrrtr9joinzipfile is_zipfileZipFilenamelisttempfile gettempdir atomic_openwriteread)rrarchivememberprefixzip_filetmpextracted_path file_handlers rDextract_zipped_pathsrsE  ww~~d ggmmD)OGV "''..1''--0 66*+ "''..1   g & w'H X&&((     CWW\\#v||C'8'<=N 77>>. )  ( 6L   x}}V4 5 6 > 6 s /!FF&c#JKtjtjj |\}} tj |d5}|dddtj ||y#1swY xYw#t$rtj|wxYww)z-Write a file to the disk in an atomic fashion)dirwbN) rmkstemprXrrdirnamefdopenr; BaseExceptionremove)filenametmp_descriptortmp_name tmp_handlers rDrr's (//BGGOOH4MNNH YY~t ,     8X&    ( s47B#BA4B3B#4A=9B B  B#ct|yt|ttttfr t dt |S)aTake an object and test to see if it can be represented as a dictionary. Unless it can not be represented as such, return an OrderedDict, e.g., :: >>> from_key_val_list([('key', 'val')]) OrderedDict([('key', 'val')]) >>> from_key_val_list('string') Traceback (most recent call last): ... ValueError: cannot encode objects that are not 2-tuples >>> from_key_val_list({'key': 'val'}) OrderedDict([('key', 'val')]) :rtype: OrderedDict N+cannot encode objects that are not 2-tuples)rVrrboolr5r8rvalues rDfrom_key_val_listr4s6$ }%#udC01FGG u rGc|yt|ttttfr t dt|t r|j}t|S)aTake an object and test to see if it can be represented as a dictionary. If it can be, return a list of tuples, e.g., :: >>> to_key_val_list([('key', 'val')]) [('key', 'val')] >>> to_key_val_list({'key': 'val'}) [('key', 'val')] >>> to_key_val_list('string') Traceback (most recent call last): ... ValueError: cannot encode objects that are not 2-tuples :rtype: list Nr) rVrrrr5r8rrIlistrs rDto_key_val_listrOsJ" }%#udC01FGG%!  ;rGcg}t|D]5}|dd|ddcxk(rdk(rnnt|dd}|j|7|S)aParse lists as described by RFC 2068 Section 2. In particular, parse comma-separated lists where the elements of the list may include quoted-strings. A quoted-string could contain a comma. A non-quoted string could have quotes in the middle. Quotes are removed automatically after parsing. It basically works like :func:`parse_set_header` just that items may appear multiple times and case sensitivity is preserved. The return value is a standard :class:`list`: >>> parse_list_header('token, "quoted value"') ['token', 'quoted value'] To create a header from the :class:`list` again, use the :func:`dump_header` function. :param value: a string with a list header. :return: :class:`list` :rtype: list Nrr")_parse_list_headerunquote_header_valueappend)rresultitems rDparse_list_headerrmsY.F"5) 8tBCy 'C ''Qr 3D d MrGci}t|D]H}d|vrd||< |jdd\}}|dd|ddcxk(rdk(rnnt|dd}|||<J|S)a^Parse lists of key, value pairs as described by RFC 2068 Section 2 and convert them into a python dict: >>> d = parse_dict_header('foo="is a fish", bar="as well"') >>> type(d) is dict True >>> sorted(d.items()) [('bar', 'as well'), ('foo', 'is a fish')] If there is no value for a key it will be `None`: >>> parse_dict_header('key_without_value') {'key_without_value': None} To create a header from the :class:`dict` again, use the :func:`dump_header` function. :param value: a string with a dict header. :return: :class:`dict` :rtype: dict =Nrrr)rr9r)rrrrs rDparse_dict_headerrs},F"5) d?F4L jja( e !9bc )c )(q5Et  MrGc|rF|d|dcxk(rdk(r5n|S|dd}|r|dddk7r"|jddjd dS|S) zUnquotes a header value. (Reversal of :func:`quote_header_value`). This does not use the real unquoting but what browsers are actually using for quoting. :param value: the header value to unquote. :rtype: str rrrrNrUz\\\z\")r;)r is_filenames rDrrsd qU2Y-#- La eBQi61==.66ucB B LrGcX|Dcic]}|j|j}}|Scc}w)zReturns a key/value dictionary from a CookieJar. :param cj: CookieJar object to extract cookies from. :rtype: dict )rr)cjcookie cookie_dicts rDdict_from_cookiejarrs/<>>6;; ,>K> ?s'ct||S)zReturns a CookieJar from a key/value dictionary. :param cj: CookieJar to insert cookies into. :param cookie_dict: Dict of key/values to insert into CookieJar. :rtype: CookieJar r)rrs rDadd_dict_to_cookiejarrs {B //rGc^tjdttjdtj }tjdtj }tjd}|j ||j |z|j |zS)zlReturns encodings from given content string. :param content: bytestring to extract encodings from. zIn requests 3.0, get_encodings_from_content will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)z!])flagsz+]z$^<\?xml.*?encoding=["\']*(.+?)["\'>])r\r]DeprecationWarningr<compiler>findall)content charset_re pragma_rexml_res rDget_encodings_from_contentrs  MM 1  @MJ IQSQUQUVI ZZ? @F 7#   G $ % .. ! "rGcH|jd}|dj|dd}}i}d}|D]m}|j}|s|d}}|jd} | dk7r+|d| j|}|| dzdj|}|||j<o||fS) zReturns content type and parameters from given header :param header: string :return: tuple containing content type and dictionary of parameters r-rrNz"' Trr)r9stripfindlower) headertokens content_typeparams params_dictitems_to_stripparamkeyrindex_of_equalss rD_parse_content_type_headerrs\\# F!!9??,fQRj&LKN-  C#jjoO"$,_-33NCo134::>J',K $-  $$rGc|jd}|syt|\}}d|vr|djdSd|vryd|vryy) z}Returns encodings from given HTTP Header Dict. :param headers: dictionary to extract encoding from. :rtype: str z content-typeNcharsetz'"textz ISO-8859-1zapplication/jsonrN)rprr)headersrrs rDget_encoding_from_headersrs`;;~.L 5lCL&Fi &&u-- \)*rGc#K|j |Ed{ytj|jd}|D]}|j|}|s||jdd}|r|yy7ew)zStream decodes an iterator.Nr;errorsrGT)final)encodingcodecsgetincrementaldecoderdecode)iteratorrdecoderchunkrvs rDstream_decode_response_unicoder*s zz6f**1::6iHG ^^E " H 4 (B   sA<A:AA<"A<c#Kd}||dkr t|}|t|kr||||z||z }|t|kryyw)z Iterate over slices of a string.rN)rP)string slice_lengthposs rD iter_slicesr;sW C|q06{ F S3-.. | F s AAAc>tjdtg}t|j}|r t |j |S t |j |dS#t$r|j|Y5wxYw#t$r|j cYSwxYw)zReturns the requested content back in unicode. :param r: Response object to get unicode content from. Tried: 1. charset from content-type 2. fall back and replace all unicode characters :rtype: str zIn requests 3.0, get_unicode_from_response will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)r;r) r\r]rrrrr UnicodeErrorr TypeError)rtried_encodingsrs rDget_unicode_from_responserEs MM 1  O)3H -qyy(+ + 199hy99  -  " "8 , - yys#A$ B$BBBBzBABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~c|jd}tdt|D]l}||dd}t|dk(rI|jr9 t t |d}|tvr|||ddz||<Vd||||<bd||||<nd j|S#t $rtd|dwxYw) zUn-escape any percent-escape sequences in a URI that are unreserved characters. This leaves all reserved, illegal and non-ASCII bytes encoded. :rtype: str %rrrUz"Invalid percent-escape sequence: ''N) r9rangerPisalnumchrr5r8r!UNRESERVED_SETr)uripartsihcs rDunquote_unreservedr rs IIcNE 1c%j ! & !HQqM q6Q;199; LAr ON"uQx|+auQxj>a58*~E!H & 775> L #EaS!JKK Ls B''Ccpd}d} tt||S#t$rt||cYSwxYw)zRe-quote the given URI. This function passes the given URI through an unquote/quote cycle to ensure that it is fully and consistently quoted. :rtype: str z!#$%&'()*+,/:;=?@[]~z!#$&'()*+,/:;=?@[]~)safe)rr r!)rsafe_with_percentsafe_without_percents rD requote_urirsJ/0 5',3DEE 5S344 5s 55c rtjdtj|d}|j d\}}tjdtjt t |d}tjdtj|d|z}||z||zk(S)zThis function allows you to check if an IP belongs to a network subnet Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24 returns False if ip = 192.168.1.1 and net = 192.168.100.0/24 :rtype: bool z=Lrr)structunpacksocket inet_atonr9dotted_netmaskr5)ipnetipaddrnetaddrbitsnetmasknetworks rDaddress_in_networkrs]]4!1!1"!5 6q 9FIIcNMGTmmD&"2"2>#d)3L"MNqQGmmD&"2"27";I)r inet_ntoarpack)maskrs rDrrs7 b4i1, ,D   FKKd3 44rGcN tj|y#t$rYywxYw)z :rtype: bool FT)rrr7) string_ips rDis_ipv4_addressr%s.#  s  $$c |jddk(rR t|jdd}|dks|dkDry t j |jddyy#t$rYywxYw#t $rYywxYw)zV Very simple check of the cidr format in no_proxy variable. :rtype: bool rrFrrT)countr5r9r8rrr7)string_networkr"s rD is_valid_cidrr)s C A% ~++C034D !8tby    ^11#6q9 :     s"A('A7( A43A47 BBc#8K|du}|r2tjj|}|tj|< d|r(tj|=y|tj|<yy#|r(tj|=w|tj|<wwxYww)zSet the environment variable 'env_name' to 'value' Save previous value, yield, and then restore the previous value stored in the environment variable 'env_name'. If 'value' is None, do nothingN)rXrorp)env_namer value_changed old_values rD set_environr.s%MJJNN8, $ 81  JJx('0 8$ = JJx('0 8$ s9BA++B+,BBcd}|}||d}t|}|jy|rd|jddjdD}t |jr=|D]7}t |rt |j|s%y|j|k(s7yn_|j}|jr|d |jz }|D]0}|jj|s|j|s0ytd|5 t|j}dddryy #ttjf$rd }Y)wxYw#1swY.xYw) zL Returns whether we should bypass proxies or not. :rtype: bool ctjj|xs-tjj|jSN)rXrorpupper)rs rD get_proxyz(should_bypass_proxies..get_proxys-zz~~c"AbjjnnSYY[&AArGNno_proxyTc3&K|] }|s| ywr1ri)rjr?s rDrlz(should_bypass_proxies.. sRTTDRs r,:F)rrur;r9r%r)rportendswithr.rrrgaierror) rwr4r3 no_proxy_argparsedproxy_iphost_with_portr?bypasss rDshould_bypass_proxiesrAsZB LZ( c]F SX%5%5c2%>%D%DS%IR 6?? +$  *)&//8D#__0  $__N{{Afkk]"33  ??++D1^5L5LT5R  Z . !&//2F  6??+ F  s*ED22E EEEEc4t||riStS)zA Return a dict of environment proxies. :rtype: dict r4)rAr)rwr4s rDget_environ_proxiesrD0s S84 |rGc.|xsi}t|}|j+|j|j|jdS|jdz|jz|jd|jzdg}d}|D]}||vs||}|S|S)zSelect a proxy for the url, if applicable. :param url: The url being for the request :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs Nallz://zall://)rrurpscheme)rwproxiesurlparts proxy_keysproxy proxy_keys rD select_proxyrM<s mG}H {{8??GKK,>?? %("3"338$$$ J E  I&E  L  LrGc8||ni}|j}t|j}|jd}|j }|rOt ||sBt ||}|j||jd}|r|j|||S)aThis method takes proxy information from a request and configuration input to resolve a mapping of target proxies. This will consider settings such as NO_PROXY to strip proxy configurations. :param request: Request or PreparedRequest :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs :param trust_env: Boolean declaring whether to trust environment configs :rtype: dict r4rCrF)rwrrGrpcopyrArD setdefault) requestrH trust_envrwrGr4 new_proxiesenviron_proxiesrKs rDresolve_proxiesrUVs!,g"G ++C c] ! !F{{:&H,,.K.sXF-cHE##FO,?,?,FG   " "65 1 rGc|dtS)zO Return a string representing the default user agent. :rtype: str rr)rs rDdefault_user_agentrWqs V1[M ""rGc:tttdddS)z9 :rtype: requests.structures.CaseInsensitiveDict z*/*z keep-alive)z User-AgentzAccept-EncodingAccept Connection)r#rWDEFAULT_ACCEPT_ENCODINGrirGrDdefault_headersr\zs% ,.6&   rGcg}d}|j|}|s|Stjd|D]} |jdd\}}d|jdi}|jdD]:} |jd\}}|j|||j|<<|j ||S#t$r|d}}YwxYw#t$rY4wxYw) zReturn a list of parsed link headers proxies. i.e. Link: ; rel=front; type="image/jpeg",; rel=back;type="image/jpeg" :rtype: list z '"z, * '"r)rr<r9r8r) rlinks replace_charsvalrwrlinkrrs rDparse_header_linksrbs EM KK &E  xx& "))C+KCsyy*+\\#& HE "[[- U.3[[-GD=) *  H  T!$ L "rC "  s#B?1C? CC CCasciirUc|dd}|tjtjfvry|ddtjk(ry|ddtjtj fvry|j t}|dk(ry |dk(r |dddtk(ry |d ddtk(ry |dk(r|ddtk(ry |d dtk(ryy)z :rtype: str Nzutf-32rez utf-8-sigrUzutf-16rrNz utf-16-berz utf-16-lez utf-32-bez utf-32-le) r BOM_UTF32_LE BOM_UTF32_BEBOM_UTF8 BOM_UTF16_LE BOM_UTF16_BEr'_null_null2_null3)datasample nullcounts rDguess_json_utfrss"1XF &%%v':':;; bqzV__$ bqzf))6+>+>?? U#IA~A~ #A#;&  !$Q$<6 !A~ "1:  !":  rGct|}|\}}}}}}} |j} | s|| }} |rdj|| g} ||}|d}t|| |d|| fS)zGiven a URL that may or may not have a scheme, prepend the given scheme. Does not replace a present scheme with the one provided as an argument. :rtype: str @r)rnetlocrr) rw new_schemer=rGauthr?r9rrqueryfragmentrvs rDprepend_scheme_if_neededr{s{ s^F6<3FD$dE8 ]]F V 4.) ~ | vvtRA BBrGct|} t|jt|jf}|S#tt f$rd}Y|SwxYw)z{Given a url with authentication components, extract them into a tuple of username,password. :rtype: (str,str) )rr)rrusernamepasswordr`r)rwr=rxs rDget_auth_from_urlrsU c]F('&//*BC K I & Ks*9A AcB|\}}t||dt||dy)zVerifies that header parts don't contain leading whitespace reserved characters, or return characters. :param header: tuple, in the format (name, value). rrN)_validate_header_part)rrrs rDcheck_header_validityrs% KD%&$*&%+rGc t|tr t|}n7t|tr t|}nt d|d|dt ||j|s|dk(rdnd}t d|d|y) Nz Header part (z) from z# must be of type str or bytes, not rrrzTInvalid leading whitespace, reserved character(s), or return character(s) in header z: )rVrr rr r typer=)r header_partheader_validator_index validator header_kinds rDrr s+s#*+AB K '+,BC K?'&:115k1B0C E  ??; ' 6! ;f &&1]"[O E   (rGc~t|\}}}}}}|s||}}|jddd}t|||||dfS)zW Given a url remove the fragment and the authentication part. :rtype: str rurrr)rrsplitr)rwrGrvrrrryrzs rD urldefragauthrsU 5=SM1FFD&% V ]]3 "2 &F vvtVUB? @@rGct|jdd}|.t|jtr ||jyt d#t $r t dwxYw)zfMove file pointer back to its recorded starting position so it can be read again on redirect. rTNz;An error occurred when rewinding request body for redirect.z+Unable to rewind request body for redirect.)rbodyrV_body_positionrr7r")prepared_request body_seeks rD rewind_bodyr/st(--vtrs  #0$ :) ," $c* )) BHHWl489JKL <<7$L / D3N) X&"J   6<@F202%22"$PM 05, 550 11*:z 46# "L  g  @C:  , &A"SrG