grthtrhthjhtyjytjytkergtrhtrjytjerhrfh3 ct„j~Šã)@sÎdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZm Z m!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 �r&dZ/YnXdZ/dd d d d d ddddddddddddddddddd d!d"d#d$d%d&d'd(g!Z0d)e j1dd*…Z2da3de j4fddddd+œd,d„Z5d-d „Z6gZ7d~d.d%„Z8d/d&„Z9e j:d0e j;ƒZGd4d „d ƒZ?d5d!„Z@Gd6d „d ƒZAGd7d„deAƒZBGd8d „d eAƒZCGd9d „d eAƒZDd:d;„ZEGdd„deGƒZHGd?d„deHƒZIGd@d„dƒZJGdAd„deJeAƒZKGdBd„deJeAƒZLejMZNGdCd„dƒZOGdDd„deAeOƒZPGdEd„deAeOƒZQGdFdG„dGeAƒZRGdHd„deRƒZSeTejUdIƒ�r2GdJdK„dKeRƒZVe0jWdKƒGdLd „d eAƒZXGdMd„deAƒZYdNdO„ZZdPdQ„Z[GdRd„deAƒZ\dSdT„Z]GdUd„deAƒZ^GdVd„de^ƒZ_GdWd„deAƒZ`dXZaejbdYk�rÌddZlcmdZdmeZend[d#„Zdd\d"„ZeiZfGd]d'„d'ƒZgGd^d(„d(egƒZhdaid_d`„Zjdakdadb„Zldamdcdd„Zndaodedf„ZpGdgdh„dhƒZqdidj„Zrddkdl„Zsdmdn„Zte judok�r”ddplvmwZwmxZxdqdr„Zydsdt„Zzdudv„Z{dwd$„Z|n6ejbdYk�rÂdxdy„Z}dzd$„Z|d{d|„Z~d}dv„Z{nerZ|esZ{dS)€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}|jdtdƒ|dk r2tdƒ‚ts>tdƒ‚tjtjj||d�}t |d�}t |ƒ} n0|r~t |d�}t |ƒ} nt dkr’t ƒa } nt } | j |||ƒ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/lib64/python3.6/urllib/request.pyr0Œs*<       cCs|adS)N)rF)rKrLrLrMr1áscCs4t|ƒ\}}tjt||ƒƒ�è}|jƒ}|dkrD| rDtjj|ƒ|fS|rTt|dƒ}nt j dd�}|j }t j |ƒ|�†||f} d } d} d} d} d |kr¢t|d ƒ} |r²|| | | ƒxB|j| ƒ}|sÄP| t|ƒ7} |j|ƒ| d7} |r´|| | | ƒq´WWd QRXWd QRX| dk�r0| | k�r0td | | 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-LengthNz1retrieval incomplete: got only %i out of %i bytesi éÿÿÿÿ)r Ú contextlibÚclosingr0ÚinfoÚosÚpathÚnormpathrGÚtempfileZNamedTemporaryFileÚnameÚ_url_tempfilesÚappendÚintÚreadÚlenÚwriter)rHÚfilenameÚ reporthookrIZurl_typerXÚfpÚheadersÚtfpÚresultÚbsÚsizer_ÚblocknumÚblockrLrLrMr6æsD         $c CsHx0tD](}ytj|ƒWqtk r,YqXqWtdd…=trDdadS)z0Clean up temporary files from urlretrieve calls.N)r\rWÚunlinkÚOSErrorrF)Z temp_filerLrLrMr7%s   z:\d+$cCs<|j}t|ƒd}|dkr&|jddƒ}tjd|dƒ}|jƒ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_host4s   rwc@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 |jƒD]\}}|j||ƒq.W|dkrVt|ƒ}||_ ||_ |rl||_ dS)N) rpreÚunredirected_hdrsÚ_datarIÚ _tunnel_hostÚitemsÚ add_headerrwÚorigin_req_hostÚ unverifiableÚmethod) ÚselfrHrIrer}r~rÚkeyÚvaluerLrLrMÚ__init__FszRequest.__init__cCs|jrdj|j|jƒS|jS)Nz{}#{})ÚfragmentÚformatÚ _full_url)r€rLrLrMrpXszRequest.full_urlcCs(t|ƒ|_t|jƒ\|_|_|jƒdS)N)rr†rr„Ú_parse)r€rHrLrLrMrp^s cCsd|_d|_d|_dS)Nrn)r†r„Úselector)r€rLrLrMrpescCs|jS)N)ry)r€rLrLrMrIksz Request.datacCs(||jkr$||_|jdƒr$|jdƒdS)NzContent-length)ryÚ has_headerÚ remove_header)r€rIrLrLrMrIos  cCs d|_dS)N)rI)r€rLrLrMrIyscCsNt|jƒ\|_}|jdkr(td|jƒ‚t|ƒ\|_|_|jrJt|jƒ|_dS)Nzunknown url type: %r) r r†ÚtyperBrpr rvrˆr )r€ÚrestrLrLrMr‡}s  zRequest._parsecCs|jdk rdnd}t|d|ƒS)z3Return a string indicating the HTTP request method.NÚPOSTÚGETr)rIÚgetattr)r€Zdefault_methodrLrLrMÚ get_method…szRequest.get_methodcCs|jS)N)rp)r€rLrLrMÚ get_full_urlŠszRequest.get_full_urlcCs4|jdkr|j r|j|_n||_|j|_||_dS)NÚhttps)r‹rzrvrprˆ)r€rvr‹rLrLrMÚ set_proxy�s  zRequest.set_proxycCs |j|jkS)N)rˆrp)r€rLrLrMÚ has_proxy•szRequest.has_proxycCs||j|jƒ<dS)N)reÚ capitalize)r€r�ÚvalrLrLrMr|˜szRequest.add_headercCs||j|jƒ<dS)N)rxr•)r€r�r–rLrLrMÚadd_unredirected_headerœszRequest.add_unredirected_headercCs||jkp||jkS)N)rerx)r€Ú header_namerLrLrMr‰ s zRequest.has_headercCs|jj||jj||ƒƒS)N)reÚgetrx)r€r˜ÚdefaultrLrLrMrq¤szRequest.get_headercCs |jj|dƒ|jj|dƒdS)N)reÚpoprx)r€r˜rLrLrMrŠ©szRequest.remove_headercCs"|jjƒ}|j|jƒt|jƒƒS)N)rxÚcopyÚupdatereÚlistr{)r€ÚhdrsrLrLrMÚ header_items­s  zRequest.header_items)N)Ú__name__Ú __module__Ú __qualname__rƒÚpropertyrpÚsetterÚdeleterrIr‡r�r‘r“r”r|r—r‰rqrŠr rLrLrLrMrDs(     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)r€Zclient_versionrLrLrMrƒ³s zOpenerDirector.__init__c CsZt|dƒstdt|ƒƒ‚d}�xt|ƒD�]}|dkr:q*|jdƒ}|d|…}||dd…}|jd ƒrÈ|jdƒ|d}||dd…}y t|ƒ}Wntk r¬YnX|jj |iƒ} | |j|<n>|d krÜ|}|j } n*|d krð|}|j } n|d kr*|}|j } nq*| j |gƒ} | �r&tj| |ƒn | j|ƒd }q*W|�rVtj|j|ƒ|j|ƒdS)NÚ add_parentz%expected BaseHandler instance, got %rFÚredirect_requestÚdo_openÚ proxy_openÚ_rRÚerrorrGÚresponseruT)r¯r°r±)Ú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)r€rLrLrMÚcloseíszOpenerDirector.closec Gs<|j|fƒ}x*|D]"}t||ƒ}||Ž}|dk r|SqWdS)N)r™r�) r€ÚchainrÁÚ meth_nameÚargsr©r¼ÚfuncrgrLrLrMÚ _call_chainñs    zOpenerDirector._call_chainNc Csªt|tƒrt||ƒ}n|}|dk r(||_||_|j}|d}x(|jj|gƒD]}t||ƒ}||ƒ}qLW|j ||ƒ} |d}x*|j 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|j|jdd|ƒ}|r|S|j}|j|j||d|ƒ}|r>|S|j|jdd|ƒS)NršZ default_openrÌÚunknownÚ unknown_open)rÉrªr‹)r€rÎrIrgr¿rLrLrMrÌs    zOpenerDirector._opencGs~|d kr,|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|Ž}|r^|S|rz|dd f|}|j|ŽSdS) NÚhttpr’r:z http_error_%srRZ_errorrršÚhttp_error_default)rÑr’)r«rÉ)r€ÚprotorÇÚdictrÆZhttp_errZ orig_argsrgrLrLrMr³'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|j tƒtƒ}xN|D]F}x@|D]8}t|tƒrlt||ƒr€|j|ƒqHt||ƒrH|j|ƒqHWq>Wx|D]}|j|ƒqŽWx|D]}|j|ƒƒq¦Wx&|D]}t|tƒrÔ|ƒ}|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-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)r€ràrLrLrMr®gszBaseHandler.add_parentcCsdS)NrL)r€rLrLrMrÄjszBaseHandler.closecCst|dƒsdS|j|jkS)NÚ handler_orderT)rµrá)r€ÚotherrLrLrMÚ__lt__ns zBaseHandler.__lt__N)r¡r¢r£rár®rÄrãrLrLrLrMrdsc@s eZdZdZdZdd„ZeZdS)r/zProcess HTTP error responses.iècCsJ|j|j|jƒ}}}d|ko*dknsF|jjd|||||ƒ}|S)NéÈi,rÑ)ÚcodeÚmsgrVràr³)r€rur´rårærŸrLrLrMÚ http_response{s z HTTPErrorProcessor.http_responseN)r¡r¢r£Ú__doc__rárçÚhttps_responserLrLrLrMr/ws c@seZdZdd„ZdS)rcCst|j||||ƒ‚dS)N)rrp)r€rÎrdrårærŸrLrLrMrÒ‰sz*HTTPDefaultErrorHandler.http_error_defaultN)r¡r¢r£rÒrLrLrLrMrˆsc@s4eZdZdZdZdd„Zdd„ZeZZZ dZ dS) réé c sx|jƒ}|dkr|dkp&|dko&|dks:t|j||||ƒ‚|jdd ƒ}d‰t‡fd d „|jjƒDƒƒ}t|||jdd�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. é-é.é/é3rŽÚHEADr�ú z%20úcontent-lengthú content-typec3s&|]\}}|jƒˆkr||fVqdS)N)rt)Ú.0ÚkÚv)ÚCONTENT_HEADERSrLrMú ¯sz7HTTPRedirectHandler.redirect_request..T)rer}r~)rìrírîrï)rŽrð)rìrírî)ròró) r�rrpÚreplacerÔrer{rr}) r€rÎrdråræreÚnewurlÚmZ newheadersrL)r÷rMr¯”s  z$HTTPRedirectHandler.redirect_requestc CsNd|kr|d}nd|kr$|d}ndSt|ƒ}|jdkrRt||d||f||ƒ‚|j rp|jrpt|ƒ}d|d <t|ƒ}t|d tj d �}t |j |ƒ}|j ||||||ƒ}|dkr´dSt |d ƒ�r|j} |_| j|d ƒ|jksît| ƒ|jk�rt|j ||j|||ƒ‚ni} |_|_| j|d ƒd| |<|jƒ|jƒ|jj||jd�S)NÚlocationÚurirÑr’Úftprnz+%s - Redirection to url '%s' is not allowedú/r:z iso-8859-1)ÚencodingÚsafeÚ redirect_dictrrR)rJ)rÑr’rþrn)rÚschemerrXZnetlocržrr ÚstringZ punctuationrrpr¯rµrr™Ú max_repeatsr`Úmax_redirectionsÚinf_msgr_rÄràrGrJ) r€rÎrdrårærerúÚ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£rrr¯r Úhttp_error_301Úhttp_error_303Úhttp_error_307rrLrLrLrMrŒs &< c Cs�t|ƒ\}}|jdƒs d}|}n:|jdƒs6td|ƒ‚|jddƒ}|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:rRrS)r r¹rBr¸rr) ÚproxyrZr_schemeÚ authorityÚendZuserinfoÚhostportÚuserÚpasswordrLrLrMÚ _parse_proxyýs       rc@s"eZdZdZddd„Zdd„ZdS)rédNcCs^|dkrtƒ}t|dƒs tdƒ‚||_x2|jƒD]&\}}t|d||||jfdd„ƒq0WdS)NÚkeyszproxies must be a mappingz%s_opencSs ||||ƒS)NrL)Úrrr‹r½rLrLrMÚ%sz'ProxyHandler.__init__..)r5rµÚAssertionErrorÚproxiesr{Úsetattrr±)r€rr‹rHrLrLrMrƒs zProxyHandler.__init__c Cs´|j}t|ƒ\}}}}|dkr"|}|jr6t|jƒr6dS|rv|rvdt|ƒt|ƒf} tj| jƒƒjdƒ} |j dd| ƒt|ƒ}|j ||ƒ||ksš|dkrždS|j j ||j d�SdS)Nz%s:%sÚasciizProxy-authorizationzBasic r’)rJ)r‹rrvÚ proxy_bypassr Úbase64Ú b64encodeÚencodeÚdecoder|r“ràrGrJ) r€rÎrr‹Z orig_typeZ proxy_typerrrZ 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)r€rLrLrMrƒFszHTTPPasswordMgr.__init__cs`t|tƒr|g}|ˆjkr$iˆj|<x6dD].‰t‡‡fdd„|Dƒƒ}||fˆj||<q*WdS)NTFcsg|]}ˆj|ˆƒ‘qSrL)Ú reduce_uri)rôÚu)Ú default_portr€rLrMú Qsz0HTTPPasswordMgr.add_password..)TF)rÊrËr"Útuple)r€Úrealmrýrr"Ú reduced_urirL)r%r€rMÚ add_passwordIs    zHTTPPasswordMgr.add_passwordc Cs`|jj|iƒ}xLdD]D}|j||ƒ}x2|jƒD]&\}}x|D]}|j||ƒr<|SqdD]6}|j||ƒ}x$|jD]}|j||ƒr|j|SqWqWdS)NTF)TF)r#r7r+)r€r,r%r-rýrLrLrMr<¥s     z-HTTPPasswordMgrWithPriorAuth.is_authenticated)F)F)r¡r¢r£rƒr*r;r<Ú __classcell__rLrL)r:rMr"Žs  c@sTeZdZejdejƒZddd„Zdd„Zdd„Z d d „Z d d „Z d d„Z e Z e ZdS)r#z1(?:^|,)[ ]*([^ ,]+)[ ]+realm=(["']?)([^"']*)\2NcCs"|dkrtƒ}||_|jj|_dS)N)r r"r*)r€Z password_mgrrLrLrMrƒÂsz!AbstractBasicAuthHandler.__init__ccstd}xFtjj|ƒD]6}|jƒ\}}}|d kr:tjdtdƒ||fVd}qW|sp|rb|jƒd}nd}|dfVdS) NFú"ú'zBasic Auth Realm was unquotedéTrrn)r>r?)r#ÚrxÚfinditerÚgroupsr?r@Ú UserWarningÚsplit)r€ÚheaderZfound_challengeZmorr r(rLrLrMÚ _parse_realmÈs z%AbstractBasicAuthHandler._parse_realmc Cs~|j|ƒ}|sdSd}xL|D]D}x>|j|ƒD]0\}}|jƒdkrF|}q,|dk r,|j|||ƒSq,WqW|dk rztd|fƒ‚dS)NÚbasiczBAbstractBasicAuthHandler does not support the following scheme: %r)Zget_allrGrtÚretry_http_basic_authrB) r€ÚauthreqrvrÎreZ unsupportedrFrr(rLrLrMÚhttp_error_auth_reqedÜs   z.AbstractBasicAuthHandler.http_error_auth_reqedcCs||jj||ƒ\}}|dk rtd||f}dtj|jƒƒjdƒ}|j|jdƒ|krTdS|j|j|ƒ|j j ||j d�SdSdS)Nz%s:%szBasic r)rJ) r"r.rrr r!rqÚ auth_headerr—ràrGrJ)r€rvrÎr(rÚpwÚrawÚauthrLrLrMrIös z.AbstractBasicAuthHandler.retry_http_basic_authcCsxt|jdƒ s|jj|jƒ r"|S|jdƒst|jjd|jƒ\}}dj||ƒjƒ}tj |ƒj ƒ}|j ddj|j ƒƒƒ|S)Nr<Ú Authorizationz{0}:{1}zBasic {}) rµr"r<rpr‰r.r…r rZstandard_b64encoder!r—Ústrip)r€rÎrr"Z credentialsZauth_strrLrLrMÚ http_requests z%AbstractBasicAuthHandler.http_requestcCsLt|jdƒrHd|jko dknr8|jj|jdƒn|jj|jdƒ|S)Nr<räi,TF)rµr"rår;rp)r€rÎr´rLrLrMrçs  z&AbstractBasicAuthHandler.http_response)N)r¡r¢r£ÚreÚcompileÚIrArƒrGrKrIrRrçÚ https_requestrérLrLrLrMr#­s   c@seZdZdZdd„ZdS)r$rPcCs|j}|jd|||ƒ}|S)Nzwww-authenticate)rprK)r€rÎrdrårærerHr´rLrLrMÚhttp_error_401 s z#HTTPBasicAuthHandler.http_error_401N)r¡r¢r£rLrWrLrLrLrMr$sc@seZdZdZdd„ZdS)r%zProxy-authorizationcCs|j}|jd|||ƒ}|S)Nzproxy-authenticate)rvrK)r€rÎrdrårærerr´rLrLrMÚhttp_error_407+s z$ProxyBasicAuthHandler.http_error_407N)r¡r¢r£rLrXrLrLrLrMr%'sc@sNeZdZddd„Zdd„Zdd„Zdd „Zd d „Zd d „Zdd„Z dd„Z dS)r&NcCs4|dkrtƒ}||_|jj|_d|_d|_d|_dS)Nr)r r"r*ÚretriedÚ nonce_countÚ last_nonce)r€r"rLrLrMrƒEs z"AbstractDigestAuthHandler.__init__cCs d|_dS)Nr)rY)r€rLrLrMÚreset_retry_countNsz+AbstractDigestAuthHandler.reset_retry_countcCs||j|dƒ}|jdkr*t|jdd|dƒ‚n|jd7_|rx|jƒd}|jƒdkr`|j||ƒS|jƒdkrxtd|ƒ‚dS) Néi‘zdigest auth failedrRrZdigestrHzEAbstractDigestAuthHandler does not support the following scheme: '%s')r™rYrrprErtÚretry_http_digest_authrB)r€rLrvrÎrerJrrLrLrMrKQs        z/AbstractDigestAuthHandler.http_error_auth_reqedcCsz|jddƒ\}}ttdt|ƒƒƒ}|j||ƒ}|rvd|}|jj|jdƒ|krRdS|j|j|ƒ|j j ||j d�}|SdS)NrñrRz Digest %s)rJ) rEÚparse_keqv_listÚfilterÚparse_http_listÚget_authorizationrer™rLr—ràrGrJ)r€rÎrOÚtokenZ challengeÚchalZauth_valZresprLrLrMr^es z0AbstractDigestAuthHandler.retry_http_digest_authcCs@d|j|tjƒf}|jdƒtdƒ}tj|ƒjƒ}|dd…S)Nz %s:%s:%s:rrQé)rZÚtimeZctimer Ú _randombytesÚhashlibÚsha1Ú hexdigest)r€ÚnonceÚsÚbÚdigrLrLrMÚ get_cnonceqsz$AbstractDigestAuthHandler.get_cnoncecCs¾y6|d}|d}|jdƒ}|jddƒ}|jddƒ}Wntk rJdSX|j|ƒ\}} |dkrfdS|jj||jƒ\} } | dkr†dS|jdk r |j|j|ƒ} nd} d| || f} d|jƒ|j f}|d k�r.||j krè|j d 7_ n d |_ ||_ d |j }|j |ƒ}d ||||||ƒf}| || ƒ|ƒ}n2|dk�rT| || ƒd|||ƒfƒ}n t d |ƒ‚d| |||j |f}|�r†|d|7}| �r˜|d| 7}|d|7}|�rº|d||f7}|S)Nr(rkÚqopÚ algorithmÚMD5Úopaquez%s:%s:%sz%s:%srOrRz%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_implsr"r.rprIÚget_entity_digestr�rˆr[rZror)r€rÎrdr(rkrprqrsÚHÚKDrrMZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr4rLrLrMrb|sV              z+AbstractDigestAuthHandler.get_authorizationcsD|dkrdd„‰n|dkr$dd„‰n td|ƒ‚‡fdd„}ˆ|fS)NrrcSstj|jdƒƒjƒS)Nr)rhZmd5r rj)ÚxrLrLrMr»sz?AbstractDigestAuthHandler.get_algorithm_impls..ZSHAcSstj|jdƒƒjƒS)Nr)rhrir rj)ryrLrLrMr½sz.Unsupported digest authentication algorithm %rcsˆd||fƒS)Nz%s:%srL)rlÚd)rwrLrMrÂs)rB)r€rqrxrL)rwrMru¸s   z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrL)r€rIrdrLrLrMrvÅsz+AbstractDigestAuthHandler.get_entity_digest)N) r¡r¢r£rƒr\rKr^rorbrurvrLrLrLrMr&: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. rPiêcCs*t|jƒd}|jd|||ƒ}|jƒ|S)NrRzwww-authenticate)rrprKr\)r€rÎrdrårærervÚretryrLrLrMrWÔs  z$HTTPDigestAuthHandler.http_error_401N)r¡r¢r£rèrLrárWrLrLrLrMr'Êsc@seZdZdZdZdd„ZdS)r(zProxy-AuthorizationiêcCs"|j}|jd|||ƒ}|jƒ|S)Nzproxy-authenticate)rvrKr\)r€rÎrdrårærervr{rLrLrMrXás  z%ProxyDigestAuthHandler.http_error_407N)r¡r¢r£rLrárXrLrLrLrMr(Üsc@s6eZdZd dd„Zdd„Zdd„Zdd „Zd d „Zd S)ÚAbstractHTTPHandlerrcCs ||_dS)N)Ú _debuglevel)r€Ú debuglevelrLrLrMrƒêszAbstractHTTPHandler.__init__cCs ||_dS)N)r})r€ÚlevelrLrLrMÚset_http_debuglevelísz'AbstractHTTPHandler.set_http_debuglevelcCstjjj|j|jƒƒS)N)rÑrØÚHTTPConnectionÚ_get_content_lengthrIr�)r€rurLrLrMr‚ðsz'AbstractHTTPHandler._get_content_lengthc Cs |j}|stdƒ‚|jdk r–|j}t|tƒr8d}t|ƒ‚|jdƒsN|jddƒ|jdƒ r–|jdƒ r–|j|ƒ}|dk rŠ|jdt|ƒƒn |jddƒ|}|j ƒr¼t |j ƒ\}}t |ƒ\}} |jdƒsÒ|jd|ƒx2|j jD]&\} } | jƒ} |j| ƒsÜ|j| | ƒ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-encodingZchunkedro)rvrrIrÊrËr¶r‰r—r‚r”r rˆr ràr¨r•) r€rurvrIræZcontent_lengthZsel_hostrZselZsel_pathr[r‚rLrLrMÚ do_request_õs>          zAbstractHTTPHandler.do_request_c  s\|j}|stdƒ‚||fd|ji|—Ž}|j|jƒt|jƒ‰ˆjt‡fdd„|jj ƒDƒƒƒdˆd<tdd„ˆj ƒDƒƒ‰|j r¶i}d}|ˆkr¦ˆ|||<ˆ|=|j |j |d �y`y&|j |j ƒ|j|jˆ|jd ƒd �Wn,tk �r }zt|ƒ‚WYd d }~XnX|jƒ} Wn|jƒ‚YnX|j�rF|jjƒd |_|jƒ| _| 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 givenrJc3s"|]\}}|ˆkr||fVqdS)NrL)rôrõrö)rerLrMrø)sz.AbstractHTTPHandler.do_open..rÄÚ Connectioncss|]\}}|jƒ|fVqdS)N)Útitle)rôr[r–rLrLrMrø6szProxy-Authorization)rezTransfer-encoding)Zencode_chunkedN)rvrrJZset_debuglevelr}rÔrxr�rer{rzZ set_tunnelrur�rˆrIr‰rmÚ getresponserÄZsockr‘rHÚreasonræ) r€Z http_classrÎZhttp_conn_argsrvrßZtunnel_headersZproxy_auth_hdrÚerrrrL)rerMr°s@  "     zAbstractHTTPHandler.do_openN)r)r¡r¢r£rƒr€r‚rƒr°rLrLrLrMr|ès  &r|c@seZdZdd„ZejZdS)r)cCs|jtjj|ƒS)N)r°rÑrØr�)r€rÎrLrLrMÚ http_open`szHTTPHandler.http_openN)r¡r¢r£r‰r|rƒrRrLrLrLrMr)^sr×c@s$eZdZddd„Zdd„ZejZdS)rErNcCstj||ƒ||_||_dS)N)r|rƒÚ_contextÚ_check_hostname)r€r~r>Úcheck_hostnamerLrLrMrƒis zHTTPSHandler.__init__cCs|jtjj||j|jd�S)N)r>rŒ)r°rÑrØr×rŠr‹)r€rÎrLrLrMÚ https_openns zHTTPSHandler.https_open)rNN)r¡r¢r£rƒr�r|rƒrVrLrLrLrMrEgs rEc@s.eZdZddd„Zdd„Zdd„ZeZeZdS) rNcCs$ddl}|dkr|jjƒ}||_dS)Nr)Zhttp.cookiejarÚ cookiejarZ CookieJar)r€rŽrÑrLrLrMrƒws zHTTPCookieProcessor.__init__cCs|jj|ƒ|S)N)rŽZadd_cookie_header)r€rurLrLrMrR}s z HTTPCookieProcessor.http_requestcCs|jj||ƒ|S)N)rŽZextract_cookies)r€rur´rLrLrMrç�sz!HTTPCookieProcessor.http_response)N)r¡r¢r£rƒrRrçrVrérLrLrLrMrvs  c@seZdZdd„ZdS)r.cCs|j}td|ƒ‚dS)Nzunknown url type: %s)r‹r)r€rÎr‹rLrLrMrЉszUnknownHandler.unknown_openN)r¡r¢r£rÐrLrLrLrMr.ˆscCsRi}xH|D]@}|jddƒ\}}|ddkrB|ddkrB|dd…}|||<q W|S)z>Parse list of key=value strings where keys are not duplicated.ú=rRrr>rSrS)rE)ÚlZparsedZeltrõrörLrLrMr_�s   r_cCs¢g}d}d}}xt|D]l}|r,||7}d}q|rV|dkr@d}qn |dkrLd}||7}q|dkrn|j|ƒd}q|dkrzd}||7}qW|r”|j|ƒ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. rnFú\Tr>ú,cSsg|] }|jƒ‘qSrL)rQ)rôÚpartrLrLrMr&Àsz#parse_http_list..)r])rlÚresr“Úescaper ZcurrLrLrMra—s4     rac@s(eZdZdd„ZdZdd„Zdd„ZdS)r*cCs\|j}|dd…dkrN|dd…dkrN|jrN|jdkrN|j|jƒkrXtdƒ‚n |j|ƒSdS)Nr:z//r@rÿÚ localhostz-file:// scheme is supported only on localhost)rˆrvÚ get_namesrÚopen_local_file)r€rÎrHrLrLrMÚ file_openÄs &  zFileHandler.file_openNc Cs`tjdkrZy*ttjdƒdtjtjƒƒdƒt_Wn$tjk rXtjdƒft_YnXtjS)Nr–r:)r*Únamesr'rÕÚgethostbyname_exÚ gethostnameÚgaierrorÚ gethostbyname)r€rLrLrMr—Ïs  zFileHandler.get_namescCsddl}ddl}|j}|j}t|ƒ}y¤tj|ƒ}|j}|jj |j dd�} |j |ƒd} |j d| pbd|| fƒ} |r~t |ƒ\}} | sš| rÆt|ƒ|jƒkrÆ|r¬d||} nd|} tt|dƒ| | ƒSWn*tk rò}zt|ƒ‚WYdd}~XnXtdƒ‚dS) NrT)Úusegmtz6Content-type: %s Content-length: %d Last-modified: %s z text/plainzfile://Úrbzfile not on local host)Ú email.utilsÚ mimetypesrvrˆr4rWÚstatÚst_sizeÚutilsÚ formatdateÚst_mtimeÚ guess_typeÚmessage_from_stringr Ú_safe_gethostbynamer—rrGrmr)r€rÎÚemailr¢rvrbZ localfileÚstatsriÚmodifiedÚmtyperer1ZorigurlÚexprLrLrMr˜Ús0  zFileHandler.open_local_file)r¡r¢r£r™ršr—r˜rLrLrLrMr*Âs  c Cs&y tj|ƒStjk r dSXdS)N)rÕržr�)rvrLrLrMrªõs rªc@seZdZdd„Zdd„ZdS)r+cCs.ddl}ddl}|j}|s"tdƒ‚t|ƒ\}}|dkr>|j}nt|ƒ}t|ƒ\}}|rdt|ƒ\}}nd}t |ƒ}|pvd}|p~d}yt j |ƒ}Wn*t k r¸}zt|ƒ‚WYdd}~XnXt |jƒ\} } | jdƒ} ttt | ƒƒ} | dd…| d} } | �r| d �r| dd…} yÐ|j||||| |jƒ} | �r8d�p:d}x:| D]2}t|ƒ\}}|jƒdk�rB|dk�rB|jƒ}�qBW| j| |ƒ\}}d}|j|jƒd}|�r®|d |7}|dk �rÎ|dk�rÎ|d|7}tj|ƒ}t|||jƒS|jk �r(}z$td|ƒ}|jtj ƒdƒ‚WYdd}~XnXdS)Nrzftp error: no host givenrnrÿrRrUÚDr‹ÚaÚAr¾rzzContent-type: %s zContent-length: %d z ftp error: %rr:rSrS)r±r²r¾rUrzr°)!Úftplibr¢rvrr ÚFTP_PORTr^rrr rÕržrmrrˆrEržÚmapÚ connect_ftprJrrtÚupperÚretrfiler¨rpr«r©rÚ all_errorsÚwith_tracebackÚsysÚexc_info)r€rÎr³r¢rvr1rr"rærXÚattrsÚdirsrNÚfwr‹Úattrr‚rdÚretrlenrer®r¯ÚexcrLrLrMÚftp_openüs\           zFTPHandler.ftp_openc Cst||||||dd�S)NF)Ú persistent)Ú ftpwrapper)r€rr"rvr1r¾rJrLrLrMr¶1szFTPHandler.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é<re)ÚcacherJÚsoonestÚdelayÚ max_conns)r€rLrLrMrƒ8s zCacheFTPHandler.__init__cCs ||_dS)N)rÉ)r€ÚtrLrLrMÚ setTimeout?szCacheFTPHandler.setTimeoutcCs ||_dS)N)rÊ)r€rûrLrLrMÚ setMaxConnsBszCacheFTPHandler.setMaxConnscCsr|||dj|ƒ|f}||jkr4tjƒ|j|j|<n,t||||||ƒ|j|<tjƒ|j|j|<|jƒ|j|S)Nrÿ)ÚjoinrÇrfrÉrJrÅÚ check_cache)r€rr"rvr1r¾rJr�rLrLrMr¶Es  zCacheFTPHandler.connect_ftpcCsÈtjƒ}|j|krTx@t|jjƒƒD].\}}||kr"|j|jƒ|j|=|j|=q"Wtt|jjƒƒƒ|_t |jƒ|j krÄx6t|jjƒƒD]$\}}||jkrˆ|j|=|j|=PqˆWtt|jjƒƒƒ|_dS)N) rfrÈržrJr{rÇrÄÚminÚvaluesr`rÊ)r€rËrõrörLrLrMrÏPs   zCacheFTPHandler.check_cachecCs4x|jjƒD] }|jƒq W|jjƒ|jjƒdS)N)rÇrÑrÄÚclearrJ)r€ÚconnrLrLrMÚ clear_cacheds  zCacheFTPHandler.clear_cacheN) r¡r¢r£rƒrÌrÍr¶rÏrÔrLrLrLrMr,5s  c@seZdZdd„ZdS)r-cCs~|j}|jddƒ\}}|jddƒ\}}t|ƒ}|jdƒrNtj|ƒ}|dd…}|sVd}tjd|t|ƒfƒ}t t j |ƒ||ƒS) Nú:rRr’z;base64éztext/plain;charset=US-ASCIIz$Content-type: %s Content-length: %d iùÿÿÿ) rprErÚendswithrÚ decodebytesr«r©r`rÚioÚBytesIO)r€rÎrHrrIZ mediatypererLrLrMÚ data_openks    zDataHandler.data_openN)r¡r¢r£rÛrLrLrLrMr-jsrëÚ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 )ÚpathnamerLrLrMr4‘scCst|ƒ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ƒ‚||_|j dƒ|_ |j 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ÚAcceptú*/*)rârã)r:r¡r?r@rAr5rµrrr™ràráÚversionr¨Ú_URLopener__tempfilesrWrlÚ_URLopener__unlinkÚ tempcacheÚftpcache)r€rZx509rærLrLrMrƒ¬s  zURLopener.__init__cCs |jƒdS)N)rÄ)r€rLrLrMÚ__del__ÆszURLopener.__del__cCs |jƒdS)N)Úcleanup)r€rLrLrMrÄÉszURLopener.closec CsZ|jrFx2|jD](}y|j|ƒWqtk r4YqXqW|jdd…=|jrV|jjƒdS)N)rårærmrçrÒ)r€rNrLrLrMrêÌs   zURLopener.cleanupcGs|jj|ƒdS)zdAdd a header to be used by the HTTP interface only e.g. u.addheader('Accept', 'sound/basic')N)r¨r])r€rÇrLrLrMÚ addheaderÚszURLopener.addheadercCsntt|ƒƒ}t|dd�}|jrL||jkrL|j|\}}t|dƒ}t|||ƒSt|ƒ\}}|s`d}||jkr–|j|}t|ƒ\}} t| ƒ\} } | |f}nd}d|} ||_ | j ddƒ} t || ƒ sÈ| d kræ|rÚ|j |||ƒS|j ||ƒSy,|dk�rt|| ƒ|ƒSt|| ƒ||ƒSWnVttfk �r.‚Yn<tk �rh} ztd | ƒjtjƒd ƒ‚WYdd} ~ XnXdS) z6Use URLopener().open(file) instead of open(file, 'r').z%/:=&?~#+!$,;'@()*[]|)rr rNNZopen_ú-r²r˜z socket errorr:)rrr rçrGrr rr r‹rùrµÚopen_unknown_proxyÚ open_unknownr�rrrmrºr»r¼)r€rÍrIrbrerdÚurltyperHrÚ proxyhostrvrˆr[rærLrLrMrGàs<             zURLopener.opencCst|ƒ\}}tdd|ƒ‚dS)z/Overridable interface to open unknown URL type.z url errorzunknown url typeN)r rm)r€rÍrIr‹rHrLrLrMrîs zURLopener.open_unknowncCs t|ƒ\}}tdd||ƒ‚dS)z/Overridable interface to open unknown URL type.z url errorzinvalid proxy for %sN)r rm)r€rrÍrIr‹rHrLrLrMrí s zURLopener.open_unknown_proxyc Cs&tt|ƒƒ}|jr&||jkr&|j|St|ƒ\}}|dkrš| sH|dkršy.|j|ƒ}|jƒ}|jƒtt|ƒdƒ|fSt k r˜} zWYdd} ~ XnX|j ||ƒ}�zH|jƒ} |rÂt |dƒ} n|ddl } t|ƒ\} }t|pÞdƒ\} }t |pîdƒ\}} t |�pdƒ\}} tjj|ƒd}| j|ƒ\}}|jj|ƒtj|dƒ} z¤|| f}|jdk �r^||j|<d }d }d}d}d | k�r„t| d ƒ}|�r–||||ƒxH|j|ƒ}|�sªP|t|ƒ7}| j|ƒ|d7}|�r˜||||ƒ�q˜WWd| jƒXWd|jƒX|dk�r"||k�r"td ||f|ƒ‚|S)ztretrieve(url) returns (filename, headers) for a local object or (tempfilename, headers) for a remote object.NrNrRrOrrnirQzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rS)rrrçr r˜rVrÄr4r rmrGrZrrrWrXÚsplitextZmkstemprår]Úfdopenr^r_r`rar)r€rHrbrcrIr‹Zurl1rdrŸrærerfrZZgarbagerXÚsuffixÚfdrgrhrir_rjrkrLrLrMÚretrievesl                  zURLopener.retrievecCs(d}d}t|tƒrDefault error handler: close the connection and raise OSError.N)rÄr)r€rHrdrùrúrerLrLrMrÒÁszURLopener.http_error_defaultcCstjj||j|jd�S)N)ràrá)rÑrØr×ràrá)r€rvrLrLrMÚ_https_connectionÇszURLopener._https_connectioncCs|j|j||ƒS)zUse HTTPS protocol.)r÷rû)r€rHrIrLrLrMÚ open_httpsÌszURLopener.open_httpscCs^t|tƒstdƒ‚|dd…dkrP|dd…dkrP|dd…jƒdkrPtd ƒ‚n |j|ƒSdS) z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr:z//r@rÿé z localhost/z-file:// scheme is supported only on localhost)rÊrËrrtrBr˜)r€rHrLrLrMÚ open_fileÐs  4 zURLopener.open_filecCs\ddl}ddl}t|ƒ\}}t|ƒ}ytj|ƒ}Wn0tk rb}zt|j|j ƒ‚WYdd}~XnX|j } |j j |j dd�} |j|ƒd} |jd| p–d| | fƒ} |sÔ|} |dd…dkrÂd |} tt|d ƒ| | ƒSt|ƒ\}}| oütj|ƒtƒftƒk�rP|} |dd…dk�r d |} n|dd …d k�r>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 r4rWr£rmrÚstrerrorrbr¤r¥r¦r§r¨r©rrGr rÕržr–ÚthishostrB)r€rHr«r¢rvrNZ localnamer¬Úerir­r®reZurlfiler1rLrLrMr˜Ùs:     zURLopener.open_local_filecCs˜t|tƒstdƒ‚ddl}t|ƒ\}}|s2tdƒ‚t|ƒ\}}t|ƒ\}}|r\t|ƒ\}}nd}t|ƒ}t|ppdƒ}t|p|dƒ}t j |ƒ}|sžddl }|j }nt |ƒ}t|ƒ\}} t|ƒ}|jdƒ} | dd…| d} } | oæ| d �rö| dd…} | �r| d �rd| d<|||dj| ƒf} t|jƒtk�rlx8t|jƒD]*} | | k�r>|j| }|j| =|jƒ�q>Wyæ| |jk�r�t||||| ƒ|j| <| �sœd}nd }x:| D]2}t|ƒ\}}|jƒd k�r¦|dk�r¦|jƒ}�q¦W|j| j| |ƒ\}}|jd|ƒd}d}|�r|d|7}|dk �r:|dk�r:|d|7}tj|ƒ}t||d|ƒStƒk �r’}z td|ƒj t!j"ƒdƒ‚WYdd}~XnXdS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNzftp error: no host givenrnrÿrRr°rUr‹r±r²r¾rzzftp:zContent-Type: %s zContent-Length: %d z ftp error %rr:rSrS)r±r²r¾rUrzr°)#rÊrËrr¢r r rrr rÕržr³r´r^rrErÎr`rèÚ MAXFTPCACHEržrÄrÅrrtr·r¸r¨r«r©rÚ ftperrorsrºr»r¼)r€rHr¢rvrXr1rr"r³r½r¾rNr�rõrör‹rÀr‚rdrÁr®rer¯rLrLrMÚopen_ftpùsp                   zURLopener.open_ftpc Cs<t|tƒstdƒ‚y|jddƒ\}}Wntk rDtddƒ‚YnX|sNd}|jdƒ}|dkrŽd ||d …krŽ||dd …}|d |…}nd }g}|jd tj d tj tjƒƒƒƒ|jd|ƒ|dkrät j |j dƒƒjdƒ}nt|ƒ}|jdt|ƒƒ|jd ƒ|j|ƒdj|ƒ}tj|ƒ}tj|ƒ}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�NrnzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %dÚ )rÊrËrrErBrmÚrfindr]rfZstrftimeZgmtimerrØr r!r r`rÎr«r©rÙÚStringIOr) r€rHrIr‹ZsemirræreÚfrLrLrMÚ open_data3s6        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þr˜rr rLrLrLrMr8Ÿs.  $   B\     :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)r€rÇr9rLrLrMrƒ`szFancyURLopener.__init__cCst||d||ƒS)z3Default error handling -- don't raise an exception.zhttp:)r)r€rHrdrùrúrerLrLrMrÒfsz!FancyURLopener.http_error_defaultNc Csn|jd7_zR|jrJ|j|jkrJt|dƒr4|j}n|j}|||dd|ƒS|j||||||ƒ}|Sd|_XdS)z%Error 302 -- relocated (temporarily).rRÚhttp_error_500iôz)Internal Server Error: Redirect RecursionNr)r r rµrrÒÚredirect_internal) r€rHrdrùrúrerIr½rgrLrLrMr js  zFancyURLopener.http_error_302c Csxd|kr|d}nd|kr$|d}ndS|jƒt|jd||ƒ}t|ƒ}|jd krnt|||d|||ƒ‚|j|ƒS) NrürýrÕrÑr’rþrnz( Redirection to url '%s' is not allowed.)rÑr’rþrn)rÄrr‹rrrrG) r€rHrdrùrúrerIrúrrLrLrMr|s   z FancyURLopener.redirect_internalcCs|j||||||ƒS)z*Error 301 -- also relocated (permanently).)r )r€rHrdrùrúrerIrLrLrMr ˜szFancyURLopener.http_error_301cCs|j||||||ƒS)z;Error 303 -- also relocated (essentially identical to 302).)r )r€rHrdrùrúrerIrLrLrMr œszFancyURLopener.http_error_303cCs2|dkr|j||||||ƒS|j|||||ƒSdS)z1Error 307 -- relocated, but turn POST into error.N)r rÒ)r€rHrdrùrúrerIrLrLrMr  szFancyURLopener.http_error_307Fc CsÈd|krtj||||||ƒ|d}tjd|ƒ} | sHtj||||||ƒ| jƒ\} } | jƒdkrttj||||||ƒ|sŒtj||||||ƒd|jd} |dkr²t|| ƒ|| ƒSt|| ƒ|| |ƒSdS)z_Error 401 -- authentication required. This function supports Basic authentication only.zwww-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"rHZretry_Ú _basic_authN)r8rÒrSÚmatchrCrtr‹r�) r€rHrdrùrúrerIr{Ústuffrrr(r[rLrLrMrW§s&         zFancyURLopener.http_error_401c CsÈd|krtj||||||ƒ|d}tjd|ƒ} | sHtj||||||ƒ| jƒ\} } | jƒdkrttj||||||ƒ|sŒtj||||||ƒd|jd} |dkr²t|| ƒ|| ƒSt|| ƒ|| |ƒSdS)zeError 407 -- proxy authentication required. This function supports Basic authentication only.zproxy-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"rHZ retry_proxy_rN)r8rÒrSrrCrtr‹r�) r€rHrdrùrúrerIr{rrrr(r[rLrLrMrXÀs&         zFancyURLopener.http_error_407cCsÄt|ƒ\}}d||}|jd}t|ƒ\}} t| ƒ\} } | jdƒd} | | d…} |j| || ƒ\} } | pl| srdSdt| dd�t| dd�| f} d| | |jd<|dkr´|j|ƒS|j||ƒSdS)Nzhttp://rÑú@rRz%s:%s@%srn)r)r rr r¸Úget_user_passwdr rG)r€rHr(rIrvrˆrúrrïrðÚ proxyselectorr¾rr"rLrLrMÚretry_proxy_http_basic_authÙs         z*FancyURLopener.retry_proxy_http_basic_authcCsÄt|ƒ\}}d||}|jd}t|ƒ\}} t| ƒ\} } | jdƒd} | | d…} |j| || ƒ\} } | pl| srdSdt| dd�t| dd�| f} d| | |jd<|dkr´|j|ƒS|j||ƒSdS)Nzhttps://r’rrRz%s:%s@%srn)r)r rr r¸rr rG)r€rHr(rIrvrˆrúrrïrðrr¾rr"rLrLrMÚretry_proxy_https_basic_authës         z+FancyURLopener.retry_proxy_https_basic_authc Cs�t|ƒ\}}|jdƒd}||d…}|j|||ƒ\}}|p>|sDdSdt|dd�t|dd�|f}d||} |dkr€|j| ƒS|j| |ƒSdS)NrrRz%s:%s@%srn)rzhttp://)r r¸rr rG) r€rHr(rIrvrˆr¾rr"rúrLrLrMrIýs     z$FancyURLopener.retry_http_basic_authc Cs�t|ƒ\}}|jdƒd}||d…}|j|||ƒ\}}|p>|sDdSdt|dd�t|dd�|f}d||} |dkr€|j| ƒS|j| |ƒSdS)NrrRz%s:%s@%srn)rzhttps://)r r¸rr rG) r€rHr(rIrvrˆr¾rr"rúrLrLrMÚretry_https_basic_auth s     z%FancyURLopener.retry_https_basic_authrcCs`|d|jƒ}||jkr2|r(|j|=n |j|S|j||ƒ\}}|sJ|rX||f|j|<||fS)Nr)rtr Úprompt_user_passwd)r€rvr(rÔr�rr"rLrLrMr s   zFancyURLopener.get_user_passwdc CsTddl}y,td||fƒ}|jd|||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)r€rvr(rrr"rLrLrMr$ sz!FancyURLopener.prompt_user_passwd)N)N)N)N)NF)NF)N)N)N)N)r)r¡r¢r£rèrƒrÒr rr r r rWrXrrrIrrrrLrLrLrMr9]s$           cCstdkrtjdƒatS)z8Return the IP address of the magic hostname 'localhost'.Nr–)Ú _localhostrÕržrLrLrLrMr–4 s r–c CsPtdkrLyttjtjƒƒdƒaWn(tjk rJttjdƒdƒaYnXtS)z,Return the IP addresses of the current host.Nr:r–)Ú _thishostr'rÕr›rœr�rLrLrLrMr< s rcCstdkrddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)Ú _ftperrorsr³r¹)r³rLrLrMrG srcCstdkrtjdƒatS)z%Return an empty email Message object.Nrn)Ú _noheadersr«r©rLrLrLrMÚ noheadersP s r"c@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.NTc CsX||_||_||_||_||_||_d|_||_y |jƒWn|j ƒ‚YnXdS)Nr) rr"rvr1r¾rJÚrefcountÚ keepaliveÚinitrÄ)r€rr"rvr1r¾rJrÄrLrLrMrƒ] s zftpwrapper.__init__cCs\ddl}d|_|jƒ|_|jj|j|j|jƒ|jj|j |j ƒdj |j ƒ}|jj |ƒdS)Nrrÿ)r³ÚbusyZFTPrþZconnectrvr1rJZloginrr"rÎr¾Úcwd)r€r³Z_targetrLrLrMr%m s  zftpwrapper.initc -Cs¸ddl}|jƒ|dkr"d}d}n d|}d}y|jj|ƒWn*|jk rh|jƒ|jj|ƒYnXd}|rè| rèyd|}|jj|ƒ\}}WnR|jk ræ}z4t|ƒdd…d krÖt d |ƒj t j ƒd ƒ‚WYdd}~XnX|�s‚|jjdƒ|�rn|jj ƒ} zJy|jj|ƒWn4|jk �rP}zt d |ƒ|‚WYdd}~XnXWd|jj| ƒXd |}nd }|jj|ƒ\}}d|_t|jdƒ|jƒ} |jd7_|jƒ| |fS)Nrrzr°zTYPE ArRzTYPE zRETR r@Z550z ftp error: %rr:zLIST ZLISTr )rzr°)r³Ú endtransferrþZvoidcmdr¹r%Z ntransfercmdZ error_permrËrrºr»r¼Úpwdr'r&rÚmakefileÚ file_closer#rÄ) r€rNr‹r³ÚcmdÚisdirrÓrÁr‡r)ZftpobjrLrLrMr¸v sN     $ zftpwrapper.retrfilecCs d|_dS)Nr)r&)r€rLrLrMr(£ szftpwrapper.endtransfercCsd|_|jdkr|jƒdS)NFr)r$r#Ú real_close)r€rLrLrMrĦ s zftpwrapper.closecCs4|jƒ|jd8_|jdkr0|j r0|jƒdS)NrRr)r(r#r$r.)r€rLrLrMr+« szftpwrapper.file_closec Cs2|jƒy|jjƒWntƒk r,YnXdS)N)r(rþrÄr)r€rLrLrMr.± s zftpwrapper.real_close)NT) r¡r¢r£rèrƒr%r¸r(rÄr+r.rLrLrLrMrÅZ s  -rÅcCs¼i}xBtjjƒD]4\}}|jƒ}|r|dd…dkr|||dd…<qWdtjkr^|jddƒxXtjjƒD]J\}}|dd…dkrj|jƒ}|r |||dd …<qj|j|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. éNÚ_proxyZREQUEST_METHODrÑiúÿÿÿiúÿÿÿiúÿÿÿiúÿÿÿiúÿÿÿ)rWÚenvironr{rtr›)rr[r‚rLrLrMÚgetproxies_environment¹ s   r2c Cs²|dkrtƒ}y |d}Wntk r.dSX|dkr.r’Ú.z (.+\.)?%s$) r2rtr rEÚlstriprSr•rrU)rvrZno_proxyÚhostonlyr1Z no_proxy_listr[ÚpatternrLrLrMÚproxy_bypass_environmentØ s&     r9c Csddlm}t|ƒ\}}dd„}d|kr4|dr4dSd}xÞ|jd fƒD]Î}|sPqFtjd |ƒ}|dk �r|dkrœytj|ƒ}||ƒ}Wntk ršwFYnX||jd ƒƒ} |jd ƒ} | dkrÖd |jd ƒj dƒd } nt | d d…ƒ} d| } || ?| | ?k�rdSqF|||ƒ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|jdƒ}ttt|ƒƒ}t|ƒdkr<|ddddgdd…}|dd>|dd>B|dd>B|d BS) Nr5rêrérRrer:rQr@)rEržrµr^r`)ZipAddrr0rLrLrMÚip2num s   z,_proxy_bypass_macosx_sysconf..ip2numr5Zexclude_simpleTNÚ exceptionsz(\d+(?:\.\d+)*)(/\d+)?rRr:rQé F) r:r r™rSrrÕržrmÚgroupÚcountr^) rvÚproxy_settingsr:r7r1r<ZhostIPr‚rûr4ÚmaskrLrLrMÚ_proxy_bypass_macosx_sysconfû s:        rCÚdarwin)Ú_get_proxy_settingsÚ _get_proxiescCstƒ}t||ƒS)N)rErC)rvrArLrLrMÚproxy_bypass_macosx_sysconf: srGcCstƒS)z±Return a dictionary of scheme -> proxy server URL mappings. This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. )rFrLrLrLrMÚgetproxies_macosx_sysconf> srHcCs 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)r2r9rG)rvrrLrLrMrH s rcCs tƒp tƒS)N)r2rHrLrLrLrMr5U scCsi}y ddl}Wntk r$|SXyÎ|j|jdƒ}|j|dƒd}|rêt|j|dƒdƒ}d|kr¬x‚|jdƒD]4}|jddƒ\}}tjd |ƒsžd ||f}|||<qrWn>|dd …d krÆ||d <n$d||d <d||d<d||d<|j ƒ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�rrRz ^([^/:]+)://z%s://%sr]zhttp:rÑz http://%sz https://%sr’zftp://%srþ) ÚwinregÚ ImportErrorÚOpenKeyÚHKEY_CURRENT_USERÚ QueryValueExrËrErSrZClosermrBr¶)rrJÚinternetSettingsÚ proxyEnableZ proxyServerÚpr¿ZaddressrLrLrMÚgetproxies_registryZ s8          rRcCs 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. )r2rRrLrLrLrMr5‡ sc &Cs~y ddl}Wntk r dSXy6|j|jdƒ}|j|dƒd}t|j|dƒdƒ}Wntk rldSX| sz| r~dSt|ƒ\}}|g}y tj |ƒ}||kr®|j |ƒWntk rÄYnXy tj |ƒ}||krä|j |ƒWntk �rüYnX|j dƒ}xp|D]h} | dk�r*d|k�r*dS| j dd ƒ} | j d d ƒ} | j d dƒ} x$|D]} tj| | tjƒ�rTdS�qTW�qWdS) Nrz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsrIZ ProxyOverriderzr5rRz\.r4z.*ú?)rJrKrLrMrNrËrmr rÕržr]ZgetfqdnrErùrSrrU) rvrJrOrPZ proxyOverrideZrawHostr1ZaddrZfqdnr5r–rLrLrMÚproxy_bypass_registry� sR              rTcCs 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)r2r9rT)rvrrLrLrMr s )NNN)N)rèrr»r«rhZ http.clientrÑrÙrWr2rSrÕrr»rfÚ collectionsrZrTr?Z urllib.errorrrrZ urllib.parserrrrr r r r r rrrrrrrrrZurllib.responserrrDrKrCÚ__all__Ú version_infor§rFrÖr0r1r\r6r7rTÚASCIIrrrwrrr2rr/rrrrr r!r"r#r$r%Úurandomrgr&r'r(r|r)rµrØrEr]rr.r_rar*rªr+r,r-rr[Z nturl2pathr4r3rèr8r9rr–rrr rr!r"rÅr2r9rCÚplatformZ_scproxyrErFrGrHrr5rRrTrLrLrLrMÚDsìP   T ?n$q*@ o  v  +3:5! AW  _ #<    - 2