B °-_“‡ã!@sÆdZddlZddlZddlZddlZddlZddlZddlZddl 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+y ddl,Z,Wne-k rdZ.YnXdZ.dd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(g!Z/d)e j0dd*…Z1da2de j3fddddd+œd,d„Z4d-d „Z5gZ6d~d.d%„Z7d/d&„Z8e  9d0e j:¡Z;d1d2„Zd5d!„Z?Gd6d „d ƒZ@Gd7d„de@ƒZAGd8d „d e@ƒZBGd9d „d e@ƒZCd:d;„ZDGdd„deFƒZGGd?d„deGƒZHGd@d„dƒZIGdAd„deIe@ƒZJGdBd„deIe@ƒZKejLZMGdCd„dƒZNGdDd„de@eNƒZOGdEd„de@eNƒZPGdFdG„dGe@ƒZQGdHd„deQƒZReSejTdIƒr*GdJdK„dKeQƒZUe/ VdK¡GdLd „d e@ƒZWGdMd„de@ƒZXdNdO„ZYdPdQ„ZZGdRd„de@ƒZ[dSdT„Z\GdUd„de@ƒZ]GdVd„de]ƒZ^GdWd„de@ƒZ_dXZ`ejadYkrÄddZlbmcZcmdZdnd[d#„Zcd\d"„ZdiZeGd]d'„d'ƒZfGd^d(„d(efƒZgdahd_d`„Zidajdadb„Zkdaldcdd„Zmdandedf„ZoGdgdh„dhƒZpdidj„Zqddkdl„Zrdmdn„Zse jtdokrŒddplumvZvmwZwdqdr„Zxdsdt„Zydudv„Zzdwd$„Z{n6ejadYkrºdxdy„Z|dzd$„Z{d{d|„Z}d}dv„ZzneqZ{erZzdS)€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Ú HTTPErrorÚContentTooShortError)ÚurlparseÚurlsplitÚurljoinÚunwrapÚquoteÚunquoteÚ splittypeÚ splithostÚ splitportÚ splituserÚ splitpasswdÚ splitattrÚ splitqueryÚ splitvalueÚsplittagÚto_bytesÚunquote_to_bytesÚ urlunparse)Ú addinfourlÚ addclosehookFTÚRequestÚOpenerDirectorÚ BaseHandlerÚHTTPDefaultErrorHandlerÚHTTPRedirectHandlerÚHTTPCookieProcessorÚ ProxyHandlerÚHTTPPasswordMgrÚHTTPPasswordMgrWithDefaultRealmÚHTTPPasswordMgrWithPriorAuthÚAbstractBasicAuthHandlerÚHTTPBasicAuthHandlerÚProxyBasicAuthHandlerÚAbstractDigestAuthHandlerÚHTTPDigestAuthHandlerÚProxyDigestAuthHandlerÚ HTTPHandlerÚ FileHandlerÚ FTPHandlerÚCacheFTPHandlerÚ DataHandlerÚUnknownHandlerÚHTTPErrorProcessorÚurlopenÚinstall_openerÚ build_openerÚ pathname2urlÚ url2pathnameÚ getproxiesÚ urlretrieveÚ urlcleanupÚ URLopenerÚFancyURLopenerz%d.%dé)ÚcafileÚcapathÚ cadefaultÚcontextc Cs¤|s |s |rfddl}| dtd¡|dk r2tdƒ‚ts>tdƒ‚tjtjj||d}t |d}t |ƒ} n0|r~t |d}t |ƒ} nt dkr’t ƒa } nt } |   |||¡S) a$ Open the URL url, which can be either a string or a Request object. *data* must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details. urllib.request module uses HTTP/1.1 and includes a "Connection:close" header in its HTTP requests. The optional *timeout* parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This only works for HTTP, HTTPS and FTP connections. If *context* is specified, it must be a ssl.SSLContext instance describing the various SSL options. See HTTPSConnection for more details. The optional *cafile* and *capath* parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files. More information can be found in ssl.SSLContext.load_verify_locations(). The *cadefault* parameter is ignored. This function always returns an object which can work as a context manager and has methods such as * geturl() - return the URL of the resource retrieved, commonly used to determine if a redirect was followed * info() - return the meta-information of the page, such as headers, in the form of an email.message_from_string() instance (see Quick Reference to HTTP Headers) * getcode() - return the HTTP status code of the response. Raises URLError on errors. For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse object slightly modified. In addition to the three new methods above, the msg attribute contains the same information as the reason attribute --- the reason phrase returned by the server --- instead of the response headers as it is specified in the documentation for HTTPResponse. For FTP, file, and data URLs and requests explicitly handled by legacy URLopener and FancyURLopener classes, this function returns a urllib.response.addinfourl object. Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandler to ensure this never happens). In addition, if proxy settings are detected (for example, when a *_proxy environment variable like http_proxy is set), ProxyHandler is default installed and makes sure the requests are handled through the proxy. rNzJcafile, 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>) ÚwarningsÚwarnÚDeprecationWarningÚ ValueErrorÚ _have_sslÚsslZcreate_default_contextZPurposeZ SERVER_AUTHÚ HTTPSHandlerr2Ú_openerÚopen) ÚurlÚdataÚtimeoutr;r<r=r>r?Z https_handlerÚopener©rLú$/usr/lib/python3.7/urllib/request.pyr0‹s*<       cCs|adS)N)rF)rKrLrLrMr1àsc Cs2t|ƒ\}}t t||ƒ¡æ}| ¡}|dkrB|sBtj |¡|fS|rRt|dƒ}nt j dd}|j }t   |¡|†||f} d} d} d} d} d|kr t|d ƒ} |r°|| | | ƒxB| | ¡}|sÂP| t|ƒ7} | |¡| d 7} |r²|| | | ƒq²WWd QRXWd QRX| dkr.| | kr.td | | f| ƒ‚| 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. ÚfileÚwbF)Údeletei éÿÿÿÿrzcontent-lengthzContent-LengthéNz1retrieval incomplete: got only %i out of %i bytes)r Ú contextlibÚclosingr0ÚinfoÚosÚpathÚnormpathrGÚtempfileZNamedTemporaryFileÚnameÚ_url_tempfilesÚappendÚintÚreadÚlenÚwriter)rHÚfilenameÚ reporthookrIZurl_typerWÚfpÚheadersÚtfpÚresultÚbsÚsizer^ÚblocknumÚblockrLrLrMr6åsD          $c CsHx0tD](}yt |¡Wqtk r,YqXqWtdd…=trDdadS)z0Clean up temporary files from urlretrieve calls.N)r[rVÚunlinkÚOSErrorrF)Z temp_filerLrLrMr7$s   z:\d+$cCs<|j}t|ƒd}|dkr&| dd¡}t d|d¡}| ¡S)zˆReturn request-host, as defined by RFC 2965. Variation from RFC: returned value is lowercased, for convenient comparison. rRÚÚHost)Úfull_urlrÚ get_headerÚ _cut_port_reÚsubÚlower)ÚrequestrHÚhostrLrLrMÚ request_host3s   rvc@sÊeZdZdidddfdd„Zedd„ƒZejdd„ƒZejdd„ƒZed d „ƒZejd d „ƒZejd d „ƒZd d„Z dd„Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zd#dd„Zdd „Zd!d"„ZdS)$rNFc Csp||_i|_i|_d|_||_d|_x | ¡D]\}}| ||¡q.W|dkrVt|ƒ}||_ ||_ |rl||_ dS)N) rordÚunredirected_hdrsÚ_datarIÚ _tunnel_hostÚitemsÚ add_headerrvÚorigin_req_hostÚ unverifiableÚmethod) ÚselfrHrIrdr|r}r~ÚkeyÚvaluerLrLrMÚ__init__EszRequest.__init__cCs|jrd |j|j¡S|jS)Nz{}#{})ÚfragmentÚformatÚ _full_url)rrLrLrMroWszRequest.full_urlcCs(t|ƒ|_t|jƒ\|_|_| ¡dS)N)rr…rrƒÚ_parse)rrHrLrLrMro]s cCsd|_d|_d|_dS)Nrm)r…rƒÚselector)rrLrLrMrodscCs|jS)N)rx)rrLrLrMrIjsz Request.datacCs(||jkr$||_| d¡r$| d¡dS)NzContent-length)rxÚ has_headerÚ remove_header)rrIrLrLrMrIns  cCs d|_dS)N)rI)rrLrLrMrIxscCsNt|jƒ\|_}|jdkr(td|jƒ‚t|ƒ\|_|_|jrJt|jƒ|_dS)Nzunknown url type: %r) r r…ÚtyperBror rur‡r )rÚrestrLrLrMr†|s  zRequest._parsecCs|jdk rdnd}t|d|ƒS)z3Return a string indicating the HTTP request method.NÚPOSTÚGETr~)rIÚgetattr)rZdefault_methodrLrLrMÚ get_method„szRequest.get_methodcCs|jS)N)ro)rrLrLrMÚ get_full_url‰szRequest.get_full_urlcCs2|jdkr|js|j|_n||_|j|_||_dS)NÚhttps)rŠryruror‡)rrurŠrLrLrMÚ set_proxyŒs  zRequest.set_proxycCs |j|jkS)N)r‡ro)rrLrLrMÚ has_proxy”szRequest.has_proxycCs||j| ¡<dS)N)rdÚ capitalize)rr€ÚvalrLrLrMr{—szRequest.add_headercCs||j| ¡<dS)N)rwr”)rr€r•rLrLrMÚadd_unredirected_header›szRequest.add_unredirected_headercCs||jkp||jkS)N)rdrw)rÚ header_namerLrLrMrˆŸs zRequest.has_headercCs|j ||j ||¡¡S)N)rdÚgetrw)rr—ÚdefaultrLrLrMrp£szRequest.get_headercCs |j |d¡|j |d¡dS)N)rdÚpoprw)rr—rLrLrMr‰¨szRequest.remove_headercCs"|j ¡}| |j¡t| ¡ƒS)N)rwÚcopyÚupdaterdÚlistrz)rÚhdrsrLrLrMÚ header_items¬s  zRequest.header_items)N)Ú__name__Ú __module__Ú __qualname__r‚ÚpropertyroÚsetterÚdeleterrIr†rrr’r“r{r–rˆrpr‰rŸrLrLrLrMrCs(     c@sNeZdZdd„Zdd„Zdd„Zdd„Zd ejfd d „Z dd d „Z dd„Z d S)rcCs6dt}d|fg|_g|_i|_i|_i|_i|_dS)NzPython-urllib/%sz User-agent)Ú __version__Ú addheadersÚhandlersÚ handle_openÚ handle_errorÚprocess_responseÚprocess_request)rZclient_versionrLrLrMr‚²s zOpenerDirector.__init__c CsZt|dƒstdt|ƒƒ‚d}xt|ƒD]}|dkr:q*| d¡}|d|…}||dd…}| d¡rÈ| d¡|d}||dd…}y t|ƒ}Wntk r¬YnX|j  |i¡} | |j|<n>|dkrÜ|}|j } n*|d krð|}|j } n|d kr*|}|j } nq*|   |g¡} | r&t | |¡n |  |¡d }q*W|rVt |j|¡| |¡dS) NÚ add_parentz%expected BaseHandler instance, got %rF)Úredirect_requestÚdo_openÚ proxy_openÚ_rRÚerrorrGÚresponsertT)ÚhasattrÚ TypeErrorrŠÚdirÚfindÚ startswithr]rBrªr˜r©r«r¬Ú setdefaultÚbisectZinsortr\r¨r­) rÚhandlerZaddedÚmethÚiÚprotocolZ conditionÚjÚkindÚlookupr¨rLrLrMÚ add_handler½sJ         zOpenerDirector.add_handlercCsdS)NrL)rrLrLrMÚcloseìszOpenerDirector.closec Gs<| |d¡}x*|D]"}t||ƒ}||Ž}|dk r|SqWdS)NrL)r˜rŽ) rÚchainrÀÚ meth_nameÚargsr¨r»ÚfuncrfrLrLrMÚ _call_chainðs    zOpenerDirector._call_chainNc Csªt|tƒrt||ƒ}n|}|dk r(||_||_|j}|d}x(|j |g¡D]}t||ƒ}||ƒ}qLW|  ||¡} |d}x*|j  |g¡D]}t||ƒ}||| ƒ} qŠW| S)NZ_requestZ _response) Ú isinstanceÚstrrrIrJrŠr¬r˜rŽÚ_openr«) rÚfullurlrIrJÚreqr¾rÅZ processorr¼r³rLrLrMrGûs"      zOpenerDirector.opencCsP| |jdd|¡}|r|S|j}| |j||d|¡}|r>|S| |jdd|¡S)Nr™Z default_openrËÚunknownÚ unknown_open)rÈr©rŠ)rrÍrIrfr¾rLrLrMrËs    zOpenerDirector._opencGs~|dkr,|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|Ž}|r^|S|rz|dd f|}|j|ŽSdS) N)Úhttpr‘rÐr:z http_error_%srRZ_errorrr™Úhttp_error_default)rªrÈ)rÚprotorÆÚdictrÅZhttp_errZ orig_argsrfrLrLrMr²&s   zOpenerDirector.error)N) r r¡r¢r‚rÂrÃrÈÚsocketÚ_GLOBAL_DEFAULT_TIMEOUTrGrËr²rLrLrLrMr±s /  c Gsætƒ}ttttttttt g }t t j dƒr2|  t¡tƒ}xN|D]F}x@|D]8}t|tƒrlt||ƒr€| |¡qHt||ƒrH| |¡qHWq>Wx|D]}| |¡qŽWx|D]}| |ƒ¡q¦Wx&|D]}t|tƒrÔ|ƒ}| |¡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-r´rÐÚclientr\rEÚsetrÉrŠÚ issubclassÚaddÚremoverÂ)r¨rKZdefault_classesÚskipÚklassZcheckÚhrLrLrMr2?s0             c@s(eZdZdZdd„Zdd„Zdd„ZdS) riôcCs ||_dS)N)Úparent)rrßrLrLrMr­fszBaseHandler.add_parentcCsdS)NrL)rrLrLrMrÃiszBaseHandler.closecCst|dƒsdS|j|jkS)NÚ handler_orderT)r´rà)rÚotherrLrLrMÚ__lt__ms zBaseHandler.__lt__N)r r¡r¢ràr­rÃrârLrLrLrMrcsc@s eZdZdZdZdd„ZeZdS)r/zProcess HTTP error responses.iècCsH|j|j| ¡}}}d|kr,dksDn|j d|||||¡}|S)NéÈi,rÐ)ÚcodeÚmsgrUrßr²)rrtr³räråržrLrLrMÚ http_responsezs z HTTPErrorProcessor.http_responseN)r r¡r¢Ú__doc__ràræÚhttps_responserLrLrLrMr/vs c@seZdZdd„ZdS)rcCst|j||||ƒ‚dS)N)rro)rrÍrcräråržrLrLrMrшsz*HTTPDefaultErrorHandler.http_error_defaultN)r r¡r¢rÑrLrLrLrMr‡sc@s4eZdZdZdZdd„Zdd„ZeZZZ dZ dS) réé c st| ¡}|dkr|dks:|dkr(|dks:t|j||||ƒ‚| dd¡}d‰‡fdd „|j ¡Dƒ}t|||jd d S) a­Return 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. )i-i.i/i3)rZHEAD)i-i.i/rŒú z%20)zcontent-lengthz content-typecs"i|]\}}| ¡ˆkr||“qSrL)rs)Ú.0ÚkÚv)ÚCONTENT_HEADERSrLrMú ®sz8HTTPRedirectHandler.redirect_request..T)rdr|r})rrroÚreplacerdrzrr|) rrÍrcrärårdÚnewurlÚmZ newheadersrL)rïrMr®“s  z$HTTPRedirectHandler.redirect_requestc CsLd|kr|d}nd|kr$|d}ndSt|ƒ}|jdkrRt||d||f||ƒ‚|jsn|jrnt|ƒ}d|d<t|ƒ}t|dtj d}t |j |ƒ}|  ||||||¡}|dkr²dSt |d ƒr|j} |_|  |d ¡|jksìt| ƒ|jkrt|j ||j|||ƒ‚ni} |_|_|  |d ¡d | |<| ¡| ¡|jj||jd S) NÚlocationÚuri)rÐr‘Úftprmz+%s - Redirection to url '%s' is not allowedú/r:z iso-8859-1)ÚencodingÚsafeÚ redirect_dictrrR)rJ)rÚschemerrWZnetlocrrr ÚstringZ punctuationrror®r´rúr˜Ú max_repeatsr_Úmax_redirectionsÚinf_msgr^rÃrßrGrJ) rrÍrcrärårdròÚurlpartsÚnewZvisitedrLrLrMÚhttp_error_302¹s@        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) r r¡r¢rýrþr®rÚhttp_error_301Úhttp_error_303Úhttp_error_307rÿrLrLrLrMr‹s &< c Cst|ƒ\}}| d¡s d}|}n:| d¡s6td|ƒ‚| dd¡}|dkrNd}|d|…}t|ƒ\}}|dk r|t|ƒ\}}nd}}||||fS)a Return (scheme, user, password, host/port) given a URL or an authority. If a URL is supplied, it must have an authority (host:port) component. According to RFC 3986, having an authority component means the URL must have two slashes after the scheme. r÷Nz//zproxy URL with no authority: %rr:rQ)r r¸rBr·rr) ÚproxyrûZr_schemeÚ authorityÚendZuserinfoÚhostportÚuserÚpasswordrLrLrMÚ _parse_proxyüs       r c@s"eZdZdZddd„Zdd„ZdS)rédNcCs^|dkrtƒ}t|dƒs tdƒ‚||_x2| ¡D]&\}}t|d||||jfdd„ƒq0WdS)NÚkeyszproxies must be a mappingz%s_opencSs ||||ƒS)NrL)ÚrrrŠr¼rLrLrMÚ$sz'ProxyHandler.__init__..)r5r´ÚAssertionErrorÚproxiesrzÚsetattrr°)rrrŠrHrLrLrMr‚s zProxyHandler.__init__c Cs´|j}t|ƒ\}}}}|dkr"|}|jr6t|jƒr6dS|rv|rvdt|ƒt|ƒf} t |  ¡¡ d¡} |  dd| ¡t|ƒ}|  ||¡||ksš|dkrždS|j j ||j dSdS)Nz%s:%sÚasciizProxy-authorizationzBasic r‘)rJ)rŠr ruÚ proxy_bypassr Úbase64Ú b64encodeÚencodeÚdecoder{r’rßrGrJ) rrÍrrŠZ orig_typeZ proxy_typer r r Z user_passZcredsrLrLrMr°'s   zProxyHandler.proxy_open)N)r r¡r¢ràr‚r°rLrLrLrMrs c@s6eZdZdd„Zdd„Zdd„Zd dd „Zd d „Zd S)r cCs i|_dS)N)Úpasswd)rrLrLrMr‚EszHTTPPasswordMgr.__init__cs`t|tƒr|g}|ˆjkr$iˆj|<x6dD].‰t‡‡fdd„|Dƒƒ}||fˆj||<q*WdS)N)TFc3s|]}ˆ |ˆ¡VqdS)N)Ú reduce_uri)rìÚu)Ú default_portrrLrMú Psz/HTTPPasswordMgr.add_password..)rÉrÊrÚtuple)rÚrealmrõr rÚ reduced_urirL)rrrMÚ add_passwordHs    zHTTPPasswordMgr.add_passwordc Cs`|j |i¡}xLdD]D}| ||¡}x2| ¡D]&\}}x|D]}| ||¡r<|SqdD]6}| ||¡}x$|jD]}| ||¡r|j|SqWqWdS)N)TF)rr/r#)rr$rr%rõrLrLrMr4¤s     z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)r r¡r¢r‚r"r3r4Ú __classcell__rLrL)r2rMr"s  c@sLeZdZe dej¡Zd dd„Zdd„Zdd„Z d d „Z d d „Z e Z e Z dS)r#z0(?:^|,)[ ]*([^ ]+)[ ]+realm=(["']?)([^"']*)\2NcCs"|dkrtƒ}||_|jj|_dS)N)r rr")rZ password_mgrrLrLrMr‚Ász!AbstractBasicAuthHandler.__init__c Csˆ| |d¡}|r„| ¡d}| ¡dkr6td|ƒ‚nNtj |¡}|r„| ¡\}}}|dkrjt  dt d¡| ¡dkr„|  |||¡SdS)NrÚbasiczDAbstractBasicAuthHandler does not support the following scheme: '%s')ú"ú'zBasic Auth Realm was unquotedr:) r˜ÚsplitrsrBr#ÚrxÚsearchÚgroupsr?r@Ú UserWarningÚretry_http_basic_auth) rÚauthreqrurÍrdrûZmor r rLrLrMÚhttp_error_auth_reqedÇs      z.AbstractBasicAuthHandler.http_error_auth_reqedcCs||j ||¡\}}|dk rtd||f}dt | ¡¡ d¡}| |jd¡|krTdS| |j|¡|j j ||j dSdSdS)Nz%s:%szBasic r)rJ) rr&rrrrrpÚ auth_headerr–rßrGrJ)rrurÍr r ÚpwÚrawÚauthrLrLrMr>Ýs z.AbstractBasicAuthHandler.retry_http_basic_authcCstt|jdƒr|j |j¡s|S| d¡sp|j d|j¡\}}d ||¡ ¡}t  |¡  ¡}|  dd |  ¡¡¡|S)Nr4Ú Authorizationz{0}:{1}zBasic {}) r´rr4rorˆr&r„rrZstandard_b64encoderr–Ústrip)rrÍr rZ credentialsZauth_strrLrLrMÚ http_requestés  z%AbstractBasicAuthHandler.http_requestcCsLt|jdƒrHd|jkr"dkr8nn|j |jd¡n|j |jd¡|S)Nr4rãi,TF)r´rrär3ro)rrÍr³rLrLrMræös  z&AbstractBasicAuthHandler.http_response)N)r r¡r¢ÚreÚcompileÚIr:r‚r@r>rGræÚ https_requestrèrLrLrLrMr#¬s   c@seZdZdZdd„ZdS)r$rEcCs|j}| d|||¡}|S)Nzwww-authenticate)ror@)rrÍrcrärårdrHr³rLrLrMÚhttp_error_401s z#HTTPBasicAuthHandler.http_error_401N)r r¡r¢rArLrLrLrLrMr$sc@seZdZdZdd„ZdS)r%zProxy-authorizationcCs|j}| d|||¡}|S)Nzproxy-authenticate)rur@)rrÍrcrärårdrr³rLrLrMÚhttp_error_407s z$ProxyBasicAuthHandler.http_error_407N)r r¡r¢rArMrLrLrLrMr%sc@sNeZdZddd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dS)r&NcCs4|dkrtƒ}||_|jj|_d|_d|_d|_dS)Nr)r rr"ÚretriedÚ nonce_countÚ last_nonce)rrrLrLrMr‚,s z"AbstractDigestAuthHandler.__init__cCs d|_dS)Nr)rN)rrLrLrMÚreset_retry_count5sz+AbstractDigestAuthHandler.reset_retry_countcCs|| |d¡}|jdkr*t|jdd|dƒ‚n|jd7_|rx| ¡d}| ¡dkr`| ||¡S| ¡dkrxtd|ƒ‚dS) Néi‘zdigest auth failedrRrZdigestr6zEAbstractDigestAuthHandler does not support the following scheme: '%s')r˜rNrror9rsÚretry_http_digest_authrB)rrArurÍrdr?rûrLrLrMr@8s        z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz| dd¡\}}ttdt|ƒƒƒ}| ||¡}|rvd|}|j |jd¡|krRdS| |j|¡|j j ||j d}|SdS)NrërRz Digest %s)rJ) r9Úparse_keqv_listÚfilterÚparse_http_listÚget_authorizationrdr˜rAr–rßrGrJ)rrÍrDÚtokenZ challengeÚchalZauth_valZresprLrLrMrSLs z0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|t ¡f}| d¡tdƒ}t |¡ ¡}|dd…S)Nz %s:%s:%s:réé)rOÚtimeZctimerÚ _randombytesÚhashlibÚsha1Ú hexdigest)rÚnonceÚsÚbÚdigrLrLrMÚ get_cnonceXsz$AbstractDigestAuthHandler.get_cnoncecCs¾y6|d}|d}| d¡}| dd¡}| dd¡}Wntk rJdSX| |¡\}} |dkrfdS|j ||j¡\} } | dkr†dS|jdk r | |j|¡} nd} d| || f} d| ¡|j f}|d kr.||j krè|j d 7_ n d |_ ||_ d |j }|  |¡}d ||||||ƒf}| || ƒ|ƒ}n2|dkrT| || ƒd|||ƒfƒ}n t d |ƒ‚d| |||j |f}|r†|d|7}| r˜|d| 7}|d|7}|rº|d||f7}|S)Nr raÚqopÚ algorithmÚMD5Úopaquez%s:%s:%sz%s:%srDrRz%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")r˜ÚKeyErrorÚget_algorithm_implsrr&rorIÚget_entity_digestrr‡rPrOrer)rrÍrYr rarfrgriÚHÚKDr rBZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr,rLrLrMrWcsV              z+AbstractDigestAuthHandler.get_authorizationcsD|dkrdd„‰n|dkr$dd„‰n td|ƒ‚‡fdd„}ˆ|fS)NrhcSst | d¡¡ ¡S)Nr)r^Zmd5rr`)ÚxrLrLrMr¢óz?AbstractDigestAuthHandler.get_algorithm_impls..ZSHAcSst | d¡¡ ¡S)Nr)r^r_rr`)rorLrLrMr¤rpz.Unsupported digest authentication algorithm %rcsˆd||fƒS)Nz%s:%srL)rbÚd)rmrLrMr©rp)rB)rrgrnrL)rmrMrkŸs   z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrL)rrIrYrLrLrMrl¬sz+AbstractDigestAuthHandler.get_entity_digest)N) r r¡r¢r‚rQr@rSrerWrkrlrLrLrLrMr&!s   < c@s eZdZdZdZdZdd„ZdS)r'z¨An authentication protocol defined by RFC 2069 Digest authentication improves on basic authentication because it does not transmit passwords in the clear. rEiêcCs*t|jƒd}| d|||¡}| ¡|S)NrRzwww-authenticate)rror@rQ)rrÍrcrärårdruÚretryrLrLrMrL»s  z$HTTPDigestAuthHandler.http_error_401N)r r¡r¢rçrAràrLrLrLrLrMr'±sc@seZdZdZdZdd„ZdS)r(zProxy-AuthorizationiêcCs"|j}| d|||¡}| ¡|S)Nzproxy-authenticate)rur@rQ)rrÍrcrärårdrurrrLrLrMrMÈs  z%ProxyDigestAuthHandler.http_error_407N)r r¡r¢rAràrMrLrLrLrMr(Ãsc@s6eZdZd dd„Zdd„Zdd„Zdd „Zd d „Zd S)ÚAbstractHTTPHandlerrcCs ||_dS)N)Ú _debuglevel)rÚ debuglevelrLrLrMr‚ÑszAbstractHTTPHandler.__init__cCs ||_dS)N)rt)rÚlevelrLrLrMÚset_http_debuglevelÔsz'AbstractHTTPHandler.set_http_debuglevelcCstjj |j| ¡¡S)N)rÐr×ÚHTTPConnectionÚ_get_content_lengthrIr)rrtrLrLrMry×sz'AbstractHTTPHandler._get_content_lengthc Cs|j}|stdƒ‚|jdk r’|j}t|tƒr8d}t|ƒ‚| d¡sN| dd¡| d¡s’| d¡s’| |¡}|dk r†| dt|ƒ¡n | dd¡|}|  ¡r¸t |j ƒ\}}t |ƒ\}} | d¡sÎ| d|¡x2|j jD]&\} } |  ¡} | | ¡sØ| | | ¡qØW|S) Nz no host givenz\POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.z Content-typez!application/x-www-form-urlencodedzContent-lengthzTransfer-encodingZchunkedrn)rurrIrÉrÊrµrˆr–ryr“r r‡r rßr§r”) rrtrurIråZcontent_lengthZsel_hostrûZselZsel_pathrZrrLrLrMÚ do_request_Üs>          zAbstractHTTPHandler.do_request_c sT|j}|stdƒ‚||fd|ji|—Ž}| |j¡t|jƒ‰ˆ ‡fdd„|j  ¡Dƒ¡dˆd<dd„ˆ  ¡Dƒ‰|j r®i}d}|ˆkržˆ|||<ˆ|=|j |j |d y`y&|j |  ¡|j|jˆ| d ¡d Wn,tk r}z t|ƒ‚Wd d }~XYnX| ¡} Wn| ¡‚YnX|jr>|j ¡d |_| ¡| _| j| _| S) z•Return an HTTPResponse object for the request, using http_class. http_class must implement the HTTPConnection API from http.client. z no host givenrJcsi|]\}}|ˆkr||“qSrLrL)rìrírî)rdrLrMrðsz/AbstractHTTPHandler.do_open..rÃÚ ConnectioncSsi|]\}}|| ¡“qSrL)Útitle)rìrZr•rLrLrMrðszProxy-Authorization)rdzTransfer-encoding)Zencode_chunkedN)rurrJZset_debuglevelrtrÓrwrœrdrzryZ set_tunnelrtrr‡rIrˆrlÚ getresponserÃZsockrrHÚreasonrå) rZ http_classrÍZhttp_conn_argsrurÞZtunnel_headersZproxy_auth_hdrÚerrrrL)rdrMr¯s@       zAbstractHTTPHandler.do_openN)r)r r¡r¢r‚rwryrzr¯rLrLrLrMrsÏs  &rsc@seZdZdd„ZejZdS)r)cCs| tjj|¡S)N)r¯rÐr×rx)rrÍrLrLrMÚ http_openGszHTTPHandler.http_openN)r r¡r¢r€rsrzrGrLrLrLrMr)EsrÖc@s$eZdZddd„Zdd„ZejZdS)rErNcCst ||¡||_||_dS)N)rsr‚Ú_contextÚ_check_hostname)rrur>Úcheck_hostnamerLrLrMr‚Ps zHTTPSHandler.__init__cCs|jtjj||j|jdS)N)r>rƒ)r¯rÐr×rÖrr‚)rrÍrLrLrMÚ https_openUs zHTTPSHandler.https_open)rNN)r r¡r¢r‚r„rsrzrKrLrLrLrMrENs rEc@s.eZdZddd„Zdd„Zdd„ZeZeZdS) rNcCs$ddl}|dkr|j ¡}||_dS)Nr)Zhttp.cookiejarÚ cookiejarZ CookieJar)rr…rÐrLrLrMr‚^s zHTTPCookieProcessor.__init__cCs|j |¡|S)N)r…Zadd_cookie_header)rrtrLrLrMrGds z HTTPCookieProcessor.http_requestcCs|j ||¡|S)N)r…Zextract_cookies)rrtr³rLrLrMræhsz!HTTPCookieProcessor.http_response)N)r r¡r¢r‚rGrærKrèrLrLrLrMr]s  c@seZdZdd„ZdS)r.cCs|j}td|ƒ‚dS)Nzunknown url type: %s)rŠr)rrÍrŠrLrLrMrÏpszUnknownHandler.unknown_openN)r r¡r¢rÏrLrLrLrMr.oscCsRi}xH|D]@}| dd¡\}}|ddkrB|ddkrB|dd…}|||<q W|S)z>Parse list of key=value strings where keys are not duplicated.ú=rRrr7rQ)r9)ÚlZparsedZeltrírîrLrLrMrTts   rTcCs¢g}d}d}}xt|D]l}|r,||7}d}q|rV|dkr@d}qn |dkrLd}||7}q|dkrn| |¡d}q|dkrzd}||7}qW|r”| |¡dd„|DƒS) 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. rmFú\Tr7ú,cSsg|] }| ¡‘qSrL)rF)rìÚpartrLrLrMú §sz#parse_http_list..)r\)rbZresrŠÚescaper ZcurrLrLrMrV~s4     rVc@s(eZdZdd„ZdZdd„Zdd„ZdS)r*cCs\|j}|dd…dkrN|dd…dkrN|jrN|jdkrN|j| ¡krXtdƒ‚n | |¡SdS)Nr:z//ér÷Ú localhostz-file:// scheme is supported only on localhost)r‡ruÚ get_namesrÚopen_local_file)rrÍrHrLrLrMÚ file_open«s &  zFileHandler.file_openNcCs`tjdkrZy*tt d¡dt t ¡¡dƒt_Wn$tjk rXt d¡ft_YnXtjS)NrŽr:)r*ÚnamesrrÔÚgethostbyname_exÚ gethostnameÚgaierrorÚ gethostbyname)rrLrLrMr¶s  zFileHandler.get_namesc Csüddl}ddl}|j}|j}t|ƒ}y t |¡}|j}|jj |j dd} |  |¡d} |  d| pbd|| f¡} |r~t |ƒ\}} |r–| sÂt|ƒ| ¡krÂ|r¨d||} nd|} tt|dƒ| | ƒSWn*tk rî}z t|ƒ‚Wdd}~XYnXtdƒ‚dS) NrT)Úusegmtz6Content-type: %s Content-length: %d Last-modified: %s z text/plainzfile://Úrbzfile not on local host)Ú email.utilsÚ mimetypesrur‡r4rVÚstatÚst_sizeÚutilsÚ formatdateÚst_mtimeÚ guess_typeÚmessage_from_stringr Ú_safe_gethostbynamerrrGrlr)rrÍÚemailršruraZ localfileÚstatsrhÚmodifiedÚmtyperdr)ZorigurlÚexprLrLrMrÁs0  zFileHandler.open_local_file)r r¡r¢r‘r’rrrLrLrLrMr*©s  cCs&y t |¡Stjk r dSXdS)N)rÔr–r•)rurLrLrMr¢Üs r¢c@seZdZdd„Zdd„ZdS)r+c Cs,ddl}ddl}|j}|s"tdƒ‚t|ƒ\}}|dkr>|j}nt|ƒ}t|ƒ\}}|rdt|ƒ\}}nd}t |ƒ}|pvd}|p~d}yt   |¡}Wn*t k r¸}z t|ƒ‚Wdd}~XYnXt |jƒ\} } |  d¡} ttt | ƒƒ} | dd…| d} } | r| ds| dd…} yÐ| ||||| |j¡} | r6dp8d}x:| D]2}t|ƒ\}}| ¡d kr@|d kr@| ¡}q@W|  | |¡\}}d}| |j¡d}|r¬|d |7}|dk rÌ|dkrÌ|d |7}t |¡}t|||jƒS|jk r&}z"td |ƒ}| t  ¡d¡‚Wdd}~XYnXdS)Nrzftp error: no host givenrmr÷rQrRrJÚDrŠ)ÚaÚAr½rJrqr¨zContent-type: %s zContent-length: %d z ftp error: %rr:)!Úftplibršrurr ÚFTP_PORTr]rrr rÔr–rlrr‡r9rÚmapÚ connect_ftprJrrsÚupperÚretrfiler ror£r¡rÚ all_errorsÚwith_tracebackÚsysÚexc_info)rrÍr«ršrur)r rrårWÚattrsÚdirsrNÚfwrŠÚattrrrcÚretrlenrdr¦r§ÚexcrLrLrMÚftp_openãs\           zFTPHandler.ftp_openc Cst||||||ddS)NF)Ú persistent)Ú ftpwrapper)rr rrur)r¶rJrLrLrMr®szFTPHandler.connect_ftpN)r r¡r¢r»r®rLrLrLrMr+âs5c@s<eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd S)r,cCs"i|_i|_d|_d|_d|_dS)Nré<r[)ÚcacherJÚsoonestÚdelayÚ max_conns)rrLrLrMr‚s zCacheFTPHandler.__init__cCs ||_dS)N)rÁ)rÚtrLrLrMÚ setTimeout&szCacheFTPHandler.setTimeoutcCs ||_dS)N)rÂ)rrórLrLrMÚ setMaxConns)szCacheFTPHandler.setMaxConnscCsr|||d |¡|f}||jkr4t ¡|j|j|<n,t||||||ƒ|j|<t ¡|j|j|<| ¡|j|S)Nr÷)Újoinr¿r\rÁrJr½Ú check_cache)rr rrur)r¶rJr€rLrLrMr®,s  zCacheFTPHandler.connect_ftpcCsÈt ¡}|j|krTx@t|j ¡ƒD].\}}||kr"|j| ¡|j|=|j|=q"Wtt|j ¡ƒƒ|_t |jƒ|j krÄx6t|j ¡ƒD]$\}}||jkrˆ|j|=|j|=PqˆWtt|j ¡ƒƒ|_dS)N) r\rÀrrJrzr¿rÃÚminÚvaluesr_rÂ)rrÃrírîrLrLrMrÇ7s   zCacheFTPHandler.check_cachecCs4x|j ¡D] }| ¡q W|j ¡|j ¡dS)N)r¿rÉrÃÚclearrJ)rÚconnrLrLrMÚ clear_cacheKs  zCacheFTPHandler.clear_cacheN) r r¡r¢r‚rÄrÅr®rÇrÌrLrLrLrMr,s  c@seZdZdd„ZdS)r-cCs~|j}| dd¡\}}| dd¡\}}t|ƒ}| d¡rNt |¡}|dd…}|sVd}t d|t|ƒf¡}t t   |¡||ƒS)Nú:rRr‰z;base64iùÿÿÿztext/plain;charset=US-ASCIIz$Content-type: %s Content-length: %d ) ror9rÚendswithrÚ decodebytesr£r¡r_rÚioÚBytesIO)rrÍrHrûrIZ mediatyperdrLrLrMÚ data_openRs    zDataHandler.data_openN)r r¡r¢rÒrLrLrLrMr-QsrêÚnt)r4r3cCst|ƒS)zOS-specific conversion from a relative URL of the 'file' scheme to a file system path; not recommended for general use.)r )ÚpathnamerLrLrMr4xscCst|ƒS)zOS-specific conversion from a file system path to a relative URL of the 'file' scheme; not recommended for general use.)r )rÔrLrLrMr3}sc@sÊeZdZdZdZdeZd*dd„Zdd„Zdd „Z d d „Z d d „Z d+dd„Z d,dd„Z d-dd„Zd.dd„Zdd„Zd/dd„Zd0dd„Zdd„Zer¤dd„Zd1d d!„Zd"d#„Zd$d%„Zd&d'„Zd2d(d)„ZdS)3r8a,Class to open URLs. This is a class rather than just a subroutine because we may need more than one set of global protocol-specific options. Note -- this is a base class for those who don't want the automatic handling of errors type 302 (relocated) and 401 (authorization needed).NzPython-urllib/%scKsŒdd|jji}tj|tdd|dkr.tƒ}t|dƒs@tdƒ‚||_|  d¡|_ |  d¡|_ d |j fd g|_ g|_tj|_d|_t|_dS) NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methodsÚclassr)Ú stacklevelrzproxies must be a mappingÚkey_fileÚ cert_filez User-Agent)ZAcceptz*/*)r2r r?r@rAr5r´rrr˜r×rØÚversionr§Ú_URLopener__tempfilesrVrkÚ_URLopener__unlinkÚ tempcacheÚftpcache)rrZx509rårLrLrMr‚“s  zURLopener.__init__cCs | ¡dS)N)rÃ)rrLrLrMÚ__del__­szURLopener.__del__cCs | ¡dS)N)Úcleanup)rrLrLrMrðszURLopener.closec CsZ|jrFx2|jD](}y| |¡Wqtk r4YqXqW|jdd…=|jrV|j ¡dS)N)rÚrÛrlrÜrÊ)rrNrLrLrMrß³s   zURLopener.cleanupcGs|j |¡dS)zdAdd a header to be used by the HTTP interface only e.g. u.addheader('Accept', 'sound/basic')N)r§r\)rrÆrLrLrMÚ addheaderÁszURLopener.addheaderc Csltt|ƒƒ}t|dd}|jrL||jkrL|j|\}}t|dƒ}t|||ƒSt|ƒ\}}|s`d}||jkr–|j|}t|ƒ\}} t| ƒ\} } | |f}nd}d|} ||_ |   dd¡} t || ƒrÆ| d krä|rØ|  |||¡S|  ||¡Sy,|dkrþt|| ƒ|ƒSt|| ƒ||ƒSWnVttfk r,‚Yn<tk rf} ztd | ƒ t ¡d ¡‚Wdd} ~ XYnXdS) z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|)rùr˜rNNZopen_ú-r±rz socket errorr:)rrr rÜrGrr rr rŠrñr´Úopen_unknown_proxyÚ open_unknownrŽrrrlr²r³r´)rrÌrIrardrcÚurltyperHrÚ proxyhostrur‡rZrårLrLrMrGÇs<             zURLopener.opencCst|ƒ\}}tdd|ƒ‚dS)z/Overridable interface to open unknown URL type.z url errorzunknown url typeN)r rl)rrÌrIrŠrHrLrLrMrãës zURLopener.open_unknowncCs t|ƒ\}}tdd||ƒ‚dS)z/Overridable interface to open unknown URL type.z url errorzinvalid proxy for %sN)r rl)rrrÌrIrŠrHrLrLrMrâðs zURLopener.open_unknown_proxyc Cstt|ƒƒ}|jr&||jkr&|j|St|ƒ\}}|dkr˜|rF|dkr˜y.| |¡}| ¡}| ¡tt|ƒdƒ|fSt k r–} zWdd} ~ XYnX|  ||¡}z>| ¡} |rÀt |dƒ} nrt|ƒ\} } t| pÔdƒ\} } t | pädƒ\} } t | pôdƒ\} } t j | ¡d}t |¡\}}|j |¡t  |d¡} z¤|| f}|jdk rR||j|<d}d}d}d}d | krxt| d ƒ}|rŠ||||ƒxH| |¡}|sžP|t|ƒ7}|  |¡|d7}|rŒ||||ƒqŒWWd|  ¡XWd| ¡X|dkr||krtd ||f|ƒ‚|S) ztretrieve(url) returns (filename, headers) for a local object or (tempfilename, headers) for a remote object.NrNrRrOrmi rQrzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytes)rrrÜr rrUrÃr4r rlrGrrrVrWÚsplitextrYZmkstemprÚr\Úfdopenr]r^r_r`r)rrHrarbrIrŠZurl1rcržrårdreZgarbagerWÚsuffixÚfdrfrgrhr^rirjrLrLrMÚretrieveösj                  zURLopener.retrievecCs(d}d}t|tƒrDefault error handler: close the connection and raise OSError.N)rÃr)rrHrcrïrðrdrLrLrMrѧszURLopener.http_error_defaultcCstjj||j|jdS)N)r×rØ)rÐr×rÖr×rØ)rrurLrLrMÚ_https_connection­szURLopener._https_connectioncCs| |j||¡S)zUse HTTPS protocol.)rírñ)rrHrIrLrLrMÚ open_https²szURLopener.open_httpscCs^t|tƒstdƒ‚|dd…dkrP|dd…dkrP|dd… ¡dkrPtd ƒ‚n | |¡SdS) z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr:z//rr÷é z localhost/z-file:// scheme is supported only on localhost)rÉrÊrrsrBr)rrHrLrLrMÚ open_file¶s  4 zURLopener.open_filec Cs\ddl}ddl}t|ƒ\}}t|ƒ}yt |¡}Wn0tk rb}zt|j|j ƒ‚Wdd}~XYnX|j } |j j |j dd} | |¡d} | d| p–d| | f¡} |sÔ|} |dd…dkrÂd |} tt|d ƒ| | ƒSt|ƒ\}}|sPt |¡tƒftƒkrP|} |dd…dkr d |} n|dd …d kr>td |ƒ‚tt|d ƒ| | ƒStdƒ‚dS)zUse local file.rNT)r—z6Content-Type: %s Content-Length: %d Last-modified: %s z text/plainrRr÷zfile://r˜r:z./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)r™ršr r4rVr›rlrÚstrerrorrarœrržrŸr r¡rrGr rÔr–rŽÚthishostrB)rrHr£ršrurNZ localnamer¤Úerhr¥r¦rdZurlfiler)rLrLrMr¿s:     zURLopener.open_local_filec Cs’t|tƒstdƒ‚ddl}t|ƒ\}}|s2tdƒ‚t|ƒ\}}t|ƒ\}}|r\t|ƒ\}}nd}t|ƒ}t|ppdƒ}t|p|dƒ}t   |¡}|sžddl }|j }nt |ƒ}t|ƒ\}} t|ƒ}| d¡} | dd…| d} } | rò| dsò| dd…} | r | ds d| d<|||d | ¡f} t|jƒtkrfx8t|jƒD]*} | | kr8|j| }|j| =| ¡q8Wyæ| |jkrŠt||||| ƒ|j| <| s–d }nd }x:| D]2}t|ƒ\}}| ¡d kr |d kr | ¡}q W|j|  | |¡\}}| d |¡d}d}|r|d|7}|dk r4|dkr4|d|7}t |¡}t||d |ƒStƒk rŒ}ztd|ƒ  t! "¡d¡‚Wdd}~XYnXdS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNzftp error: no host givenrmr÷rQrRr¨rJrŠ)r©rªr½rJrqr¨zftp:zContent-Type: %s zContent-Length: %d z ftp error %rr:)#rÉrÊrršr r rrr rÔr–r«r¬r]rr9rÆr_rÝÚ MAXFTPCACHErrÃr½rrsr¯r°r r£r¡rÚ ftperrorsr²r³r´)rrHršrurWr)r rr«rµr¶rNr€rírîrŠr¸rrcr¹r¦rdr§rLrLrMÚopen_ftpßsp                   zURLopener.open_ftpc Cs<t|tƒstdƒ‚y| dd¡\}}Wntk rDtddƒ‚YnX|sNd}| d¡}|dkrŽd ||d …krŽ||dd …}|d |…}nd }g}| d t  d t  t ¡¡¡¡| d|¡|dkrät   |  d¡¡ d¡}nt|ƒ}| dt|ƒ¡| d ¡| |¡d |¡}t |¡}t |¡}t|||ƒS)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr‰rRz data errorz bad data URLztext/plain;charset=US-ASCIIú;rr†NrmzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %dÚ )rÉrÊrr9rBrlÚrfindr\r\ZstrftimeZgmtimerrÏrrr r_rÆr£r¡rÐÚStringIOr) rrHrIrŠZsemirørårdÚfrLrLrMÚ open_datas6        zURLopener.open_data)N)N)N)N)NNN)N)N)N)N)r r¡r¢rçrÚr¦rÙr‚rÞrÃrßràrGrãrârêrírîrërÑrCrñròrôrrúrrLrLrLrMr8†s.  $   A\     :c@sžeZdZdZdd„Zdd„Zd#dd„Zd d „Zd$d d „Zd%d d„Z d&dd„Z d'dd„Z d(dd„Z d)dd„Z d*dd„Zd+dd„Zd,dd„Zd-dd „Zd!d"„ZdS).r9z?Derived class with handlers for errors we can handle (perhaps).cOs(tj|f|ž|Ži|_d|_d|_dS)Nrrê)r8r‚Ú auth_cacheÚtriesÚmaxtries)rrÆr1rLrLrMr‚FszFancyURLopener.__init__cCst||d||ƒS)z3Default error handling -- don't raise an exception.zhttp:)r)rrHrcrïrðrdrLrLrMrÑLsz!FancyURLopener.http_error_defaultNc Csn|jd7_zR|jrJ|j|jkrJt|dƒr4|j}n|j}|||dd|ƒS| ||||||¡}|Sd|_XdS)z%Error 302 -- relocated (temporarily).rRÚhttp_error_500iôz)Internal Server Error: Redirect RecursionNr)rrr´rrÑÚredirect_internal) rrHrcrïrðrdrIr¼rfrLrLrMrPs  zFancyURLopener.http_error_302c Csxd|kr|d}nd|kr$|d}ndS| ¡t|jd||ƒ}t|ƒ}|jdkrnt|||d|||ƒ‚| |¡S)NrôrõrÍ)rÐr‘rörmz( Redirection to url '%s' is not allowed.)rÃrrŠrrûrrG) rrHrcrïrðrdrIròrrLrLrMrbs   z FancyURLopener.redirect_internalcCs| ||||||¡S)z*Error 301 -- also relocated (permanently).)r)rrHrcrïrðrdrIrLrLrMr~szFancyURLopener.http_error_301cCs| ||||||¡S)z;Error 303 -- also relocated (essentially identical to 302).)r)rrHrcrïrðrdrIrLrLrMr‚szFancyURLopener.http_error_303cCs2|dkr| ||||||¡S| |||||¡SdS)z1Error 307 -- relocated, but turn POST into error.N)rrÑ)rrHrcrïrðrdrIrLrLrMr†szFancyURLopener.http_error_307Fc CsÈd|krt ||||||¡|d}t d|¡} | sHt ||||||¡|  ¡\} } |  ¡dkrtt ||||||¡|sŒt ||||||¡d|jd} |dkr²t|| ƒ|| ƒSt|| ƒ|| |ƒSdS)z_Error 401 -- authentication required. This function supports Basic authentication only.zwww-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"r6Zretry_Ú _basic_authN)r8rÑrHÚmatchr<rsrŠrŽ) rrHrcrïrðrdrIrrÚstuffrrûr rZrLrLrMrLs&         zFancyURLopener.http_error_401c CsÈd|krt ||||||¡|d}t d|¡} | sHt ||||||¡|  ¡\} } |  ¡dkrtt ||||||¡|sŒt ||||||¡d|jd} |dkr²t|| ƒ|| ƒSt|| ƒ|| |ƒSdS)zeError 407 -- proxy authentication required. This function supports Basic authentication only.zproxy-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"r6Z retry_proxy_rN)r8rÑrHrr<rsrŠrŽ) rrHrcrïrðrdrIrrrrrûr rZrLrLrMrM¦s&         zFancyURLopener.http_error_407cCsÄt|ƒ\}}d||}|jd}t|ƒ\}} t| ƒ\} } |  d¡d} | | d…} | | || ¡\} } | sr| srdSdt| ddt| dd| f} d| | |jd<|dkr´| |¡S| ||¡SdS)Nzhttp://rÐú@rRz%s:%s@%srm)rù)r rr r·Úget_user_passwdr rG)rrHr rIrur‡ròrräråÚ proxyselectorr½r rrLrLrMÚretry_proxy_http_basic_auth¿s         z*FancyURLopener.retry_proxy_http_basic_authcCsÄt|ƒ\}}d||}|jd}t|ƒ\}} t| ƒ\} } |  d¡d} | | d…} | | || ¡\} } | sr| srdSdt| ddt| dd| f} d| | |jd<|dkr´| |¡S| ||¡SdS)Nzhttps://r‘r rRz%s:%s@%srm)rù)r rr r·r r rG)rrHr rIrur‡ròrrärår r½r rrLrLrMÚretry_proxy_https_basic_authÑs         z+FancyURLopener.retry_proxy_https_basic_authc Cst|ƒ\}}| d¡d}||d…}| |||¡\}}|sD|sDdSdt|ddt|dd|f}d||} |dkr€| | ¡S| | |¡SdS)Nr rRz%s:%s@%srm)rùzhttp://)r r·r r rG) rrHr rIrur‡r½r rròrLrLrMr>ãs     z$FancyURLopener.retry_http_basic_authc Cst|ƒ\}}| d¡d}||d…}| |||¡\}}|sD|sDdSdt|ddt|dd|f}d||} |dkr€| | ¡S| | |¡SdS)Nr rRz%s:%s@%srm)rùzhttps://)r r·r r rG) rrHr rIrur‡r½r rròrLrLrMÚretry_https_basic_authñs     z%FancyURLopener.retry_https_basic_authrcCs`|d| ¡}||jkr2|r(|j|=n |j|S| ||¡\}}|sJ|rX||f|j|<||fS)Nr )rsrÚprompt_user_passwd)rrur rÌr€r rrLrLrMr ÿs   zFancyURLopener.get_user_passwdcCsTddl}y,td||fƒ}| d|||f¡}||fStk rNtƒdSXdS)z#Override this in a GUI environment!rNzEnter username for %s at %s: z#Enter password for %s in %s at %s: )NN)ÚgetpassÚinputÚKeyboardInterruptÚprint)rrur rr rrLrLrMr sz!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r)r r¡r¢rçr‚rÑrrrrrrLrMr r r>rr rrLrLrLrMr9Cs$           cCstdkrt d¡atS)z8Return the IP address of the magic hostname 'localhost'.NrŽ)Ú _localhostrÔr–rLrLrLrMrŽ s rŽcCsPtdkrLytt t ¡¡dƒaWn(tjk rJtt d¡dƒaYnXtS)z,Return the IP addresses of the current host.Nr:rŽ)Ú _thishostrrÔr“r”r•rLrLrLrMrö" s röcCstdkrddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)Ú _ftperrorsr«r±)r«rLrLrMrù- srùcCstdkrt d¡atS)z%Return an empty email Message object.Nrm)Ú _noheadersr£r¡rLrLrLrMÚ noheaders6 s rc@sJeZdZdZddd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dS)r½z;Class used by open_ftp() for cache of open FTP connections.NTcCsX||_||_||_||_||_||_d|_||_y | ¡Wn|  ¡‚YnXdS)Nr) r rrur)r¶rJÚrefcountÚ keepaliveÚinitrÃ)rr rrur)r¶rJr¼rLrLrMr‚C s zftpwrapper.__init__cCs\ddl}d|_| ¡|_|j |j|j|j¡|j |j |j ¡d  |j ¡}|j  |¡dS)Nrr÷)r«ÚbusyZFTPröZconnectrur)rJZloginr rrÆr¶Úcwd)rr«Z_targetrLrLrMrS s  zftpwrapper.initc Cs¶ddl}| ¡|dkr"d}d}n d|}d}y|j |¡Wn*|jk rh| ¡|j |¡YnXd}|ræ|sæyd|}|j |¡\}}WnR|jk rä}z2t|ƒdd…dkrÔt d |ƒ  t   ¡d ¡‚Wdd}~XYnX|s€|j d¡|rl|j  ¡} zJy|j |¡Wn4|jk rN}zt d |ƒ|‚Wdd}~XYnXWd|j | ¡Xd |}nd }|j |¡\}}d|_t| d ¡|jƒ} |jd7_| ¡| |fS)Nr)rqr¨zTYPE ArRzTYPE zRETR rZ550z ftp error: %rr:zLIST ZLISTr˜)r«Ú endtransferröZvoidcmdr±rZ ntransfercmdZ error_permrÊrr²r³r´ÚpwdrrrZmakefileÚ file_closerrÃ) rrNrŠr«ÚcmdÚisdirrËr¹r~rZftpobjrLrLrMr°\ sN    $ zftpwrapper.retrfilecCs d|_dS)Nr)r)rrLrLrMr‰ szftpwrapper.endtransfercCsd|_|jdkr| ¡dS)NFr)rrÚ real_close)rrLrLrMrÃŒ s zftpwrapper.closecCs2| ¡|jd8_|jdkr.|js.| ¡dS)NrRr)rrrr#)rrLrLrMr ‘ szftpwrapper.file_closecCs2| ¡y|j ¡Wntƒk r,YnXdS)N)rrörÃrù)rrLrLrMr#— s zftpwrapper.real_close)NT) r r¡r¢rçr‚rr°rrÃr r#rLrLrLrMr½@ s  -r½cCs¼i}xBtj ¡D]4\}}| ¡}|r|dd…dkr|||dd…<qWdtjkr^| dd¡xXtj ¡D]J\}}|dd…dkrj| ¡}|r |||dd…<qj| |dd…d¡qjW|S)aReturn a dictionary of scheme -> 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. iúÿÿÿNÚ_proxyZREQUEST_METHODrÐ)rVÚenvironrzrsrš)rrZrrLrLrMÚgetproxies_environmentŸ s   r&cCs²|dkrtƒ}y |d}Wntk r.dSX|dkr.r‰Ú.z (.+\.)?%s$) r&rjr r9ÚlstriprHrŒrrJ)rurZno_proxyÚhostonlyr)Z no_proxy_listrZÚpatternrLrLrMÚproxy_bypass_environment¾ s&     r,c Csddlm}t|ƒ\}}dd„}d|kr4|dr4dSd}xÞ| d d ¡D]Î}|sPqFt d |¡}|dk r|dkrœyt |¡}||ƒ}Wntk ršwFYnX|| d ¡ƒ} | d ¡} | dkrÖd| d ¡  d¡d } nt | d d…ƒ} d| } || ?| | ?krdSqF|||ƒrFdSqFWdS)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)ÚfnmatchcSsh| d¡}ttt|ƒƒ}t|ƒdkr<|ddddgdd…}|dd>|dd>B|dd>B|d BS) Nr(rérérRr[r:rZr)r9rr­r]r_)ZipAddrr(rLrLrMÚip2numñ s   z,_proxy_bypass_macosx_sysconf..ip2numr(Zexclude_simpleTNÚ exceptionsrLz(\d+(?:\.\d+)*)(/\d+)?rRr:rZé F) r-r r˜rHrrÔr–rlÚgroupÚcountr]) ruÚproxy_settingsr-r*r)r/ZhostIPrrór,ÚmaskrLrLrMÚ_proxy_bypass_macosx_sysconfá s:        r6Údarwin)Ú_get_proxy_settingsÚ _get_proxiescCstƒ}t||ƒS)N)r8r6)rur4rLrLrMÚproxy_bypass_macosx_sysconf sr:cCstƒS)z±Return a dictionary of scheme -> proxy server URL mappings. This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. )r9rLrLrLrMÚgetproxies_macosx_sysconf$ sr;cCs tƒ}|rt||ƒSt|ƒSdS)z¸Return True, if host should be bypassed. Checks proxy settings gathered from the environment, if specified, or from the MacOSX framework SystemConfiguration. N)r&r,r:)rurrLrLrMr. s rcCs tƒp tƒS)N)r&r;rLrLrLrMr5; sc Csi}y ddl}Wntk r$|SXyÎ| |jd¡}| |d¡d}|rêt| |d¡dƒ}d|kr¬x‚| d¡D]4}| dd¡\}}t d |¡sžd ||f}|||<qrWn>|dd …d krÆ||d <n$d||d <d||d<d||d<|  ¡Wnt t t fk rYnX|S)zxReturn a dictionary of scheme -> proxy server URL mappings. Win32 uses the registry to store proxies. rNz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsÚ ProxyEnableZ ProxyServerr†rûrRz ^([^/:]+)://z%s://%srRzhttp:rÐz http://%sz https://%sr‘zftp://%srö) ÚwinregÚ ImportErrorÚOpenKeyÚHKEY_CURRENT_USERÚ QueryValueExrÊr9rHrZCloserlrBrµ)rr=ÚinternetSettingsÚ proxyEnableZ proxyServerÚpr¾ZaddressrLrLrMÚgetproxies_registry@ s8          rEcCs tƒp tƒS)z¥Return a dictionary of scheme -> proxy server URL mappings. Returns settings gathered from the environment, if specified, or the registry. )r&rErLrLrLrMr5m sc Csxy ddl}Wntk r dSXy6| |jd¡}| |d¡d}t| |d¡dƒ}Wntk rldSX|rv|szdSt|ƒ\}}|g}y t  |¡}||krª|  |¡Wntk rÀYnXy t  |¡}||krà|  |¡Wntk röYnX|  d¡}xp|D]h} | dkr$d|kr$dS|   dd ¡} |   d d ¡} |   d d¡} x$|D]} t | | tj¡rNdSqNWqWdS) Nrz;Software\Microsoft\Windows\CurrentVersion\Internet Settingsr<Z ProxyOverriderûzr(rRz\.r'z.*ú?)r=r>r?r@rArÊrlr rÔr–r\Zgetfqdnr9rñrHrrJ) rur=rBrCZ proxyOverrideZrawHostr)ZaddrZfqdnr-r•rLrLrMÚproxy_bypass_registryv sR             rGcCs tƒ}|rt||ƒSt|ƒSdS)z—Return True, if host should be bypassed. Checks proxy settings gathered from the environment, if specified, or the registry. N)r&r,rG)rurrLrLrMr¨ s )NNN)N)~rçrrºr£r^Z http.clientrÐrÐrVr*rHrÔrür³r\rYrSr?Z urllib.errorrrrZ urllib.parserrrrr r r r r rrrrrrrrrZurllib.responserrrDr>rCÚ__all__Ú version_infor¦rFrÕr0r1r[r6r7rIÚASCIIrqrvrrr2rr/rrr rr r!r"r#r$r%Úurandomr]r&r'r(rsr)r´r×rEr\rr.rTrVr*r¢r+r,r-rørZZ nturl2pathr4r3rÝr8r9rrŽrrörrùrrr½r&r,r6ÚplatformZ_scproxyr8r9r:r;rr5rErGrLrLrLrMÚDsêP   T ?n$q*@ W  v  +3:5! @W  _ #<    - 2