[l(@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+yddl,Z,Wne-k rdZ.YnXdZ.dd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'g Z/e j0dd(Z1da2de j3d)dd*dd+dd,dd-dZ4d.dZ5gZ6dddd/d$Z7d0d%Z8e j9d1e j:Z;d2d3Z<Gd4ddZ=Gd5d d Z>d6d Z?Gd7d d Z@Gd8dde@ZAGd9d d e@ZBGd:d d e@ZCd;d<ZDGd=dde@ZEGd>ddZFGd?ddeFZGGd@ddZHGdAddeHe@ZIGdBddeHe@ZJejKZLGdCddZMGdDdde@eMZNGdEdde@eMZOGdFdGdGe@ZPGdHddePZQeRejSdIrhGdJdKdKePZTe/jUdKnGdLd d e@ZVGdMdde@ZWdNdOZXdPdQZYGdRdde@ZZdSdTZ[GdUdde@Z\GdVdde\Z]GdWdde@Z^dXZ_ej`dYkr>ddZlambZbmcZcnd[d"Zbd\d!ZciZdGd]d&d&ZeGd^d'd'eeZfdagd_d`ZhdaidadbZjdakdcddZldamdedfZnGdgdhdhZodidjZpdkdlZqdmdnZre jsdokr\ddpltmuZumvZvdqdrZwdsdtZxdudvZydwd#ZznNej`dYkrdxdyZ{dzd#Zzd{d|Z|d}dvZyn epZzeqZydS)~a An extensible library for opening URLs using a variety of protocols The simplest way to use this module is to call the urlopen function, which accepts a string containing a URL or a Request object (described below). It opens the URL and returns the results as file-like object; the returned object has some extra methods described below. The OpenerDirector manages a collection of Handler objects that do all the actual work. Each Handler implements a particular protocol or option. The OpenerDirector is a composite object that invokes the Handlers needed to open the requested URL. For example, the HTTPHandler performs HTTP GET and POST requests and deals with non-error returns. The HTTPRedirectHandler automatically deals with HTTP 301, 302, 303 and 307 redirect errors, and the HTTPDigestAuthHandler deals with digest authentication. urlopen(url, data=None) -- Basic usage is the same as original urllib. pass the url and optionally data to post to an HTTP URL, and get a file-like object back. One difference is that you can also pass a Request instance instead of URL. Raises a URLError (subclass of OSError); for HTTP errors, raises an HTTPError, which can also be treated as a valid response. build_opener -- Function that creates a new OpenerDirector instance. Will install the default handlers. Accepts one or more Handlers as arguments, either instances or Handler classes that it will instantiate. If one of the argument is a subclass of the default handler, the argument will be installed instead of the default. install_opener -- Installs a new opener as the default opener. objects of interest: OpenerDirector -- Sets up the User Agent as the Python-urllib client and manages the Handler classes, while dealing with requests and responses. Request -- An object that encapsulates the state of a request. The state can be as simple as the URL. It can also include extra HTTP headers, e.g. a User-Agent. BaseHandler -- internals: BaseHandler and parent _call_chain conventions Example usage: import urllib.request # set up authentication info authinfo = urllib.request.HTTPBasicAuthHandler() authinfo.add_password(realm='PDQ Application', uri='https://mahler:8092/site-updates.py', user='klem', passwd='geheim$parole') proxy_support = urllib.request.ProxyHandler({"http" : "http://ahad-haam:3128"}) # build a new opener that adds authentication and caching FTP handlers opener = urllib.request.build_opener(proxy_support, authinfo, urllib.request.CacheFTPHandler) # install it urllib.request.install_opener(opener) f = urllib.request.urlopen('http://www.python.org/') N)URLError HTTPErrorContentTooShortError)urlparseurlspliturljoinunwrapquoteunquote splittype splithost splitport splituser splitpasswd splitattr splitquery splitvaluesplittagto_bytesunquote_to_bytes urlunparse) addinfourl addclosehookFTRequestOpenerDirector BaseHandlerHTTPDefaultErrorHandlerHTTPRedirectHandlerHTTPCookieProcessor ProxyHandlerHTTPPasswordMgrHTTPPasswordMgrWithDefaultRealmAbstractBasicAuthHandlerHTTPBasicAuthHandlerProxyBasicAuthHandlerAbstractDigestAuthHandlerHTTPDigestAuthHandlerProxyDigestAuthHandler HTTPHandler FileHandler FTPHandlerCacheFTPHandler DataHandlerUnknownHandlerHTTPErrorProcessorurlopeninstall_opener build_opener pathname2url url2pathname getproxies urlretrieve urlcleanup URLopenerFancyURLopenercafilecapath cadefaultcontextc Cs|s|s|r|dk r-tdntsBtdntjtjjd|d|}td|}t|}nF|rtd|}t|}n"tdkrta}nt}|j |||S)NzDYou can't pass both context and any of cafile, capath, and cadefaultzSSL support not availabler:r;r=) ValueError _have_sslsslZcreate_default_contextZPurposeZ SERVER_AUTH HTTPSHandlerr1_openeropen) urldatatimeoutr:r;r<r=Z https_handleropenerrH$/usr/lib/python3.4/urllib/request.pyr/s$    cCs |adS)N)rB)rGrHrHrIr0scCst|\}}tjt||E}|j}|dkrb| rbtjj||fS|rzt|d}n(t j dd}|j }t j ||||f} d } d} d} d} d |krt|d } n|r|| | | nx]|j| }|s!Pn| t|7} |j|| d7} |r|| | | qqWWd QXWd QX| dkr| | krtd | | f| n| S)aW Retrieve a URL into a temporary location on disk. Requires a URL argument. If a filename is passed, it is used as the temporary file location. The reporthook argument should be a callable that accepts a block number, a read size, and the total file size of the URL target. The data argument should be valid URL encoded data. If a filename is passed and the URL points to a local resource, the result is a copy from local file to new file. Returns a tuple containing the path to the newly created data file as well as the resulting HTTPMessage object. filewbZdeleteFirzcontent-lengthzContent-LengthNz1retrieval incomplete: got only %i out of %i bytesi )r contextlibclosingr/infoospathnormpathrCtempfileZNamedTemporaryFilename_url_tempfilesappendintreadlenwriter)rDfilename reporthookrEZurl_typerSfpheaderstfpresultbssizerZblocknumblockrHrHrIr5sD       #c CsWx4tD],}ytj|Wqtk r2YqXqWtdd=trSdandS)N)rWrRunlinkOSErrorrB)Z temp_filerHrHrIr6s    z:\d+$cCsY|j}t|d}|dkr:|jdd}ntjd|d}|jS)zReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. rMHost)full_urlr get_header _cut_port_resublower)requestrDhostrHrHrI request_hosts   rrc@s*eZdZdidddddZeddZejddZejddZed d Zejd d Zejd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZdddZdd Zd!d"ZdS)#rNFc Cs||_i|_i|_d|_||_d|_x*|jD]\}}|j||qCW|dkr~t|}n||_ ||_ |r||_ ndS)N) rkr`unredirected_hdrs_datarE _tunnel_hostitems add_headerrrorigin_req_host unverifiablemethod) selfrDrEr`rxryrzkeyvaluerHrHrI__init__s         zRequest.__init__cCs&|jrdj|j|jS|jS)Nz{}#{})fragmentformat _full_url)r{rHrHrIrks zRequest.full_urlcCs8t||_t|j\|_|_|jdS)N)rrrr_parse)r{rDrHrHrIrkscCsd|_d|_d|_dS)Nri)rrselector)r{rHrHrIrk&s  cCs|jS)N)rt)r{rHrHrIrE,sz Request.datacCs>||jkr:||_|jdr:|jdq:ndS)NzContent-length)rt has_header remove_header)r{rErHrHrIrE0s cCs d|_dS)N)rE)r{rHrHrIrE:scCswt|j\|_}|jdkr=td|jnt|\|_|_|jrst|j|_ndS)Nzunknown url type: %r) r rtyper>rkr rqrr )r{restrHrHrIr>s  zRequest._parsecCs+|jdk rdnd}t|d|S)z3Return a string indicating the HTTP request method.NPOSTGETrz)rEgetattr)r{Zdefault_methodrHrHrI get_methodFszRequest.get_methodcCs|jS)N)rk)r{rHrHrI get_full_urlKszRequest.get_full_urlcCsJ|jdkr(|j r(|j|_n||_|j|_||_dS)Nhttps)rrurqrkr)r{rqrrHrHrI set_proxyNs   zRequest.set_proxycCs|j|jkS)N)rrk)r{rHrHrI has_proxyVszRequest.has_proxycCs||j|jrrrrr setdefaultbisectZinsortrXrr) r{handlerZaddedmethiprotocolZ conditionjkindlookuprrHrHrI add_handlersJ          zOpenerDirector.add_handlercCsdS)NrH)r{rHrHrIcloseszOpenerDirector.closec GsR|j|f}x9|D]1}t||}||}|dk r|SqWdS)N)rr) r{chainr meth_nameargsrrfuncrbrHrHrI _call_chains    zOpenerDirector._call_chainNc Cst|tr!t||}n|}|dk r?||_n||_|j}|d}x8|jj|gD]!}t||}||}qqW|j ||} |d}x;|j j|gD]$}t||}||| } qW| S)NZ_requestZ _response) isinstancestrrrErFrrrr_openr) r{fullurlrErFreqrrZ processorrrrHrHrIrCs"      zOpenerDirector.opencCsp|j|jdd|}|r%|S|j}|j|j||d|}|rW|S|j|jdd|S)NrZ default_openrunknown unknown_open)rrr)r{rrErbrrHrHrIrs   zOpenerDirector._opencGs|d kr<|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|}|r|S|r|dd f|}|j|SdS) Nhttprz http_error_%srMZ_errorrrhttp_error_default)zhttpzhttps)rr)r{protordictrZhttp_errZ orig_argsrbrHrHrIrs        zOpenerDirector.error) rrrr~rrrsocket_GLOBAL_DEFAULT_TIMEOUTrCrrrHrHrHrIrss  /  c GsCt}ttttttttt g }t t j drL|j tnt}xo|D]g}x^|D]V}t|trt||r|j|qqit||ri|j|qiqiWq\Wx|D]}|j|qWx|D]}|j|qWx6|D].}t|tr.|}n|j|q W|S)a*Create an opener object from a list of handlers. The opener will use several default handlers, including support for HTTP, FTP and when applicable HTTPS. If any of the handlers passed as arguments are subclasses of the default handlers, the default handlers will not be used. HTTPSConnection)rrr-r(rrr*r)r.r,rrclientrXrAsetrr issubclassaddremover)rrGZdefault_classesskipklassZcheckhrHrHrIr1s0           c@s:eZdZdZddZddZddZdS) ricCs ||_dS)N)parent)r{rrHrHrIr(szBaseHandler.add_parentcCsdS)NrH)r{rHrHrIr+szBaseHandler.closecCs#t|dsdS|j|jkS)N handler_orderT)rr)r{otherrHrHrI__lt__/szBaseHandler.__lt__N)rrrrrrrrHrHrHrIr%s   c@s.eZdZdZdZddZeZdS)r.zProcess HTTP error responses.icCsd|j|j|j}}}d|ko7dkns`|jjd|||||}n|S)Ni,r)codemsgrQrr)r{rprrrrrHrHrI http_response<s   z HTTPErrorProcessor.http_responseN)rrr__doc__rrhttps_responserHrHrHrIr.8s  c@seZdZddZdS)rcCst|j||||dS)N)rrk)r{rr_rrrrHrHrIrJsz*HTTPDefaultErrorHandler.http_error_defaultN)rrrrrHrHrHrIrIs c@sHeZdZdZdZddZddZeZZZ dZ dS) r c s|j}|dkr$|dkp9|dko9|dksZt|j||||n|jdd }dtfd d |jjD}t|d|d|jddS)aReturn a Request or None in response to a redirect. This is called by the http_error_30x methods when a redirection response is received. If a redirection should take place, return a new Request to allow http_error_30x to perform the redirect. Otherwise, raise HTTPError if no-one else should try to handle this url. Return None if you can't but another Handler might. -./3rHEADr z%20content-length content-typec3s3|])\}}|jkr||fVqdS)N)ro).0kv)CONTENT_HEADERSrHrI ls z7HTTPRedirectHandler.redirect_request..r`rxryT)rrrr)zGETr)rrr)rr) rrrkreplacerr`rvrrx) r{rr_rrr`newurlmZ newheadersrH)rrIrUs (  z$HTTPRedirectHandler.redirect_requestc Csd|kr|d}nd|kr2|d}ndSt|}|jdkrvt||d||f||n|jst|}d|d rrr) proxyrZr_scheme authorityendZuserinfohostportuserpasswordrHrHrI _parse_proxys     r c@s1eZdZdZdddZddZdS)rdNc Cs|dkrt}nt|ds3td||_x@|jD]2\}}t|d||||jddqIWdS)Nkeyszproxies must be a mappingz%s_opencSs||||S)NrH)rrrrrHrHrIsz'ProxyHandler.__init__..)r4rAssertionErrorproxiesrvsetattrr)r{rrrDrHrHrIr~s    zProxyHandler.__init__c Cs|j}t|\}}}}|dkr6|}n|jrRt|jrRdS|r|rdt|t|f} tj| jjd} |j dd| nt|}|j ||||ks|dkrdS|j j |d|j SdS)Nz%s:%sasciizProxy-authorizationzBasic rrF)rr rq proxy_bypassr base64 b64encodeencodedecoderwrrrCrF) r{rrrZ orig_typeZ proxy_typer r r Z user_passZcredsrHrHrIrs       zProxyHandler.proxy_open)rrrrr~rrHrHrHrIrs  c@sOeZdZddZddZddZddd Zd d Zd S) r cCs i|_dS)N)passwd)r{rHrHrIr~szHTTPPasswordMgr.__init__cst|tr|g}n|jkr:ij| s z0HTTPPasswordMgr.add_password..)TF)rrrtuple)r{realmrr rZ reduced_urirH)rr{rI add_passwords  zHTTPPasswordMgr.add_passwordc Cs|jj|i}xddD]\}|j||}xA|jD]3\}}x$|D]}|j||rT|SqTWqAWqWdS)NTF)TF)NN)rrrrv is_suburi) r{r!authuriZdomainsrZreduced_authuriZurisZauthinforrHrHrIfind_user_password s  z"HTTPPasswordMgr.find_user_passwordTc Cst|}|dr=|d}|d}|dp7d}nd}|}d}t|\}}|r|dkr|dk ridd6dd 6j|} | dk rd || f}qn||fS) z@Accept authority or URI and extract only the authority and path.rMrrrNPrirz%s:%d)rr r) r{rrpartsrrrSrqportZdportrHrHrIrs         zHTTPPasswordMgr.reduce_uricCsi||krdS|d|dkr(dStj|d|df}t|t|dkredSdS)zcCheck if test is below base in a URI tree Both args must be URIs in reduced form. TrFrM) posixpath commonprefixr[)r{basetestZcommonrHrHrIr#-s zHTTPPasswordMgr.is_suburiN)rrrr~r"r%rr#rHrHrHrIr s   c@seZdZddZdS)r!cCsDtj|||\}}|dk r1||fStj|d|S)N)r r%)r{r!r$r r rHrHrIr%>s    z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)rrrr%rHrHrHrIr!<s c@sLeZdZejdejZdddZddZddZ dS) r"z1(?:.*,)*[ ]*([^ ]+)[ ]+realm=(["']?)([^"']*)\2NcCs4|dkrt}n||_|jj|_dS)N)r rr")r{Z password_mgrrHrHrIr~Ts   z!AbstractBasicAuthHandler.__init__c Cs|j|d}|r|jd}|jdkrMtd|qtjj|}|r|j\}}}|dkrtj dt dn|jdkr|j |||SqndS) NrbasiczDAbstractBasicAuthHandler does not support the following scheme: '%s'"'zBasic Auth Realm was unquotedr)r.r/) rsplitror>r"rxsearchgroupswarningswarn UserWarningretry_http_basic_auth) r{authreqrqrr`rZmor r!rHrHrIhttp_error_auth_reqedZs    z.AbstractBasicAuthHandler.http_error_auth_reqedcCs|jj||\}}|dk rd||f}dtj|jjd}|j|jd|krxdS|j|j||j j |d|j SdSdS)Nz%s:%szBasic rrF) rr%rrrrrl auth_headerrrrCrF)r{rqrr!r pwrawauthrHrHrIr7ps "z.AbstractBasicAuthHandler.retry_http_basic_auth) rrrrecompileIr1r~r9r7rHrHrHrIr"Fs    c@s"eZdZdZddZdS)r# AuthorizationcCs%|j}|jd|||}|S)Nzwww-authenticate)rkr9)r{rr_rrr`rDrrHrHrIhttp_error_401s  z#HTTPBasicAuthHandler.http_error_401N)rrrr:rBrHrHrHrIr#}s c@s"eZdZdZddZdS)r$zProxy-authorizationcCs%|j}|jd|||}|S)Nzproxy-authenticate)rqr9)r{rr_rrr`rrrHrHrIhttp_error_407s  z$ProxyBasicAuthHandler.http_error_407N)rrrr:rCrHrHrHrIr$s c@sseZdZdddZddZddZdd Zd d Zd d ZddZ ddZ dS)r%NcCsO|dkrt}n||_|jj|_d|_d|_d|_dS)Nr)r rr"retried nonce_count last_nonce)r{rrHrHrIr~s     z"AbstractDigestAuthHandler.__init__cCs d|_dS)Nr)rD)r{rHrHrIreset_retry_countsz+AbstractDigestAuthHandler.reset_retry_countcCs|j|d}|jdkr?t|jdd|dn|jd7_|r|jd}|jdkr|j||S|jdkrtd|qndS) Nizdigest auth failedrMrZdigestr-zEAbstractDigestAuthHandler does not support the following scheme: '%s')rrDrrkr0roretry_http_digest_authr>)r{r:rqrr`r8rrHrHrIr9sz/AbstractDigestAuthHandler.http_error_auth_reqedcCs|jdd\}}ttdt|}|j||}|rd|}|jj|jd|krwdS|j|j||j j |d|j }|SdS)NrrMz Digest %srF) r0parse_keqv_listfilterparse_http_listget_authorizationr`rr:rrrCrF)r{rr=tokenZ challengechalZauth_valZresprHrHrIrIs z0AbstractDigestAuthHandler.retry_http_digest_authcCsXd|j|tjf}|jdtd}tj|j}|ddS)Nz %s:%s:%s:rrL)rEtimeZctimer _randombyteshashlibsha1 hexdigest)r{noncesbdigrHrHrI get_cnoncesz$AbstractDigestAuthHandler.get_cnoncecCs_yK|d}|d}|jd}|jdd}|jdd}Wntk rcdSYnX|j|\}} |dkrdS|jj||j\} } | dkrdS|jdk r|j|j|} nd} d| || f} d|j|j f}|d kr||j kr=|j d 7_ nd |_ ||_ d |j }|j |}d ||||||f}| || |}nD|dkr| || d|||f}nt d |d| |||j |f}|r|d|7}n| r0|d| 7}n|d|7}|r[|d||f7}n|S)Nr!rVqop algorithmMD5opaquez%s:%s:%sz%s:%sr=rMz%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_implsrr%rkrEget_entity_digestrrrFrErZr)r{rrOr!rVr[r\r^HKDr r;ZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr+rHrHrIrMsV             ( z+AbstractDigestAuthHandler.get_authorizationcsR|dkrddn|dkr6ddnfdd}|fS)Nr]cSstj|jdjS)Nr)rSZmd5rrU)xrHrHrIrsz?AbstractDigestAuthHandler.get_algorithm_impls..ZSHAcSstj|jdjS)Nr)rSrTrrU)rdrHrHrIrscsd||fS)Nz%s:%srH)rWd)rbrHrIr srH)r{r\rcrH)rbrIr`s   z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrH)r{rErOrHrHrIra#sz+AbstractDigestAuthHandler.get_entity_digest) rrrr~rGr9rIrZrMr`rarHrHrHrIr%s    < c@s.eZdZdZdZdZddZdS)r&zAn authentication protocol defined by RFC 2069 Digest authentication improves on basic authentication because it does not transmit passwords in the clear. rAicCs9t|jd}|jd|||}|j|S)NrMzwww-authenticate)rrkr9rG)r{rr_rrr`rqretryrHrHrIrB2s   z$HTTPDigestAuthHandler.http_error_401N)rrrrr:rrBrHrHrHrIr&(s c@s(eZdZdZdZddZdS)r'zProxy-AuthorizationicCs/|j}|jd|||}|j|S)Nzproxy-authenticate)rqr9rG)r{rr_rrr`rqrfrHrHrIrC?s    z%ProxyDigestAuthHandler.http_error_407N)rrrr:rrCrHrHrHrIr':s c@sCeZdZdddZddZddZdd Zd S) AbstractHTTPHandlerrcCs ||_dS)N) _debuglevel)r{ debuglevelrHrHrIr~HszAbstractHTTPHandler.__init__cCs ||_dS)N)rh)r{levelrHrHrIset_http_debuglevelKsz'AbstractHTTPHandler.set_http_debuglevelc Cs|j}|stdn|jdk r|j}t|trZd}t|n|jds||jddn|jdsyt|}WnCtk rt|t j rt dt ||fnYqX|jddt ||jqn|}|jrDt|j\}}t|\}} n|jdsf|jd|nxH|jjD]:\} } | j} |j| ss|j| | qsqsW|S) Nz no host givenzLPOST data should be bytes or an iterable of bytes. It cannot be of type str.z Content-typez!application/x-www-form-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drj)rqrrErrrrr memoryview collectionsIterabler>rr[itemsizerr rr rrr) r{rprqrErZmvZsel_hostrZselZsel_pathrVr}rHrHrI do_request_NsB     !  zAbstractHTTPHandler.do_request_c  s|j}|stdn||d|j|}t|jjtfdd|jjDdd.r Connectioncss'|]\}}|j|fVqdS)N)title)rrVrrHrHrIrszProxy-Authorizationr`N)rqrrFrrsrr`rvruZ set_tunnelrprrrErh getresponserZsockrrDreasonr) r{Z http_classrZhttp_conn_argsrqrZtunnel_headersZproxy_auth_hdrerrrrH)r`rIrvs< /    &     zAbstractHTTPHandler.do_openN)rrrr~rkrprrHrHrHrIrgFs   (rgc@s%eZdZddZejZdS)r(cCs|jtjj|S)N)rrrHTTPConnection)r{rrHrHrI http_openszHTTPHandler.http_openN)rrrrwrgrp http_requestrHrHrHrIr(s  rc@s:eZdZdddddZddZejZdS)rArNcCs&tj||||_||_dS)N)rgr~_context_check_hostname)r{rir=check_hostnamerHrHrIr~s zHTTPSHandler.__init__cCs(|jtjj|d|jd|jS)Nr=r{)rrrrryrz)r{rrHrHrI https_openszHTTPSHandler.https_open)rrrr~r|rgrp https_requestrHrHrHrIrAs  rAc@sCeZdZdddZddZddZeZeZdS)rNcCs7ddl}|dkr*|jj}n||_dS)Nr)Zhttp.cookiejar cookiejarZ CookieJar)r{r~rrHrHrIr~s  zHTTPCookieProcessor.__init__cCs|jj||S)N)r~Zadd_cookie_header)r{rprHrHrIrxsz HTTPCookieProcessor.http_requestcCs|jj|||S)N)r~Zextract_cookies)r{rprrHrHrIrsz!HTTPCookieProcessor.http_response)rrrr~rxrr}rrHrHrHrIrs   c@seZdZddZdS)r-cCs|j}td|dS)Nzunknown url type: %s)rr)r{rrrHrHrIrs zUnknownHandler.unknown_openN)rrrrrHrHrHrIr-s cCspi}xc|D][}|jdd\}}|ddkr^|ddkr^|dd}n|||Parse list of key=value strings where keys are not duplicated.=rMrr.rNrN)r0)lZparsedZeltrrrHrHrIrJs  rJcCsg}d}d}}x|D]}|r?||7}d}qn|r|dkr]d}qn|dkrrd}n||7}qn|dkr|j|d}qn|dkrd}n||7}qW|r|j|ndd|DS) apParse 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. Neither commas nor quotes count if they are escaped. Only double-quotes count, not single-quotes. riF\Tr.,cSsg|]}|jqSrH)strip)rpartrHrHrIrs z#parse_http_list..)rX)rWZresrescaper ZcurrHrHrIrLs4            rLc@s:eZdZddZdZddZddZdS)r)cCs|j}|dddkrt|dddkrt|jrt|jdkrt|j|jkrtdqn |j|SdS)Nrz//r9r localhostz-file:// scheme is supported only on localhost)rrq get_namesropen_local_file)r{rrDrHrHrI file_opens  5zFileHandler.file_openNc Cs}tjdkrvy7ttjddtjtjdt_Wqvtjk rrtjdft_YqvXntjS)Nrr)r)namesr rgethostbyname_ex gethostnamegaierror gethostbyname)r{rHrHrIr(s$zFileHandler.get_namescCsaddl}ddl}|j}|j}t|}ytj|}|j}|jj |j dd} |j |d} |j d| pd|| f} |rt |\}} n| s| rt||jkr|rd||} n d|} tt|d| | SWn1tk rP}zt|WYdd}~XnXtddS) NrusegmtTz6Content-type: %s Content-length: %d Last-modified: %s z text/plainzfile://rbzfile not on local host) email.utils mimetypesrqrr3rRstatst_sizeutils formatdatest_mtime guess_typemessage_from_stringr _safe_gethostbynamerrrCrhr)r{remailrrqr]Z localfilestatsrdmodifiedmtyper`r(ZorigurlexprHrHrIr3s0       zFileHandler.open_local_file)rrrrrrrrHrHrHrIr)s   c Cs1ytj|SWntjk r,dSYnXdS)N)rrr)rqrHrHrIrOsrc@s(eZdZddZddZdS)r*cCsddl}ddl}|j}|s6tdnt|\}}|dkr`|j}n t|}t|\}}|rt|\}}nd}t |}|pd}|pd}yt j |}Wn1t k r }zt|WYdd}~XnXt |j\} } | jd} ttt | } | dd| d} } | r| d r| dd} ny|j||||| |j} | rdpd}xM| D]E}t|\}}|jdkr|dkr|j}qqW| j| |\}}d}|j|jd}|rS|d |7}n|dk r||dkr||d|7}ntj|}t|||jSWnQ|jk r}z.td|}|jtj dWYdd}~XnXdS)Nrzftp error: no host givenrirrMr@DraArrezContent-type: %s zContent-length: %d z ftp error: %rrrNrN)rrrr@rer)!ftplibrrqrr FTP_PORTrYrrr rrrhrrr0rmap connect_ftprFrroupperretrfilerrkrrr all_errorswith_tracebacksysexc_info)r{rrrrqr(r rrrSattrsdirsrJZfwrattrr}r_retrlenr`rrexcrHrHrIftp_openVs\         !  zFTPHandler.ftp_openc Cst||||||ddS)N persistentF) ftpwrapper)r{r rrqr(rrFrHrHrIrszFTPHandler.connect_ftpN)rrrrrrHrHrHrIr*Us  5c@sXeZdZddZddZddZddZd d Zd d Zd S)r+cCs1i|_i|_d|_d|_d|_dS)Nr<rP)cacherFsoonestdelay max_conns)r{rHrHrIr~s     zCacheFTPHandler.__init__cCs ||_dS)N)r)r{trHrHrI setTimeoutszCacheFTPHandler.setTimeoutcCs ||_dS)N)r)r{rrHrHrI setMaxConnsszCacheFTPHandler.setMaxConnscCs|||dj||f}||jkrJtj|j|j|YqXqW|jdd=n|jro|jjndS)N)rrrhrr)r{rJrHrHrIr*s    zURLopener.cleanupcGs|jj|dS)zdAdd a header to be used by the HTTP interface only e.g. u.addheader('Accept', 'sound/basic')N)rrX)r{rrHrHrI addheader8szURLopener.addheadercCstt|}t|dd}|jrn||jkrn|j|\}}t|d}t|||St|\}}|sd}n||jkr|j|}t|\}} t| \} } | |f}nd}d|} ||_ | j dd} t || sD|r1|j |||S|j ||Sny9|dkrft|| |St|| ||SWnattfk rYnGtk r} z'td | jtjd WYdd} ~ XnXdS) z6Use URLopener().open(file) instead of open(file, 'r').safez%/:=&?~#+!$,;'@()*[]|rrJNZopen_-rz socket errorr)rrr rrCrr rr rrropen_unknown_proxy open_unknownrrrrhrrr)r{rrEr]r`r_urltyperDr proxyhostrqrrVrrHrHrIrC>s<     zURLopener.opencCs(t|\}}tdd|dS)z/Overridable interface to open unknown URL type.z url errorzunknown url typeN)r rh)r{rrErrDrHrHrIrbszURLopener.open_unknowncCs,t|\}}tdd||dS)z/Overridable interface to open unknown URL type.z url errorzinvalid proxy for %sN)r rh)r{rrrErrDrHrHrIrgszURLopener.open_unknown_proxyc Cstt|}|jr5||jkr5|j|St|\}}|dkr| sf|dkryC|j|}|j}|jtt|d|fSWqt k r} zWYdd} ~ XqXn|j ||}z|j} |r t |d} nddl } t|\} }t|p7d\} }t |pOd\}} t |pgd\}} tjj|d}| j|\}}|jj|tj|d} z|| f}|jdk r||j|||||nx]|j|}|sZPn|t|7}| j||d7}|rA||||qAqAWWd| jXWd|jX|dkr||krtd ||f|n|S)ztretrieve(url) returns (filename, headers) for a local object or (tempfilename, headers) for a remote object.NrJrMrKrriirLzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rN)rrrr rrQrr3r rhrCrUrrrRrSsplitextZmkstemprrXfdopenrYrZr[r\r)r{rDr]r^rErZurl1r_rrr`rarUZgarbagerSsuffixfdrbrcrdrZrerfrHrHrIretrievemsl          zURLopener.retrievecCsd}d}t|tr]t|\}}|rTt|\}}t|}n|}n|\}}t|\}}t|\} } | }d}| jdkrd}n^t| \}} |rt|\}}n|rd| || f}nt|r|}n|s*tddn|r]t|}t j |j j d} nd} |rt|}t j |j j d} nd} ||} i}| rd| |dDefault error handler: close the connection and raise OSError.N)rr)r{rDr_rrr`rHrHrIrs zURLopener.http_error_defaultcCs"tjj|d|jd|jS)Nrr)rrrrr)r{rqrHrHrI_https_connection%s zURLopener._https_connectioncCs|j|j||S)zUse HTTPS protocol.)rr)r{rDrErHrHrI open_https*szURLopener.open_httpscCst|tstdn|dddkru|dddkru|ddjdkrutd n |j|SdS) z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNrz//r9r z localhost/z-file:// scheme is supported only on localhost)rrrror>r)r{rDrHrHrI open_file.s HzURLopener.open_filecCsddl}ddl}t|\}}t|}ytj|}Wn:tk r}zt|j|j WYdd}~XnX|j } |j j |j dd} |j|d} |jd| pd| | f} |s'|} |dddkrd |} ntt|d | | St|\}}| rtj|tftkr|} |dddkrd |} n)|dd d krtd |ntt|d | | StddS)zUse local file.rNrTz6Content-Type: %s Content-Length: %d Last-modified: %s z text/plainrMrzfile://rrz./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)rrr r3rRrrhrstrerrorr]rrrrrrrrCr rrrthishostr>)r{rDrrrqrJZ localnamererdrrr`Zurlfiler(rHrHrIr7s:   (  " zURLopener.open_local_filecCst|tstdnddl}t|\}}|sQtdnt|\}}t|\}}|rt|\}}nd}t|}t|pd}t|pd}t j |}|sddl }|j }n t |}t|\}} t|}|jd} | dd| d} } | rk| d rk| dd} n| r| d rd| dt j |j djd}n t|}|jdt||jd |j|dj|}tj|}tj|}t|||S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedrrMz data errorz bad data URLztext/plain;charset=US-ASCII;rrNrizDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %d )rrrr0r>rhrfindrXrQZstrftimeZgmtimerrrrr r[rrrrStringIOr) r{rDrErZsemiencodingrr`frHrHrI open_datas6  " $   zURLopener.open_data)rrrrrrrr~rrrrrCrrrrrrrr?rrrrrrrHrHrHrIr7s.      $B \   :c@seZdZdZddZddZdddZd d Zdd d Zdd dZ dddZ ddddZ ddddZ dddZ dddZdddZdddZddd Zd!d"ZdS)#r8z?Derived class with handlers for errors we can handle (perhaps).cOs2tj|||i|_d|_d|_dS)Nrr)r7r~ auth_cachetriesmaxtries)r{rkwargsrHrHrIr~s  zFancyURLopener.__init__cCst||d||S)z3Default error handling -- don't raise an exception.zhttp:)r)r{rDr_rrr`rHrHrIrsz!FancyURLopener.http_error_defaultNc Cs|jd7_|jrm|j|jkrmt|drE|j}n |j}d|_|||dd|S|j||||||}d|_|S)z%Error 302 -- relocated (temporarily).rMhttp_error_500riz)Internal Server Error: Redirect Recursion)rrrrrredirect_internal) r{rDr_rrr`rErrbrHrHrIrs       zFancyURLopener.http_error_302c Csd|kr|d}nd|kr2|d}ndS|jt|jd||}t|}|jd krt|||d|||n|j|S) Nrrrrrrriz( Redirection to url '%s' is not allowed.)zhttpzhttpszftpri)rrrrrrrC) r{rDr_rrr`rErrrHrHrIrs       z FancyURLopener.redirect_internalcCs|j||||||S)z*Error 301 -- also relocated (permanently).)r)r{rDr_rrr`rErHrHrIrszFancyURLopener.http_error_301cCs|j||||||S)z;Error 303 -- also relocated (essentially identical to 302).)r)r{rDr_rrr`rErHrHrIrszFancyURLopener.http_error_303cCsE|dkr(|j||||||S|j|||||SdS)z1Error 307 -- relocated, but turn POST into error.N)rr)r{rDr_rrr`rErHrHrIrs zFancyURLopener.http_error_307Fc Cs$d|kr+tj||||||n|d}tjd|} | sltj||||||n| j\} } | jdkrtj||||||n|stj||||||nd|jd} |dkrt|| || St|| || |SdS)z_Error 401 -- authentication required. This function supports Basic authentication only.zwww-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"r-Zretry_ _basic_authN)r7rr>matchr3rorr) r{rDr_rrr`rErfstuffrrr!rVrHrHrIrBs&    zFancyURLopener.http_error_401c Cs$d|kr+tj||||||n|d}tjd|} | sltj||||||n| j\} } | jdkrtj||||||n|stj||||||nd|jd} |dkrt|| || St|| || |SdS)zeError 407 -- proxy authentication required. This function supports Basic authentication only.zproxy-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"r-Z retry_proxy_rN)r7rr>rr3rorr) r{rDr_rrr`rErfrrrr!rVrHrHrIrCs&    zFancyURLopener.http_error_407cCs t|\}}d||}|jd}t|\}} t| \} } | jdd} | | d} |j| || \} } | p| sdSdt| ddt| dd| f} d| | |jd<|dkr|j|S|j||SdS)Nzhttp://r@rMz%s:%s@%srri)r rr rget_user_passwdr rC)r{rDr!rErqrrrrr proxyselectorrr rrHrHrIretry_proxy_http_basic_auth5s     z*FancyURLopener.retry_proxy_http_basic_authcCs t|\}}d||}|jd}t|\}} t| \} } | jdd} | | d} |j| || \} } | p| sdSdt| ddt| dd| f} d| | |jd<|dkr|j|S|j||SdS)Nzhttps://rrrMz%s:%s@%srri)r rr rrr rC)r{rDr!rErqrrrrrrrr rrHrHrIretry_proxy_https_basic_authGs     z+FancyURLopener.retry_proxy_https_basic_authc Cst|\}}|jdd}||d}|j|||\}}|pY|s`dSdt|ddt|dd|f}d||} |dkr|j| S|j| |SdS)NrrMz%s:%s@%srrizhttp://)r rrr rC) r{rDr!rErqrrr rrrHrHrIr7Ys   z$FancyURLopener.retry_http_basic_authc Cst|\}}|jdd}||d}|j|||\}}|pY|s`dSdt|ddt|dd|f}d||} |dkr|j| S|j| |SdS)NrrMz%s:%s@%srrizhttps://)r rrr rC) r{rDr!rErqrrr rrrHrHrIretry_https_basic_authgs   z%FancyURLopener.retry_https_basic_authrcCs|d|j}||jkrD|r6|j|=qD|j|Sn|j||\}}|sh|r~||f|j| proxy server URL mappings. Scan the environment for variables named _proxy; this seems to be the standard convention. If you need a different way, you can pass a proxies dictionary to the [Fancy]URLopener constructor. N_proxyZREQUEST_METHODrii)rRenvironrvror)rrVr}rHrHrIgetproxies_environment s  r$cCstjjddp'tjjdd}|dkr:dSt|\}}dd|jdD}x6|D].}|ro|j|s|j|rodSqoWd S) zTest if proxies should not be used for a particular host. Checks the environment for a variable named no_proxy, which should be a list of DNS suffixes separated by commas, or '*' for all hosts. no_proxyriZNO_PROXY*rMcSsg|]}|jqSrH)r)rrrHrHrIr5 s z,proxy_bypass_environment..rr)rRr#rr r0r)rqr%hostonlyr(Z no_proxy_listrVrHrHrIproxy_bypass_environment( s*  $r(c Csddlm}t|\}}dd}d|krK|drKdSnd}x+|jd fD]}|svqdntjd |}|dk rh|dkrytj|}||}Wqtk rwdYqXn||jd } |jd } | dkr-d |jd j dd } nt | d d} d| } || ?| | ?kr{dSqd|||rddSqdWdS)aj Return True iff this host shouldn't be accessed using a proxy This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. proxy_settings come from _scproxy._get_proxy_settings or get mocked ie: { 'exclude_simple': bool, 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.1', '10.0/16'] } r)fnmatchcSs|jd}ttt|}t|dkrY|ddddgdd}n|dd>|dd>B|dd>B|d BS) N.rrrMrPrrLr9)r0rrrYr[)ZipAddrr'rHrHrIip2numO s #z,_proxy_bypass_macosx_sysconf..ip2numr*Zexclude_simpleTN exceptionsz(\d+(?:\.\d+)*)(/\d+)?rMrrL F) r)r rr>rrrrhgroupcountrY) rqproxy_settingsr)r'r(r,ZhostIPr}rr+maskrHrHrI_proxy_bypass_macosx_sysconf? s:         # r3darwin)_get_proxy_settings _get_proxiescCst}t||S)N)r5r3)rqr1rHrHrIproxy_bypass_macosx_sysconf~ s r7cCstS)zReturn a dictionary of scheme -> proxy server URL mappings. This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. )r6rHrHrHrIgetproxies_macosx_sysconf sr8cCs!trt|St|SdS)N)r$r(r7)rqrHrHrIr s  rcCstptS)N)r$r8rHrHrHrIr4 scCsli}yddl}Wntk r.|SYnXy|j|jd}|j|dd}|r?t|j|dd}d|krx|jdD]M}|jdd\}}tjd |sd ||f}n||| proxy server URL mappings. Win32 uses the registry to store proxies. rNz;Software\Microsoft\Windows\CurrentVersion\Internet Settings ProxyEnableZ ProxyServerrrrMz ^([^/:]+)://z%s://%srHzhttp:rz http://%sz https://%srzftp://%sr) winreg ImportErrorOpenKeyHKEY_CURRENT_USER QueryValueExrr0r>rZCloserhr>r)rr:internetSettings proxyEnableZ proxyServerprZaddressrHrHrIgetproxies_registry s8         rBcCstptS)zReturn a dictionary of scheme -> proxy server URL mappings. Returns settings gathered from the environment, if specified, or the registry. )r$rBrHrHrHrIr4 sc &Csyddl}Wntk r(dSYnXyK|j|jd}|j|dd}t|j|dd}Wntk rdSYnX| s| rdSt|\}}|g}y/tj |}||kr|j |nWntk rYnXy/tj |}||kr,|j |nWntk rAYnX|j d}x|D]} | dkr}d|kr}dSn| j dd } | j d d } | j d d} x*|D]"} tj| | tjrdSqWqXWdS) Nrz;Software\Microsoft\Windows\CurrentVersion\Internet Settingsr9Z ProxyOverriderzr*rMz\.r&z.*?)r:r;r<r=r>rrhr rrrXZgetfqdnr0rr>rr@) rqr:r?r@Z proxyOverrideZrawHostr(ZaddrZfqdnr,rrHrHrIproxy_bypass_registry sR                   rDcCs!trt|St|SdS)zReturn a dictionary of scheme -> proxy server URL mappings. Returns settings gathered from the environment, if specified, or the registry. N)r$r(rD)rqrHrHrIr s  )}rrrrrSZ http.clientrrrRr)r>rrrQrmrUrOr4Z urllib.errorrrrZ urllib.parserrrrr r r r r rrrrrrrrrZurllib.responserrr@r;r?__all__rrrBrr/r0rWr5r6r?ASCIIrmrrrrr1rr.rrr rr r!r"r#r$urandomrRr%r&r'rgr(rrrArXrr-rJrLr)rr*r+r,rrVZ nturl2pathr3r2rr7r8rrrrrrrrrr$r(r3platformZ_scproxyr5r6r7r8rr4rBrDrHrHrHrIDs                v         ! ?  n $h *@ 7   q  +4 :5!      [   <   - 2