mò %U²Ic@sAdZdkZdkZdkZdkZdkZdkZdkZdkZdk Z dk Z dk Z dk Z dk Z dkZdkZdkZdkZdkZdkZydklZWn ej odklZnXdklZlZlZlZlZlZlZlZl Z l!Z!l"Z"l#Z#l$Z$l%Z%dkl&Z&l'Z'l(Z(dZ)da+dd„Z,d„Z-de.fd „ƒYZ/d e/efd „ƒYZ0d e/fd „ƒYZ1dfd„ƒYZ2dfd„ƒYZ3d„Z4dfd„ƒYZ5de5fd„ƒYZ6de5fd„ƒYZ7de5fd„ƒYZ8de5fd„ƒYZ9dfd„ƒYZ:de5fd „ƒYZ;d!fd"„ƒYZ<d#e<fd$„ƒYZ=d%fd&„ƒYZ>d'e>e5fd(„ƒYZ?d)e>e5fd*„ƒYZ@d+„ZAd,fd-„ƒYZBd.e5eBfd/„ƒYZCd0e5eBfd1„ƒYZDd2e5fd3„ƒYZEd4eEfd5„ƒYZFeGed6ƒod7eEfd8„ƒYZHnd9e5fd:„ƒYZId;e5fd<„ƒYZJd=„ZKd>„ZLd?e5fd@„ƒYZMdAe5fdB„ƒYZNdCeNfdD„ƒYZOdEe5fdF„ƒYZPdGfdH„ƒYZQdS(Is 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 that 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 IOError); 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 -- Request -- an object that encapsulates the state of a request. the state can be a simple as the URL. it can also include extra HTTP headers, e.g. a User-Agent. BaseHandler -- exceptions: URLError-- a subclass of IOError, individual protocols have their own specific subclass HTTPError-- also a valid HTTP response, so you can treat an HTTP error as an exceptional event or valid response internals: BaseHandler and parent _call_chain conventions Example usage: import urllib2 # set up authentication info authinfo = urllib2.HTTPBasicAuthHandler() authinfo.add_password('realm', 'host', 'username', 'password') proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"}) # build a new opener that adds authentication and caching FTP handlers opener = urllib2.build_opener(proxy_support, authinfo, urllib2.CacheFTPHandler) # install it urllib2.install_opener(opener) f = urllib2.urlopen('http://www.python.org/') N(sStringIO(sunwrapsunquotes splittypes splithosts addinfourls splitportssplitgophertypes splitquerys splitattrs ftpwrappers noheaderss splitusers splitpasswds splitvalue(s localhosts url2pathnames getproxiess2.4cCs*tdjo tƒanti||ƒS(N(t_openertNonet build_openertopenturltdata(RR((t$/data/zmath/lib/python2.4/urllib2.pyturlopen~s  cCs |adS(N(topenerR(R((Rtinstall_opener„stURLErrorcBstZd„Zd„ZRS(NcCs|f|_||_dS(N(treasontselftargs(R R ((Rt__init__’s cCs d|iS(Ns(R R (R ((Rt__str__–s(t__name__t __module__RR(((RR Œs t HTTPErrorcBs)tZdZeiZd„Zd„ZRS(sBRaised when HTTP error occurs, but also acts like non-error returncCsU||_||_||_||_||_|dj o|i|||ƒndS(N( tcodeR tmsgthdrstfpRtfilenameRt_HTTPError__super_init(R RRRRR((RR�s      cCsd|i|ifS(NsHTTP Error %s: %s(R RR(R ((RRªs(RRt__doc__t addinfourlRRR(((RR™s   t GopherErrorcBstZRS(N(RR(((RR­stRequestcBs¹tZdhded„Zd„Zd„Zd„Zd„Zd„Z d„Z d„Z d „Z d „Z d „Zd „Zd „Zd„Zd„Zd„Zdd„Zd„ZRS(NcCs¨t|ƒ|_d|_d|_d|_||_h|_ x*|i ƒD]\}}|i ||ƒqIWh|_|djoti|ƒ}n||_||_dS(N(tunwrapRR t_Request__originalRttypethosttportRtheaderstitemstkeytvaluet add_headertunredirected_hdrstorigin_req_hostt cookielibt request_hostt unverifiable(R RRR"R(R+R%R$((RR³s         cCse|d djoG|d}ttd|ƒo%t|d|ƒƒt||ƒSqXnt|‚dS(Ni t _Request__r_tget_(tattrtnamethasattrRtgetattrR tAttributeError(R R.R/((Rt __getattr__Ås  cCs|iƒodSndSdS(NtPOSTtGET(R thas_data(R ((Rt get_methodÑs cCs ||_dS(N(RR (R R((Rtadd_dataÙscCs |idj S(N(R RR(R ((RR6ÜscCs|iS(N(R R(R ((Rtget_dataßscCs|iS(N(R R(R ((Rt get_full_urlâscCsZ|idjoCt|iƒ\|_|_|idjotd|i‚qSn|iS(Nsunknown url type: %s(R RRt splittypeRt_Request__r_typet ValueError(R ((Rtget_typeås cCsV|idjo?t|iƒ\|_|_|iot|iƒ|_qOn|iS(N(R R Rt splithostR<t_Request__r_hosttunquote(R ((Rtget_hostìs  cCs|iS(N(R R@(R ((Rt get_selectoróscCs#|||_|_|i|_dS(N(R RR RR@(R R R((Rt set_proxyöscCs|iS(N(R R((R ((Rtget_origin_req_hostúscCs|iS(N(R R+(R ((Rtis_unverifiableýscCs||i|iƒRBRCRDRERFR&RIRKRNRQ(((RR±s$               tOpenerDirectorcBsMtZd„Zd„Zd„Zd„Zdd„Zdd„Zd„Z RS(NcCsMdt}d|fg|_g|_h|_h|_h|_h|_dS(NsPython-urllib/%ss User-agent( t __version__tserver_versionR t addheadersthandlerst handle_opent handle_errortprocess_responsetprocess_request(R RU((RRs     c Cs‘t}xYt|ƒD]K} | idƒ}| | } | |d}|i dƒoq|idƒ|d}| |d}yt |ƒ}Wnt j onX|ii| hƒ}||i| |D]6}t||ƒ}||Œ}|dj o|SqqWdS(N( tchainRLRlRWRdR1t meth_nametfuncR tresultR( R RwRlRxR RWRdRzRy((Rt _call_chainJs  c Csöt|tƒot||ƒ}n%|}|dj o|i|ƒn|iƒ}|d}x8|i i |gƒD]!}t||ƒ}||ƒ}qsW|i||ƒ}|d}x;|ii |gƒD]$}t||ƒ}|||ƒ}qÊW|S(Nt_requestt _response(t isinstancetfullurlt basestringRRtreqRR8R>RhRxR R[RLt processorR1Ret_openR_RZ( R RRR‚RhR�RxR_Re((RRUs$    cCs}|i|idd|ƒ}|o|Sn|iƒ}|i|i||d|ƒ}|o|Sn|i|idd|ƒS(NRMt default_openRƒtunknownt unknown_open(R R{RXR�RzR>Rh(R R�RRhRz((RRƒps   cGs½|ddgjo1|id}|d}d|}d}|}n|i}|d}d}|||f|}|i|Œ}|o|Sn|o$|dd f|}|i|ŒSndS( Nthttpthttpsis http_error_%sit_erroriRMthttp_error_default( tprotoR RYtdictR Rxthttp_errt orig_argsR{Rz(R R‹R RŽRxR�RzRŒ((RR]s       ( RRRRuRvR{RRRƒR](((RRSs %   cGsDtƒ}tttttttt g}t t dƒo|i tƒng}xu|D]m}xd|D]\}ti|ƒo%t||ƒo|i |ƒq¾qbt||ƒo|i |ƒqbqbWqUWx|D]}|i|ƒqÍWx|D]}|i|ƒƒqëWx8|D]0}ti|ƒo |ƒ}n|i|ƒq W|S(sCreate an opener object from a list of handlers. The opener will use several default handlers, including support for HTTP and FTP. If any of the handlers passed as arguments are subclasses of the default handlers, the default handlers will not be used. tHTTPSN(RSRt ProxyHandlertUnknownHandlert HTTPHandlertHTTPDefaultErrorHandlertHTTPRedirectHandlert FTPHandlert FileHandlertHTTPErrorProcessortdefault_classesR0thttplibRrt HTTPSHandlertskiptklassRWtchecktinspecttisclasst issubclassR~tremoveRuth(RWR›R˜R�RR¢Rœ((RR˜s6  t BaseHandlercBs)tZdZd„Zd„Zd„ZRS(NiôcCs ||_dS(N(tparentR (R R¤((RRt¿scCsdS(N((R ((RRvÂscCs(t|dƒptSn|i|ijS(Nt handler_order(R0totherRsR R¥(R R¦((Rt__lt__Æs(RRR¥RtRvR§(((RR£¼s  R—cBs#tZdZdZd„ZeZRS(sProcess HTTP error responses.iècCsV|i|i|iƒ}}}|djo%|iid|||||ƒ}n|S(NiÈiÎR‡(iÈiÎ( R_RRtinfoRR R¤R]R`(R R`R_RRR((Rt http_responseÓs  %(RRRR¥R©thttps_response(((RR—Ïs  R“cBstZd„ZRS(NcCs"t|iƒ||||ƒ‚dS(N(RR�R:RRRR(R R�RRRR((RRŠßs(RRRŠ(((RR“ÞsR”cBs:tZdZdZd„Zd„ZeZZZdZ RS(Nii cCs‹|iƒ}|d jo |d jp|djo6|djo)t|d|id |iƒd tƒSnt |i ƒ||||ƒ‚d S(s­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/i3R5tHEADR4R"R(R+N(i-i.i/i3(sGETR«(i-i.i/( R�R7tmRRtnewurlR"RERsRR:RR(R R�RRRR"R­R¬((Rtredirect_requestês  4  c Cshd|jo|idƒd}n)d|jo|idƒd}ndSti|iƒ|ƒ}|i||||||ƒ}|djodSnt|dƒoo|i}|_|i|dƒ|ijpt|ƒ|ijo)t|iƒ||i|||ƒ‚q*nh}|_|_|i|dƒd||<|iƒ|iƒ|ii|ƒS(Ntlocationiturit redirect_dicti(R"t getheadersR­turlparseturljoinR�R:R R®RRRtnewRR0R±tvisitedRLt max_repeatstlentmax_redirectionsRtinf_msgtreadRvR¤R( R R�RRRR"R­R¶Rµ((Rthttp_error_302s&   2  soThe HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: ( RRR·R¹R®R¼thttp_error_301thttp_error_303thttp_error_307Rº(((RR”âs   %R�cBs#tZdZdd„Zd„ZRS(NidcCs�|djo tƒ}nt|dƒp td‚||_x=|iƒD]/\}}t |d||||i d„ƒqJWdS(Nthas_keysproxies must be a mappings%s_opencCs||||ƒS(N(RetrtproxyR(RÁRÂRRe((Rt=s( tproxiesRt getproxiesR0tAssertionErrorR R#RRtsetattrt proxy_open(R RÄRR((RR6s     c Cs|iƒ}t|ƒ\}}t|ƒ\}}d|jo„|i ddƒ\}}d|jo[|i ddƒ\} } tidt| ƒt| ƒfƒiƒ}|idd|ƒqÁnt|ƒ}|i||ƒ||jodSn|ii|ƒSdS(Nt@it:s%s:%ssProxy-authorizationsBasic (R�R>t orig_typeR;RÂRtr_typeR?R tXXXtsplitt user_passtusertpasswordtbase64t encodestringRAtstripR&RDRR R¤R( R R�RÂRRÍRËRÌR RÏRÑRÐ((RRÈ@s   +  (RRR¥RRRÈ(((RR�2s t CustomProxycBs)tZddd„Zd„Zd„ZRS(NcCs||_||_||_dS(N(R‹R Ryt proxy_addrtaddr(R R‹RyRÖ((RR[s  cCs&|io|i|ƒodSndS(Ni(R RyR�(R R�((Rthandle`scCs|iS(N(R R×(R ((Rt get_proxyds(RRRRRØRÙ(((RRÕYs tCustomProxyHandlercBs2tZdZd„Zd„Zd„Zd„ZRS(NidcGs h|_dS(N(R RÄ(R RÄ((RRkscCsƒ|iƒ}y|i|}Wntj o dSnXxE|D]=}|i|ƒo'|i |i ƒƒ|i i |ƒSq>q>WdS(N( R�R>R‹R RÄtKeyErrorRtpRØRDRÙR¤R(R R�R‹RÜRÄ((RRÈns  cCs|ii|ƒS(N(R R¤RR�(R RÜR�((Rtdo_proxyzscCsE|i|ijo|i|ii|ƒn|g|i|iusername="%s", realm="%s", nonce="%s", uri="%s", response="%s"s , opaque="%s"s , digest="%s"s, algorithm="%s"s, qop=auth, nc=%s, cnonce="%s"("R(RåR-RLR3R4RR6RÛR tget_algorithm_implstHtKDRáRíR�R:RÐRR6tget_entity_digestR9tentdigtA1R7RCtA2RtncvalueR2tcnoncetnoncebittrespdigRð(R R�R(R3R;RåRR6RAR<R=R@R-R8R?R>RðRÐR4R9((RR)@sL    !     ) csJ|djo d„‰n|djo d„‰n‡d†}ˆ|fS(NR5cCsti|ƒiƒS(N(tmd5RµtxR0(RC((RRÃ|stSHAcCsti|ƒiƒS(N(R,RµRCR0(RC((RRÃ~scsˆd||fƒS(Ns%s:%s(R8Rtd(RRE(R8(RRÀs(R4R8R9(R R4R8R9((R8RR7ys      cCsdS(N(R(R RR(((RR:ƒs( RRRRR RR"R2R)R7R:(((RRs    9 tHTTPDigestAuthHandlercBstZdZdZd„ZRS(s¨An authentication protocol defined by RFC 2069 Digest authentication improves on basic authentication because it does not transmit passwords in the clear. R cCs?ti|iƒƒd}|id|||ƒ}|iƒ|S(Niswww-authenticate( R³R�R:R R RR"tretryR (R R�RRRR"R RG((RR ‘s   (RRRRR (((RRFˆs tProxyDigestAuthHandlercBstZdZd„ZRS(NsProxy-AuthorizationcCs2|iƒ}|id|||ƒ}|iƒ|S(Nsproxy-authenticate(R�RBR R RR"RGR (R R�RRRR"R RG((RR �s    (RRRR (((RRH™stAbstractHTTPHandlercBs/tZdd„Zd„Zd„Zd„ZRS(NicCs ||_dS(N(t debuglevelR t _debuglevel(R RJ((RR¦scCs ||_dS(N(tlevelR RK(R RL((Rtset_http_debuglevel©sc Cs8|iƒ}|ptdƒ‚n|iƒob|iƒ}|idƒp|iddƒn|idƒp|iddt |ƒƒq’nt |i ƒƒ\}}t|ƒ\} }|idƒp|id| p|ƒnxJ|iiD]<\}}|iƒ}|i|ƒp|i||ƒqôqôW|S(Ns no host givens Content-types!application/x-www-form-urlencodedsContent-lengths%dtHost(R`RBR R R6R9RRKRIR¸R;RCRþtselR?tsel_hosttsel_pathR R¤RVR/R%RH( R R`ROR/R R%RQRþRRP((Rt do_request_¬s(     "  c Cs|iƒ}|ptdƒ‚n||ƒ}|i|iƒt |i ƒ}|i |i ƒd|dRR (R R�R((RR†s (RRR†(((RR‘scCsph}xc|D][}|iddƒ\}}|ddjo"|ddjo|dd!}n|||Parse list of key=value strings where keys are not duplicated.t=iit"iÿÿÿÿN(tparsedtlteltRÎtktv(RkRmRnRlRj((RR&s"cCsg}d}t}}x½|D]µ}|o||7}t}qn|oB|djo t}qn|djo t}n||7}qn|djo|i|ƒd}qn|djo t}n||7}qW|o|i|ƒng}|D]}||i ƒqù~S(spParse 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. Rs\Rit,N( trestpartRRtescapetquoteRtcurRsRrRRÔ(RRtRpRrRRqRs((RR')s8          R–cBs)tZd„ZdZd„Zd„ZRS(NcCs_|iƒ}|d djo1|dd!djod|_|ii|ƒSn|i|ƒSdS(Nis//iRîtftp(R�RCRRR R¤Rtopen_local_file(R R�R((Rt file_openVs  % cCsBtidjo+tidƒtitiƒƒft_ntiS(Nt localhost(R–tnamesRRWt gethostbynamet gethostname(R ((Rt get_names`s+c Csdk}|iƒ} |iƒ}t|ƒ}t i |ƒ}|i }|ii|idtƒ} ti|ƒd}titd|pd|| fƒƒ} | ot| ƒ\} }n| p$| o=ti| ƒ|iƒjo!t t!|dƒ| d|ƒSnt"dƒ‚dS(Ntusegmtis6Content-type: %s Content-length: %d Last-modified: %s s text/plaintrbsfile:sfile not on local host(#t email.UtilstemailR�RBR RCtfilet url2pathnamet localfileRtstattstatstst_sizetsizetUtilst formatdatetst_mtimeRstmodifiedt mimetypest guess_typetmtypet mimetoolstMessagetStringIOR"t splitportR!RWRzR R|RRR ( R R�RŽR�R!R‡R…R€RƒR R‹R"((RRvgs     ),(RRRwRRyR|Rv(((RR–Ts  R•cBstZd„Zd„ZRS(NcCsÓ|iƒ} | p td‚nt| ƒ\} }|djo ti}n t |ƒ}t | ƒ\} } | ot | ƒ\} } nd} t| ƒ} t| pdƒ} t| pdƒ} yti| ƒ} Wn$tij o} t| ƒ‚nXt|iƒƒ\}}|idƒ}tt|ƒ}|d |d}}|o|d o|d}ny3|i| | | ||ƒ} |odpd }xP|D]H}t!|ƒ\}}|i#ƒd jo|djo|i$ƒ}qžqžW| i%||ƒ\}}d}t)i*|i+ƒƒd}|o|d|7}n|dj o|djo|d|7}nt-|ƒ}t/i0|ƒ}t1|||i+ƒƒSWn4ti2j o%} td| ft3i4ƒd‚nXdS(Ns ftp errors no host givenRRîiÿÿÿÿiiRtDRtatARgREsContent-type: %s sContent-length: %d i(s ftp errors no host given(R”R•RgRRER“(5R�RBR tIOErrorR’R!RtftplibtFTP_PORTRmt splituserRÐt splitpasswdRáRARWRzR]RR t splitattrRCRtattrsRÎtdirsRãR�R t connect_ftptfwRR.t splitvalueR%RÿtuppertretrfileRtretrlenR"RŒR�R:RŽR‘tsfR�R�Rt all_errorstsystexc_info(R R�RRŽRœR�R!RR�RŸRáRR RÐRR£R.R%R"R¤((Rtftp_open|sX        cCst|||||ƒ}|S(N(t ftpwrapperRÐRáR R!R�RŸ(R RÐRáR R!R�RŸ((RRž¯s(RRR¨Rž(((RR•{s 3tCacheFTPHandlercBs5tZd„Zd„Zd„Zd„Zd„ZRS(NcCs1h|_h|_d|_d|_d|_dS(Nii<i(R tcachettimeouttsoonesttdelayt max_conns(R ((RR·s     cCs ||_dS(N(ttR R®(R R°((Rt setTimeout¾scCs ||_dS(N(R¬R R¯(R R¬((Rt setMaxConnsÁscCs—|||di|ƒf}||ijotiƒ|i |i |