U e5d!@sddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*z ddl+Z+Wne,k rdZ-YnXdZ-ddd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'g!Z.d(e j/dd)Z0da1de j2fddddd*d+dZ3d,dZ4gZ5d}d-d$Z6d.d%Z7e 8d/e j9Z:d0d1Z;Gd2ddZGd5d d Z?Gd6dde?Z@Gd7d d e?ZAGd8d d e?ZBd9d:ZCGd;d d e?ZDGdddeFZGGd?ddZHGd@ddeHe?ZIGdAddeHe?ZJejKZLGdBddZMGdCdde?eMZNGdDdde?eMZOGdEdFdFe?ZPGdGddePZQeRejSdHr&GdIdJdJePZTe.UdJGdKd d e?ZVGdLdde?ZWdMdNZXdOdPZYGdQdde?ZZdRdSZ[GdTdde?Z\GdUdde\Z]GdVdde?Z^dWZ_ej`dXkrddYlambZbmcZcndZd"Zbd[d!ZciZdGd\d&d&ZeGd]d'd'eeZfdagd^d_Zhdaid`daZjdakdbdcZldamdddeZnGdfdgdgZodhdiZpd~djdkZqdldmZre jsdnkrddoltmuZumvZvdpdqZwdrdsZxdtduZydvd#Zzn6ej`dXkrdwdxZ{dyd#Zzdzd{Z|d|duZynepZzeqZydS)N)URLError HTTPErrorContentTooShortError)urlparseurlspliturljoinunwrapquoteunquote _splittype _splithost _splitport _splituser _splitpasswd _splitattr _splitquery _splitvalue _splittag _to_bytesunquote_to_bytes urlunparse) addinfourl addclosehookFTRequestOpenerDirector BaseHandlerHTTPDefaultErrorHandlerHTTPRedirectHandlerHTTPCookieProcessor ProxyHandlerHTTPPasswordMgrHTTPPasswordMgrWithDefaultRealmHTTPPasswordMgrWithPriorAuthAbstractBasicAuthHandlerHTTPBasicAuthHandlerProxyBasicAuthHandlerAbstractDigestAuthHandlerHTTPDigestAuthHandlerProxyDigestAuthHandler HTTPHandler FileHandler FTPHandlerCacheFTPHandler DataHandlerUnknownHandlerHTTPErrorProcessorurlopeninstall_opener build_opener pathname2url url2pathname getproxies urlretrieve urlcleanup URLopenerFancyURLopenerz%d.%d)cafilecapath cadefaultcontextc Cs|s |s |rfddl}|dtd|dk r2tdts>tdtjtjj||d}t |d}t |} n0|r~t |d}t |} nt dkrt a } nt } | |||S)NrzJcafile, capath and cadefault are deprecated, use a custom context instead.r:zDYou can't pass both context and any of cafile, capath, and cadefaultzSSL support not available)r;r<)r>) warningswarnDeprecationWarning ValueError _have_sslsslZcreate_default_contextZPurposeZ SERVER_AUTH HTTPSHandlerr2_openeropen) urldatatimeoutr;r<r=r>r?Z https_handleropenerrL&/usr/lib64/python3.8/urllib/request.pyr0s2<       cCs|adSN)rF)rKrLrLrMr1sc Cs:t|\}}tt||}|}|dkrN|sNtj||fW5QRS|r^t|d}nt j dd}|j }t ||||f} d} d} d} d} d|krt|d } |r|| | | || }|sq| t|7} ||| d 7} |r|| | | qW5QRXW5QRX| dkr6| | kr6td | | f| | S) NfilewbF)delete rcontent-lengthContent-Length1retrieval incomplete: got only %i out of %i bytes)r contextlibclosingr0infoospathnormpathrGtempfileZNamedTemporaryFilename_url_tempfilesappendintreadlenwriter)rHfilename reporthookrIZurl_typer\fpheaderstfpresultbssizercblocknumblockrLrLrMr6sH          "c CsDtD](}zt|Wqtk r*YqXqtdd=tr@dadSrN)r`r[unlinkOSErrorrF)Z temp_filerLrLrMr7$s z:\d+$cCs<|j}t|d}|dkr&|dd}td|d}|S)NrVHost)full_urlr get_header _cut_port_resublower)requestrHhostrLrLrM request_host3s   r{c@seZdZdidddfddZeddZejddZejddZed d Zejd d Zejd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZd#ddZdd Zd!d"ZdS)$rNFc Csl||_i|_i|_d|_||_d|_|D]\}}|||q,|dkrRt|}||_ ||_ |rh||_ dSrN) rtriunredirected_hdrs_datarI _tunnel_hostitems add_headerr{origin_req_host unverifiablemethod) selfrHrIrirrrkeyvaluerLrLrM__init__EszRequest.__init__cCs|jrd|j|jS|jS)Nz{}#{})fragmentformat _full_urlrrLrLrMrtWszRequest.full_urlcCs(t||_t|j\|_|_|dSrN)rrrr_parserrHrLrLrMrt]s cCsd|_d|_d|_dSNrr)rrselectorrrLrLrMrtdscCs|jSrN)r}rrLrLrMrIjsz Request.datacCs(||jkr$||_|dr$|ddS)NContent-length)r} has_header remove_header)rrIrLrLrMrIns  cCs d|_dSrN)rIrrLrLrMrIxscCsNt|j\|_}|jdkr(td|jt|\|_|_|jrJt|j|_dS)Nzunknown url type: %r) r rtyperBrtr rzrr )rrestrLrLrMr|s  zRequest._parsecCs|jdk rdnd}t|d|S)NPOSTGETr)rIgetattr)rZdefault_methodrLrLrM get_methodszRequest.get_methodcCs|jSrN)rtrrLrLrM get_full_urlszRequest.get_full_urlcCs2|jdkr|js|j|_n||_|j|_||_dS)Nhttps)rr~rzrtr)rrzrrLrLrM set_proxys  zRequest.set_proxycCs |j|jkSrN)rrtrrLrLrM has_proxyszRequest.has_proxycCs||j|<dSrN)ri capitalizerrvalrLrLrMrszRequest.add_headercCs||j|<dSrN)r|rrrLrLrMadd_unredirected_headerszRequest.add_unredirected_headercCs||jkp||jkSrN)rir|r header_namerLrLrMrs zRequest.has_headercCs|j||j||SrN)rigetr|)rrdefaultrLrLrMrus zRequest.get_headercCs |j|d|j|ddSrN)ripopr|rrLrLrMrszRequest.remove_headercCs|j|j}t|SrN)r|rilistr)rhdrsrLrLrM header_itemss zRequest.header_items)N)__name__ __module__ __qualname__rpropertyrtsetterdeleterrIrrrrrrrrrurrrLrLrLrMrCs8        c@sNeZdZddZddZddZddZd ejfd d Z dd d Z ddZ d S)rcCs6dt}d|fg|_g|_i|_i|_i|_i|_dS)NPython-urllib/%sz User-agent) __version__ addheadershandlers handle_open handle_errorprocess_responseprocess_request)rZclient_versionrLrLrMrs zOpenerDirector.__init__c CsTt|dstdt|d}t|D]}|dkr6q&|d}|d|}||dd}|dr|d|d}||dd}z t|}Wntk rYnX|j |i} | |j|<n>|dkr|}|j } n*|d kr|}|j } n|d kr&|}|j } nq&| |g} | r"t| |n | |d }q&|rPt|j|||dS) N add_parentz%expected BaseHandler instance, got %rF)redirect_requestdo_open proxy_open_rVerrorrGresponseryT)hasattr TypeErrorrdirfind startswithrbrBrrrrr setdefaultbisectZinsortrarr) rhandlerZaddedmethiprotocolZ conditionjkindlookuprrLrLrM add_handlersL        zOpenerDirector.add_handlercCsdSrNrLrrLrLrMcloseszOpenerDirector.closec Gs<||d}|D]&}t||}||}|dk r|SqdS)NrL)rr) rchainr meth_nameargsrrfuncrkrLrLrM _call_chains   zOpenerDirector._call_chainNc Cst|trt||}n|}|dk r(||_||_|j}|d}|j|gD]}t||}||}qJt d|j |j|j | |||} |d}|j|gD]}t||}||| } q| S)NZ_requestzurllib.RequestZ _response) isinstancestrrrIrJrrrrsysauditrtrir_openr) rfullurlrIrJreqrrZ processorrrrLrLrMrGs$       zOpenerDirector.opencCsP||jdd|}|r|S|j}||j||d|}|r>|S||jdd|S)NrZ default_openrunknown unknown_open)rrr)rrrIrkrrLrLrMrs$   zOpenerDirector._opencGs~|dkr,|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|}|r^|S|rz|dd f|}|j|SdS) Nhttprrr:z http_error_%srVZ_errorrrhttp_error_default)rr)rprotordictrZhttp_errZ orig_argsrkrLrLrMr&s   zOpenerDirector.error)N) rrrrrrrsocket_GLOBAL_DEFAULT_TIMEOUTrGrrrLrLrLrMrs /  c Gst}ttttttttt g }t t j dr2| tt}|D]B}|D]8}t|trht||r|||qDt||rD||qDq<|D]}||q|D]}||q|D]}t|tr|}||q|S)NHTTPSConnection)rrr.r)rrr+r*r/r-rrclientrarEsetrr issubclassaddremover)rrKZdefault_classesskipklassZcheckhrLrLrMr2?s8          c@s(eZdZdZddZddZddZdS) rcCs ||_dSrN)parent)rrrLrLrMrfszBaseHandler.add_parentcCsdSrNrLrrLrLrMriszBaseHandler.closecCst|dsdS|j|jkS)N handler_orderT)rr)rotherrLrLrM__lt__ms zBaseHandler.__lt__N)rrrrrrrrLrLrLrMrcsc@seZdZdZddZeZdS)r/icCsH|j|j|}}}d|kr,dksDn|jd|||||}|S)N,r)codemsgrZrr)rryrrrrrLrLrM http_responsezsz HTTPErrorProcessor.http_responseN)rrrrrhttps_responserLrLrLrMr/vs c@seZdZddZdS)rcCst|j||||dSrN)rrt)rrrhrrrrLrLrMrsz*HTTPDefaultErrorHandler.http_error_defaultN)rrrrrLrLrLrMrsc@s4eZdZdZdZddZddZeZZZ dZ dS) r c st|}|dkr|dks:|dkr(|dks:t|j|||||dd}dfdd |jD}t|||jd d S) N)-./i3)rZHEAD)rrrr z%20)rTz content-typecs"i|]\}}|kr||qSrL)rx.0kvZCONTENT_HEADERSrLrM s z8HTTPRedirectHandler.redirect_request..T)rirr)rrrtreplacerirrr) rrrhrrrinewurlmZ newheadersrLrrMrs  z$HTTPRedirectHandler.redirect_requestc CsLd|kr|d}nd|kr$|d}ndSt|}|jdkrRt||d||f|||jsn|jrnt|}d|d<t|}t|dtj d}t |j |}| ||||||}|dkrdSt |d r|j} |_| |d |jkst| |jkrt|j ||j|||ni} |_|_| |d d | |<|||jj||jd S) Nlocationurirrftprrz+%s - Redirection to url '%s' is not allowed/r:z iso-8859-1)encodingsafe redirect_dictrrVrJ)rschemerr\Znetlocrrr stringZ punctuationrrtrrr r max_repeatsrdmax_redirectionsinf_msgrcrrrGrJ) rrrhrrrirurlpartsnewZvisitedrLrLrMhttp_error_302sT         z"HTTPRedirectHandler.http_error_302zoThe HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: N) rrrrrrrhttp_error_301http_error_303http_error_307rrLrLrLrMrs &< c Cst|\}}|ds d}|}nZ|ds6td|d|krV|d}|d|}n |dd}|dkrnd}|d|}t|\}}|dk rt|\}} nd}} ||| |fS)Nr //zproxy URL with no authority: %r@r:rS)r rrBrrr) proxyrZr_scheme authorityZhost_separatorendZuserinfohostportuserpasswordrLrLrM _parse_proxys$        r"c@s"eZdZdZdddZddZdS)rdNcCsP|dkrt}||_|D].\}}|}t|d||||jfddqdS)Nz%s_opencSs ||||SrNrL)rrrrrLrLrM)sz'ProxyHandler.__init__..)r5proxiesrrxsetattrr)rr&rrHrLrLrMr!s zProxyHandler.__init__c Cs|j}t|\}}}}|dkr"|}|jr6t|jr6dS|rv|rvdt|t|f} t| d} | dd| t|}| ||||ks|dkrdS|j j ||j dSdS)N%s:%sasciiProxy-authorizationBasic rr)rr"rz proxy_bypassr base64 b64encodeencodedecoderrrrGrJ) rrrrZ orig_typeZ proxy_typer r!rZ user_passZcredsrLrLrMr,s" zProxyHandler.proxy_open)N)rrrrrrrLrLrLrMrs c@s6eZdZddZddZddZd dd Zd d Zd S)r cCs i|_dSrN)passwdrrLrLrMrJszHTTPPasswordMgr.__init__cs\t|tr|g}|jkr$ij|<dD].tfdd|D}||fj||<q(dS)NTFc3s|]}|VqdSrN) reduce_uri)ru default_portrrLrM Tsz/HTTPPasswordMgr.add_password..)rrr1tuple)rrealmrr r1 reduced_urirLr5rM add_passwordMs   zHTTPPasswordMgr.add_passwordc Cs`|j|i}dD]H}|||}|D].\}}|D] }|||r6|Sq6q*qdS)Nr2NN)r1rr3r is_suburi) rr9authuriZdomainsr6reduced_authuriZurisZauthinforrLrLrMfind_user_passwordXs  z"HTTPPasswordMgr.find_user_passwordTc Cst|}|dr.|d}|d}|dp*d}n d}|}d}t|\}}|r~|dkr~|dk r~ddd|} | dk r~d|| f}||fS) NrVrr:r Pirz%s:%d)rr r) rrr6partsrrr\rzportZdportrLrLrMr3bs$  zHTTPPasswordMgr.reduce_uricCsN||kr dS|d|dkr dS|d}|dddkr@|d7}|d|S)NTrFrVrSr )r)rbasetestprefixrLrLrMr=yszHTTPPasswordMgr.is_suburiN)T)rrrrr;r@r3r=rLrLrLrMr Hs   c@seZdZddZdS)r!cCs0t|||\}}|dk r"||fSt|d|SrN)r r@)rr9r>r r!rLrLrMr@s z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)rrrr@rLrLrLrMr!scs<eZdZfddZd fdd Zd ddZdd ZZS) r"csi|_tj||dSrN) authenticatedsuperrrrkwargs __class__rLrMrsz%HTTPPasswordMgrWithPriorAuth.__init__Fcs<||||dk r&td|||t||||dSrN)update_authenticatedrHr;)rr9rr r1is_authenticatedrKrLrMr;s z)HTTPPasswordMgrWithPriorAuth.add_passwordcCs>t|tr|g}dD]$}|D]}|||}||j|<qqdSNr2)rrr3rG)rrrNr6r4r:rLrLrMrMs   z1HTTPPasswordMgrWithPriorAuth.update_authenticatedcCsDdD]:}|||}|jD]"}|||r|j|SqqdSrO)r3rGr=)rr>r6r?rrLrLrMrNs    z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)rrrrr;rMrN __classcell__rLrLrKrMr"s  c@sTeZdZedejZdddZddZddZ d d Z d d Z d dZ e Z e ZdS)r#z1(?:^|,)[ ]*([^ ,]+)[ ]+realm=(["']?)([^"']*)\2NcCs"|dkrt}||_|jj|_dSrN)r r1r;)rZ password_mgrrLrLrMrsz!AbstractBasicAuthHandler.__init__ccspd}tj|D]6}|\}}}|dkr8tdtd||fVd}q|sl|r^|d}nd}|dfVdS)NF)"'zBasic Auth Realm was unquotedTrrr)r#rxfinditergroupsr?r@ UserWarningsplit)rheaderZfound_challengeZmorr r9rLrLrM _parse_realms z%AbstractBasicAuthHandler._parse_realmc Cs~||}|sdSd}|D]H}||D]8\}}|dkrB|}q(|dk r(||||Sq(q|dk rztd|fdS)NbasiczBAbstractBasicAuthHandler does not support the following scheme: %r)Zget_allrZrxretry_http_basic_authrB) rauthreqrzrriZ unsupportedrYrr9rLrLrMhttp_error_auth_reqeds  z.AbstractBasicAuthHandler.http_error_auth_reqedcCs||j||\}}|dk rtd||f}dt|d}||jd|krTdS||j||j j ||j dSdSdS)Nr(r+r)r) r1r@r-r.r/r0ru auth_headerrrrGrJ)rrzrr9r pwrawauthrLrLrMr\s z.AbstractBasicAuthHandler.retry_http_basic_authcCstt|jdr|j|js|S|dsp|jd|j\}}d||}t | }| dd| |S)NrN Authorizationz{0}:{1}zBasic {}) rr1rNrtrr@rr/r-Zstandard_b64encoder0rstrip)rrr r1Z credentialsZauth_strrLrLrM http_requests    z%AbstractBasicAuthHandler.http_requestcCsLt|jdrHd|jkr"dkr8nn|j|jdn|j|jd|S)NrNrrTF)rr1rrMrt)rrrrLrLrMrs  z&AbstractBasicAuthHandler.http_response)N)rrrrecompileIrTrrZr^r\rer https_requestrrLrLrLrMr#s   c@seZdZdZddZdS)r$rccCs|j}|d|||}|S)Nwww-authenticate)rtr^)rrrhrrrirHrrLrLrMhttp_error_401$sz#HTTPBasicAuthHandler.http_error_401N)rrrr_rkrLrLrLrMr$ sc@seZdZdZddZdS)r%r*cCs|j}|d|||}|SNproxy-authenticate)rzr^)rrrhrrrirrrLrLrMhttp_error_407/sz$ProxyBasicAuthHandler.http_error_407N)rrrr_rnrLrLrLrMr%+sc@sNeZdZdddZddZddZdd Zd d Zd d ZddZ ddZ dS)r&NcCs4|dkrt}||_|jj|_d|_d|_d|_dSNr)r r1r;retried nonce_count last_nonce)rr1rLrLrMrIs z"AbstractDigestAuthHandler.__init__cCs d|_dSro)rprrLrLrMreset_retry_countRsz+AbstractDigestAuthHandler.reset_retry_countcCs|||d}|jdkr*t|jdd|dn|jd7_|rx|d}|dkr`|||S|dkrxtd|dS) Nizdigest auth failedrVrZdigestr[zEAbstractDigestAuthHandler does not support the following scheme: '%s')rrprrtrXrxretry_http_digest_authrB)rr_rzrrir]rrLrLrMr^Us       z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz|dd\}}ttdt|}|||}|rvd|}|j|jd|krRdS||j||j j ||j d}|SdS)NrrVz Digest %sr) rXparse_keqv_listfilterparse_http_listget_authorizationrirr_rrrGrJ)rrrbtokenZ challengechalZauth_valZresprLrLrMruis z0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|tf}|dtd}t|}|ddS)Nz %s:%s:%s:r))rqtimectimer/ _randombyteshashlibsha1 hexdigest)rnoncesbdigrLrLrM get_cnonceusz$AbstractDigestAuthHandler.get_cnoncecCsz6|d}|d}|d}|dd}|dd}Wntk rLYdSX||\}} |dkrhdS|j||j\} } | dkrdS|jdk r||j|} nd} d| || f} d||j f}|dkr| || d|||f}n~d | d kr\||j kr|j d 7_ n d |_ ||_ d |j }| |}d |||d ||f}| || |}n td|d| |||j |f}|r|d|7}| r|d| 7}|d|7}|r|d||f7}|S)Nr9rqop algorithmMD5opaquez%s:%s:%sr(rb,rVz%08xz%s:%s:%s:%s:%szqop '%s' is not supported.z>username="%s", realm="%s", nonce="%s", uri="%s", response="%s"z , opaque="%s"z , digest="%s"z, algorithm="%s"z, qop=auth, nc=%s, cnonce="%s")rKeyErrorget_algorithm_implsr1r@rtrIget_entity_digestrrrXrrrqrr)rrr{r9rrrrHKDr r`ZentdigZA1ZA2ZrespdigZncvalueZcnonceZnoncebitrDrLrLrMrys\           z+AbstractDigestAuthHandler.get_authorizationcsD|dkrddn|dkr$ddn td|fdd}|fS)NrcSst|dSNr))rZmd5r/rxrLrLrMr%z?AbstractDigestAuthHandler.get_algorithm_impls..ZSHAcSst|dSr)rrr/rrrLrLrMr%rz.Unsupported digest authentication algorithm %rcsd||fS)Nr(rL)rdrrLrMr%r)rB)rrrrLrrMrs   z-AbstractDigestAuthHandler.get_algorithm_implscCsdSrNrL)rrIr{rLrLrMrsz+AbstractDigestAuthHandler.get_entity_digest)N) rrrrrsr^rurryrrrLrLrLrMr&>s   > c@seZdZdZdZddZdS)r'rccCs*t|jd}|d|||}||S)NrVrj)rrtr^rsrrrhrrrirzretryrLrLrMrksz$HTTPDigestAuthHandler.http_error_401N)rrrr_rrkrLrLrLrMr'sc@seZdZdZdZddZdS)r(Proxy-AuthorizationrcCs"|j}|d|||}||Srl)rzr^rsrrLrLrMrnsz%ProxyDigestAuthHandler.http_error_407N)rrrr_rrnrLrLrLrMr(sc@s6eZdZd ddZddZddZdd Zd d Zd S)AbstractHTTPHandlerrcCs ||_dSrN _debuglevel)r debuglevelrLrLrMrszAbstractHTTPHandler.__init__cCs ||_dSrNr)rlevelrLrLrMset_http_debuglevelsz'AbstractHTTPHandler.set_http_debuglevelcCstjj|j|SrN)rrHTTPConnection_get_content_lengthrIrrryrLrLrMrsz'AbstractHTTPHandler._get_content_lengthc Cs|j}|std|jdk r|j}t|tr8d}t||dsN|dd|ds|ds||}|dk r|dt|n |dd|}| rt |j \}}t |\}} |ds|d||j jD]&\} } | } || s|| | q|S) N no host givenz\POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.z Content-type!application/x-www-form-urlencodedrTransfer-encodingZchunkedrs)rzrrIrrrrrrrr rr rrr) rryrzrIrZcontent_lengthZsel_hostrZselZsel_pathr_rrLrLrM do_request_sJ         zAbstractHTTPHandler.do_request_c sT|j}|std||fd|ji|}||jt|jfdd|j Ddd<dd D|j ri}d}|kr|||<|=|j |j |d z`z&|j | |j|j|d d Wn,tk r}z t|W5d}~XYnX|} Wn|YnX|jr>|jd|_|| _| j| _| S) NrrJcsi|]\}}|kr||qSrLrLrrirLrMr/sz/AbstractHTTPHandler.do_open..r ConnectioncSsi|]\}}||qSrL)title)rr_rrLrLrMr<srrr)Zencode_chunked)rzrrJZset_debuglevelrrr|updaterirr~Z set_tunnelryrrrIrrq getresponserZsockrrHreasonr) rZ http_classrZhttp_conn_argsrzrZtunnel_headersZproxy_auth_hdrerrr$rLrrMr!sB        zAbstractHTTPHandler.do_openN)r)rrrrrrrrrLrLrLrMrs  &rc@seZdZddZejZdS)r)cCs|tjj|SrN)rrrrrrrLrLrM http_openfszHTTPHandler.http_openN)rrrrrrrerLrLrLrMr)dsrc@s$eZdZdddZddZejZdS)rErNcCst||||_||_dSrN)rr_context_check_hostname)rrr>check_hostnamerLrLrMros zHTTPSHandler.__init__cCs|jtjj||j|jdS)N)r>r)rrrrrrrrLrLrM https_opents zHTTPSHandler.https_open)rNN)rrrrrrrrirLrLrLrMrEms rEc@s.eZdZdddZddZddZeZeZdS) rNcCs$ddl}|dkr|j}||_dSro)Zhttp.cookiejar cookiejarZ CookieJar)rrrrLrLrMr}s zHTTPCookieProcessor.__init__cCs|j||SrN)rZadd_cookie_headerrrLrLrMres z HTTPCookieProcessor.http_requestcCs|j|||SrN)rZextract_cookies)rryrrLrLrMrsz!HTTPCookieProcessor.http_response)N)rrrrrerrirrLrLrLrMr|s  c@seZdZddZdS)r.cCs|j}td|dS)Nzunknown url type: %s)rr)rrrrLrLrMrszUnknownHandler.unknown_openN)rrrrrLrLrLrMr.scCsNi}|D]@}|dd\}}|ddkr@|ddkr@|dd}|||<q|S)N=rVrrQrS)rX)lZparsedZeltrrrLrLrMrvs  rvcCsg}d}d}}|D]l}|r*||7}d}q|rT|dkr>d}qn |dkrJd}||7}q|dkrl||d}q|dkrxd}||7}q|r||dd|DS) NrrF\TrQrcSsg|] }|qSrL)rd)rpartrLrLrM sz#parse_http_list..)ra)rresrescaper ZcurrLrLrMrxs4    rxc@s(eZdZddZdZddZddZdS)r*cCs\|j}|dddkrN|dddkrN|jrN|jdkrN|j|krXtdn ||SdS)Nr:rrSr  localhost-file:// scheme is supported only on localhost)rrz get_namesropen_local_file)rrrHrLrLrM file_opens& zFileHandler.file_openNcCs`tjdkrZz*ttddttdt_Wn$tjk rXtdft_YnXtjS)Nrr:)r*namesr8rgethostbyname_ex gethostnamegaierror gethostbynamerrLrLrMrs   zFileHandler.get_namesc Csddl}ddl}|j}|j}t|}zt|}|j}|jj |j dd} | |d} | d| pbd|| f} |r~t |\}} |r| st||kr|rd||} nd|} tt|d| | WSWn*tk r}z t|W5d}~XYnXtddS) NrTZusegmtz6Content-type: %s Content-length: %d Last-modified: %s text/plainfile://rbzfile not on local host) email.utils mimetypesrzrr4r[statst_sizeutils formatdatest_mtime guess_typemessage_from_stringr _safe_gethostbynamerrrGrqr)rremailrrzrfZ localfilestatsrmmodifiedmtyperirCZorigurlexprLrLrMrs:   zFileHandler.open_local_file)rrrrrrrrLrLrLrMr*s  cCs*z t|WStjk r$YdSXdSrN)rrr)rzrLrLrMrs rc@seZdZddZddZdS)r+c Cs*ddl}ddl}|j}|s"tdt|\}}|dkr>|j}nt|}t|\}}|rdt|\}}nd}t |}|pvd}|p~d}zt |}Wn*t k r}z t|W5d}~XYnXt |j\} } | d} ttt | } | dd| d} } | r| ds| dd} z|||||| |j} | r6dp8d}| D]2}t|\}}|d kr>|d kr>|}q>| | |\}}d}||jd}|r|d |7}|dk r|dkr|d |7}t|}t|||jWS|jk r$}z"td |}|t dW5d}~XYnXdS)Nrftp error: no host givenrrr rSrVrhDraArrhrrzContent-type: %s zContent-length: %d ftp error: %rr:)!ftplibrrzrr FTP_PORTrbrrr rrrqrrrXrmap connect_ftprJrrxupperretrfilerrtrrr all_errorswith_tracebackrexc_info)rrrrrzrCr r1rr\attrsdirsrOfwrattrrrhretrlenrirrexcrLrLrMftp_opens^          zFTPHandler.ftp_openc Cst||||||ddS)NF) persistent) ftpwrapper)rr r1rzrCrrJrLrLrMr7szFTPHandler.connect_ftpN)rrrrrrLrLrLrMr+s5c@s<eZdZddZddZddZddZd d Zd d Zd S)r,cCs"i|_i|_d|_d|_d|_dS)Nr<r})cacherJsoonestdelay max_connsrrLrLrMr>s zCacheFTPHandler.__init__cCs ||_dSrN)r)rtrLrLrM setTimeoutEszCacheFTPHandler.setTimeoutcCs ||_dSrN)r)rrrLrLrM setMaxConnsHszCacheFTPHandler.setMaxConnscCsr|||d||f}||jkr4t|j|j|<n,t|||||||j|<t|j|j|<||j|S)Nr )joinrr~rrJr check_cache)rr r1rzrCrrJrrLrLrMrKs   zCacheFTPHandler.connect_ftpcCst}|j|krPt|jD].\}}||kr |j||j|=|j|=q tt|j|_t |j|j krt|jD]&\}}||jkr|j|=|j|=qqtt|j|_dSrN) r~rrrJrrrminvaluesrdr)rrrrrLrLrMrVs   zCacheFTPHandler.check_cachecCs0|jD] }|q |j|jdSrN)rrrclearrJ)rconnrLrLrM clear_cachejs  zCacheFTPHandler.clear_cacheN) rrrrrrrrrrLrLrLrMr,;s  c@seZdZddZdS)r-cCs~|j}|dd\}}|dd\}}t|}|drNt|}|dd}|sVd}td|t|f}t t |||S)N:rVrz;base64itext/plain;charset=US-ASCIIz$Content-type: %s Content-length: %d ) rtrXrendswithr- decodebytesrrrdrioBytesIO)rrrHrrIZ mediatyperirLrLrM data_openqs     zDataHandler.data_openN)rrrr rLrLrLrMr-psrnt)r4r3cCst|SrN)r pathnamerLrLrMr4scCst|SrN)r r rLrLrMr3sc@seZdZdZdeZd)ddZddZddZd d Z d d Z d*d dZ d+ddZ d,ddZ d-ddZddZd.ddZd/ddZddZerddZd0dd Zd!d"Zd#d$Zd%d&Zd1d'd(ZdS)2r8NrcKszdd|jji}tj|tdd|dkr.t}||_|d|_|d|_ d|j fdg|_ g|_ t j|_d|_t|_dS) NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methodsclassrS) stacklevelkey_file cert_filez User-Agent)ZAcceptz*/*)rLrr?r@rAr5r&rrrversionr_URLopener__tempfilesr[rp_URLopener__unlink tempcacheftpcache)rr&Zx509rrLrLrMrs   zURLopener.__init__cCs |dSrN)rrrLrLrM__del__szURLopener.__del__cCs |dSrN)cleanuprrLrLrMrszURLopener.closec CsV|jrB|jD](}z||Wq tk r2Yq Xq |jdd=|jrR|jdSrN)rrrqrr)rrOrLrLrMrs  zURLopener.cleanupcGs|j|dSrN)rra)rrrLrLrM addheaderszURLopener.addheaderc Csptt|}t|dd}|jrL||jkrL|j|\}}t|d}t|||St|\}}|s`d}||jkr|j|}t|\}} t| \} } | |f}nd}d|} ||_ | dd} t || r| dkr|r| |||S| ||Sz0|dkrt|| |WSt|| ||WSWnVttfk r0Yn<tk rj} ztd | td W5d} ~ XYnXdS) Nz%/:=&?~#+!$,;'@()*[]|r rrOZopen_-rrz socket errorr:)rrr rrGrr r&r rrropen_unknown_proxy open_unknownrrrrqrrr)rrrIrfrirhurltyperHr proxyhostrzrr_rrLrLrMrGs<             zURLopener.opencCst|\}}tdd|dS)N url errorzunknown url typer rq)rrrIrrHrLrLrMr s zURLopener.open_unknowncCs t|\}}tdd||dS)Nrzinvalid proxy for %sr )rrrrIrrHrLrLrMrs zURLopener.open_unknown_proxyc Cstt|}|jr&||jkr&|j|St|\}}|dkr|rF|dkrz0||}|}|tt|d|fWSt k r} zW5d} ~ XYnX| ||}z<|} |rt |d} nrt|\} } t| pd\} } t | pd\} } t | pd\} } t j| d}t|\}}|j|t |d} z|| f}|jdk rT||j|<d}d}d}d}d| krzt| d }|r||||||}|sq|t|7}| ||d7}|r||||qW5| XW5|X|dkr||krtd ||f||S) NrOrVrPrrrRrSrrTrUrW)rrrr rrZrr4r rqrGrrr[r\splitextr^Zmkstemprrafdopenrbrcrdrer)rrHrfrgrIrZurl1rhrrrirjZgarbager\suffixfdrkrlrmrcrnrorLrLrMretrievesn                  zURLopener.retrievecCs$d}d}t|trtd |tt|d | | Std dS)NrTrz6Content-Type: %s Content-Length: %d Last-modified: %s rrVr rrr:z./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)rrr r4r[rrqrstrerrorrfrrrrrrrrGr rrrthishostrB)rrHrrrzrOZ localnamerermrrriZurlfilerCrLrLrMrs@      zURLopener.open_local_filec Cst|tstdddl}t|\}}|s2tdt|\}}t|\}}|r\t|\}}nd}t|}t|ppd}t|p|d}t |}|sddl }|j }nt |}t|\}} t|}|d} | dd| d} } | r| ds| dd} | r | ds d| d<|||d| f} t|jtkrbt|jD]*} | | kr6|j| }|j| =|q6z| |jkrt||||| |j| <| sd}nd }| D]2}t|\}}|d kr|d kr|}q|j| | |\}}|d |d}d}|r |d |7}|dk r,|dkr,|d|7}t|}t||d |WStk r}ztd| t!"dW5d}~XYnXdS)NzCftp error: proxy support for ftp protocol currently not implementedrrrrr rSrVrrhrrzftp:zContent-Type: %s zContent-Length: %d z ftp error %rr:)#rrrrr r rrr rrrrrbrrXrrdr MAXFTPCACHErrrrrxrrrrrr ftperrorsrrr)rrHrrzr\rCr r1rrrrOrrrrrrrhrrrirrLrLrMopen_ftpst                   zURLopener.open_ftpc Cs<t|tstdz|dd\}}Wntk rDtddYnX|sNd}|d}|dkrd ||dkr||dd}|d|}nd }g}|d t d t t|d ||dkrt | dd}nt|}|dt||d ||d|}t|}t|}t|||S)NzEdata error: proxy support for data protocol currently not implementedrrVz data errorz bad data URLr;rrrrzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr-r)zlatin-1zContent-Length: %d )rrrrXrBrqrfindrar~strftimegmtimer-rr/r0r rdrrrrStringIOr) rrHrIrZsemir rrifrLrLrM open_data8s8         zURLopener.open_data)N)N)N)N)NNN)N)N)N)N)rrrrrrrrrrrrGrrr%r(r*r'rrCr.r/r1rr7r?rLrLrLrMr8s,  $   A\     :c@seZdZddZddZd"ddZdd Zd#d d Zd$d d Zd%ddZ d&ddZ d'ddZ d(ddZ d)ddZ d*ddZd+ddZd,ddZd d!ZdS)-r9cOs(tj|f||i|_d|_d|_dS)Nrr)r8r auth_cachetriesmaxtriesrIrLrLrMreszFancyURLopener.__init__cCst||d||S)Nr&)rr-rLrLrMrksz!FancyURLopener.http_error_defaultNc Csv|jd7_zZ|jrN|j|jkrNt|dr4|j}n|j}|||dd|WS|||||||}|WSd|_XdS)NrVrhttp_error_500rz)Internal Server Error: Redirect Recursion)rArBrrCrredirect_internal) rrHrhr+r,rirIrrkrLrLrMros   zFancyURLopener.http_error_302c Csxd|kr|d}nd|kr$|d}ndS|t|jd||}t|}|jdkrnt|||d|||||S)Nrrrrz( Redirection to url '%s' is not allowed.)rrrrrrrG) rrHrhr+r,rirIrrrLrLrMrDs"   z FancyURLopener.redirect_internalcCs|||||||SrNrrrHrhr+r,rirIrLrLrMrszFancyURLopener.http_error_301cCs|||||||SrNrErFrLrLrMrszFancyURLopener.http_error_303cCs2|dkr|||||||S||||||SdSrN)rrrFrLrLrMrszFancyURLopener.http_error_307Fc Csd|krt|||||||d}td|} | sHt||||||| \} } | dkrtt|||||||st||||||d|jd} |dkrt|| || St|| || |SdS)Nrj![ ]*([^ ]+)[ ]+realm="([^"]*)"r[Zretry_ _basic_authr8rrfmatchrVrxrr rrHrhr+r,rirIrZstuffrJrr9r_rLrLrMrks:      zFancyURLopener.http_error_401c Csd|krt|||||||d}td|} | sHt||||||| \} } | dkrtt|||||||st||||||d|jd} |dkrt|| || St|| || |SdS)NrmrGr[Z retry_proxy_rHrIrKrLrLrMrns:      zFancyURLopener.http_error_407cCst|\}}d||}|jd}t|\}} t| \} } | dd} | | d} || || \} } | sr| srdSdt| ddt| dd| f} d| | |jd<|dkr||S|||SdS)Nhttp://rrrV%s:%s@%srrrr r&r rget_user_passwdr rGrrHr9rIrzrrrrrZ proxyselectorrr r1rLrLrMretry_proxy_http_basic_auths$         z*FancyURLopener.retry_proxy_http_basic_authcCst|\}}d||}|jd}t|\}} t| \} } | dd} | | d} || || \} } | sr| srdSdt| ddt| dd| f} d| | |jd<|dkr||S|||SdS)Nhttps://rrrVrMrrrrNrPrLrLrMretry_proxy_https_basic_auths$         z+FancyURLopener.retry_proxy_https_basic_authc Cst|\}}|dd}||d}||||\}}|sD|sDdSdt|ddt|dd|f}d||} |dkr|| S|| |SdS)NrrVrMrrrrLr rrOr rG rrHr9rIrzrrr r1rrLrLrMr\ s      z$FancyURLopener.retry_http_basic_authc Cst|\}}|dd}||d}||||\}}|sD|sDdSdt|ddt|dd|f}d||} |dkr|| S|| |SdS)NrrVrMrrrrRrTrUrLrLrMretry_https_basic_auth s      z%FancyURLopener.retry_https_basic_authrcCs`|d|}||jkr2|r(|j|=n |j|S|||\}}|sJ|rX||f|j|<||fS)Nr)rxr@prompt_user_passwd)rrzr9rrr r1rLrLrMrO s   zFancyURLopener.get_user_passwdcCsXddl}z.td||f}|d|||f}||fWStk rRtYdSXdS)NrzEnter username for %s at %s: z#Enter password for %s in %s at %s: r<)getpassinputKeyboardInterruptprint)rrzr9rXr r1rLrLrMrW) s z!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r)rrrrrrrDrrrrkrnrQrSr\rVrOrWrLrLrLrMr9bs&           cCstdkrtdatS)Nr) _localhostrrrLrLrLrMr9 s rcCsPtdkrLztttdaWn(tjk rJttddaYnXtS)Nr:r) _thishostr8rrrrrLrLrLrMr3A s r3cCstdkrddl}|jatSro) _ftperrorsrr)rrLrLrMr6L sr6cCstdkrtdatSr) _noheadersrrrLrLrLrM noheadersU s r`c@sFeZdZdddZddZddZd d Zd d Zd dZddZ dS)rNTcCsX||_||_||_||_||_||_d|_||_z |Wn| YnXdSro) r r1rzrCrrJrefcount keepaliveinitr)rr r1rzrCrrJrrLrLrMrb s zftpwrapper.__init__cCs\ddl}d|_||_|j|j|j|j|j|j |j d |j }|j |dS)Nrr )rbusyZFTPr ZconnectrzrCrJZloginr r1rrcwd)rrZ_targetrLrLrMrcr s  zftpwrapper.initc Csddl}||dkr"d}d}n d|}d}z|j|Wn*|jk rh||j|YnXd}|r|szd|}|j|\}}WnR|jk r}z2t|dddkrt d | t d W5d}~XYnX|s|jd|rl|j } zJz|j|Wn4|jk rN}zt d ||W5d}~XYnXW5|j| Xd |}nd }|j|\}}d|_t|d |j} |jd7_|| |fS)Nr)rrzTYPE ArVzTYPE zRETR rSZ550rr:zLIST ZLISTr)r endtransferr ZvoidcmdrrcZ ntransfercmdZ error_permrrrrrpwdrerdrZmakefile file_closerar) rrOrrcmdisdirrrrrgZftpobjrLrLrMr{ sP    $ zftpwrapper.retrfilecCs d|_dSro)rdrrLrLrMrf szftpwrapper.endtransfercCsd|_|jdkr|dS)NFr)rbra real_closerrLrLrMr s zftpwrapper.closecCs2||jd8_|jdkr.|js.|dS)NrVr)rfrarbrkrrLrLrMrh szftpwrapper.file_closecCs2|z|jWntk r,YnXdSrN)rfr rr6rrLrLrMrk s zftpwrapper.real_close)NT) rrrrrcrrfrrhrkrLrLrLrMr_ s  -rcCsi}tjD]4\}}|}|r|dddkr|||dd<qdtjkrZ|ddtjD]J\}}|dddkrd|}|r|||dd<qd||dddqd|S)Ni_proxyZREQUEST_METHODr)r[environrrxr)r&r_rrLrLrMgetproxies_environment s   rncCs|dkrt}z |d}Wntk r0YdSX|dkr>dS|}t|\}}|dD]Z}|}|r\|d}|}||ks||krdSd|}||s||r\dSq\dS)NZnoF*Tr.)rnrrxr rXrdlstripr)rzr&Zno_proxyhostonlyrCr_rLrLrMproxy_bypass_environment s*   rsc Cs0ddlm}t|\}}dd}d|kr4|dr4dSd}|dd D]}|sNqDtd |}|dk r|dkrzt|}||}Wntk rYqDYnX||d } |d } | dkrd |d  dd } nt | d d} | dksD| dkrqDd| } || ?| | ?kr*dSqD|||rDdSqDdS)Nr)fnmatchcSsh|d}ttt|}t|dkr<|ddddgdd}|dd>|dd>B|dd>B|d BS) NrprrrVr}r:r|rS)rXrrrbrd)ZipAddrrBrLrLrMip2num s   z,_proxy_bypass_macosx_sysconf..ip2numrpZexclude_simpleT exceptionsrLz(\d+(?:\.\d+)*)(/\d+)?rVr:r| F) rtr rrfrJrrrqgroupcountrb) rzproxy_settingsrtrrrCrvZhostIPrrrDmaskrLrLrM_proxy_bypass_macosx_sysconf s>         r}darwin)_get_proxy_settings _get_proxiescCst}t||SrN)rr})rzr{rLrLrMproxy_bypass_macosx_sysconfF srcCstSrN)rrLrLrLrMgetproxies_macosx_sysconfJ srcCs t}|rt||St|SdSrN)rnrsrrzr&rLrLrMr,T s r,cCs tp tSrN)rnrrLrLrLrMr5a sc Csi}z ddl}Wntk r(|YSXz||jd}||dd}|rt||dd}d|kr|dD]4}|dd\}}td|sd ||f}|||<qtn>|dd d kr||d <n$d ||d <d||d<d||d<| Wnt t t fk rYnX|S)Nr;Software\Microsoft\Windows\CurrentVersion\Internet Settings ProxyEnableZ ProxyServerrr8rVz (?:[^/:]+)://z%s://%srtr&rz http://%sz https://%srzftp://%sr ) winreg ImportErrorOpenKeyHKEY_CURRENT_USER QueryValueExrrXrfrJZCloserqrBr)r&rinternetSettings proxyEnableZ proxyServerprZaddressrLrLrMgetproxies_registryf sF          rcCs tp tSrN)rnrrLrLrLrMr5 sc Cs|z ddl}Wntk r"YdSXz6||jd}||dd}t||dd}Wntk rpYdSX|rz|s~dSt|\}}|g}z t |}||kr| |Wntk rYnXz t |}||kr| |Wntk rYnX| d}|D]j} | dkr*d|kr*dS| dd } | d d } | d d} |D] } t| | tjrRdSqRq dS) NrrrZ ProxyOverrider8zrprVz\.roz.*?)rrrrrrrqr rrraZgetfqdnrXrrfrJrh) rzrrrZ proxyOverrideZrawHostrCZaddrZfqdnrErrLrLrMproxy_bypass_registry s`           rcCs t}|rt||St|SdSrN)rnrsrrrLrLrMr, s )NNN)N)}r-rrrZ http.clientrrr[ posixpathrfrrrr~r^rXr?Z urllib.errorrrrZ urllib.parserrrrr r r r r rrrrrrrrrZurllib.responserrrDrrC__all__ version_inforrFrr0r1r`r6r7rgASCIIrvr{rrr2rr/rrr"rr r!r"r#r$r%urandomrr&r'r(rr)rrrErarr.rvrxr*rr+r,r-r5r_Z nturl2pathr4r3rr8r9r\rr]r3r^r6r_r`rrnrsr}platformZ_scproxyrrrrr,r5rrrLrLrLrMTsP   U ?m$q!+@ o  v  +3:5! @W  _ %A    - 2