m %UIc@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+ddZ,dZ-de.fd YZ/d e/efd YZ0d e/fd YZ1dfdYZ2dfdYZ3dZ4dfdYZ5de5fdYZ6de5fdYZ7de5fdYZ8de5fdYZ9dfdYZ: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/YZCd0e5eBfd1YZDd2e5fd3YZEd4eEfd5YZFeGed6od7eEfd8YZHnd9e5fd:YZId;e5fd<YZJd=ZKd>ZLd?e5fd@YZMdAe5fdBYZNdCeNfdDYZOdEe5fdFYZPdGfdHYZQdS(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 tanti||S(N(t_openertNonet build_openertopenturltdata(RR((t$/data/zmath/lib/python2.4/urllib2.pyturlopen~s  cCs |adS(N(topenerR(R((Rtinstall_openerstURLErrorcBstZdZdZRS(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)tZdZeiZdZdZRS(sBRaised when HTTP error occurs, but also acts like non-error returncCsU||_||_||_||_||_|dj o|i|||ndS(N( tcodeR tmsgthdrstfpRtfilenameRt_HTTPError__super_init(R RRRRR((RRs      cCsd|i|ifS(NsHTTP Error %s: %s(R RR(R ((RRs(RRt__doc__t addinfourlRRR(((RRs   t GopherErrorcBstZRS(N(RR(((RRstRequestcBstZdhdedZdZdZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZddZdZRS(NcCst||_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$((RRs         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|iodSndSdS(NtPOSTtGET(R thas_data(R ((Rt get_methods cCs ||_dS(N(RR (R R((Rtadd_datascCs |idj S(N(R RR(R ((RR6scCs|iS(N(R R(R ((Rtget_datascCs|iS(N(R R(R ((Rt get_full_urlscCsZ|idjoCt|i\|_|_|idjotd|iqSn|iS(Nsunknown url type: %s(R RRt splittypeRt_Request__r_typet ValueError(R ((Rtget_types cCsV|idjo?t|i\|_|_|iot|i|_qOn|iS(N(R R Rt splithostR<t_Request__r_hosttunquote(R ((Rtget_hosts  cCs|iS(N(R R@(R ((Rt get_selectorscCs#|||_|_|i|_dS(N(R RR RR@(R R R((Rt set_proxyscCs|iS(N(R R((R ((Rtget_origin_req_hostscCs|iS(N(R R+(R ((Rtis_unverifiablescCs||i|iRBRCRDRERFR&RIRKRNRQ(((RRs$               tOpenerDirectorcBsMtZdZdZdZdZddZddZdZ 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 Cst}xYt|D]K} | id}| | } | |d}|i doq|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 Cst|tot||}n%|}|dj o|i|n|i}|d}x8|i i |gD]!}t||}||}qsW|i||}|d}x;|ii |gD]$}t||}|||}qW|S(Nt_requestt _response(t isinstancetfullurlt basestringRRtreqRR8R>RhRxR R[RLt processorR1Ret_openR_RZ( R RRRRhRRxR_Re((RRUs$    cCs}|i|idd|}|o|Sn|i}|i|i||d|}|o|Sn|i|idd|S(NRMt default_openRtunknownt unknown_open(R R{RXRRzR>Rh(R RRRhRz((RRps   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 RR RRxRRzR((RR]s       ( RRRRuRvR{RRRR](((RRSs %   cGsDt}tttttttt g}t t do|i tng}xu|D]m}xd|D]\}ti|o%t||o|i |qqbt||o|i |qbqbWqUWx|D]}|i|qWx|D]}|i|qWx8|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(RWRRRRRR((RRs6  t BaseHandlercBs)tZdZdZdZdZRS(NicCs ||_dS(N(tparentR (R R((RRtscCsdS(N((R ((RRvscCs(t|dptSn|i|ijS(Nt handler_order(R0totherRsR R(R R((Rt__lt__s(RRRRtRvR(((RRs  RcBs#tZdZdZdZeZRS(sProcess HTTP error responses.icCsV|i|i|i}}}|djo%|iid|||||}n|S(NiiR(ii( R_RRtinfoRR RR]R`(R R`R_RRR((Rt http_responses  %(RRRRRthttps_response(((RRs  RcBstZdZRS(NcCs"t|i||||dS(N(RRR:RRRR(R RRRRR((RRs(RRR(((RRsRcBs:tZdZdZdZdZeZZZdZ RS(Nii cCs|i}|d jo |d jp|djo6|djo)t|d|id |id tSnt |i ||||d S(sReturn 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/( RR7tmRRtnewurlR"RERsRR:RR(R RRRRR"RR((Rtredirect_requests  4  c Cshd|jo|idd}n)d|jo|idd}ndSti|i|}|i||||||}|djodSnt|doo|i}|_|i|d|ijpt||ijo)t|i||i|||q*nh}|_|_|i|dd||<|i|i|ii|S(Ntlocationiturit redirect_dicti(R"t getheadersRturlparseturljoinRR:R RRRRtnewRR0RtvisitedRLt max_repeatstlentmax_redirectionsRtinf_msgtreadRvRR( R RRRRR"RRR((Rthttp_error_302s&   2  soThe HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: ( RRRRRRthttp_error_301thttp_error_303thttp_error_307R(((RRs   %RcBs#tZdZddZdZRS(NidcCs|djo t}nt|dp td||_x=|iD]/\}}t |d||||i dqJWdS(Nthas_keysproxies must be a mappings%s_opencCs||||S(N(RetrtproxyR(RRRRe((Rt=s( tproxiesRt getproxiesR0tAssertionErrorR R#RRtsetattrt proxy_open(R RRR((RR6s     c Cs|i}t|\}}t|\}}d|jo|i dd\}}d|jo[|i dd\} } tidt| t| fi}|idd|qnt|}|i||||jodSn|ii|SdS(Nt@it:s%s:%ssProxy-authorizationsBasic (RR>t orig_typeR;RRtr_typeR?R tXXXtsplitt user_passtusertpasswordtbase64t encodestringRAtstripR&RDRR RR( R RRRRRRR RRR((RR@s   +  (RRRRRR(((RR2s t CustomProxycBs)tZdddZdZdZRS(NcCs||_||_||_dS(N(RR Ryt proxy_addrtaddr(R RRyR((RR[s  cCs&|io|i|odSndS(Ni(R RyR(R R((Rthandle`scCs|iS(N(R R(R ((Rt get_proxyds(RRRRRR(((RRYs tCustomProxyHandlercBs2tZdZdZdZdZdZRS(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( RR>RR RtKeyErrorRtpRRDRRR(R RRRR((RRns  cCs|ii|S(N(R RRR(R RR((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(RR-RLR3R4RR6RR tget_algorithm_implstHtKDRRRR:RRR6tget_entity_digestR9tentdigtA1R7RCtA2RtncvalueR2tcnoncetnoncebittrespdigR(R RR(R3R;RRR6RAR<R=R@R-R8R?R>RRR4R9((RR)@sL    !     ) csJ|djo dn|djo dnd}|fS(NR5cCsti|iS(N(tmd5RtxR0(RC((RR|stSHAcCsti|iS(N(R,RRCR0(RC((RR~scsd||fS(Ns%s:%s(R8Rtd(RRE(R8(RRs(R4R8R9(R R4R8R9((R8RR7ys      cCsdS(N(R(R RR(((RR:s( RRRRR RR"R2R)R7R:(((RRs    9 tHTTPDigestAuthHandlercBstZdZdZdZRS(sAn authentication protocol defined by RFC 2069 Digest authentication improves on basic authentication because it does not transmit passwords in the clear. R cCs?ti|id}|id|||}|i|S(Niswww-authenticate( RRR:R R RR"tretryR (R RRRRR"R RG((RR s   (RRRRR (((RRFs tProxyDigestAuthHandlercBstZdZdZRS(NsProxy-AuthorizationcCs2|i}|id|||}|i|S(Nsproxy-authenticate(RRBR R RR"RGR (R RRRRR"R RG((RR s    (RRRR (((RRHstAbstractHTTPHandlercBs/tZddZdZdZdZRS(NicCs ||_dS(N(t debuglevelR t _debuglevel(R RJ((RRscCs ||_dS(N(tlevelR RK(R RL((Rtset_http_debuglevelsc Cs8|i}|ptdn|iob|i}|idp|iddn|idp|iddt |qnt |i \}}t|\} }|idp|id| p|nxJ|iiD]<\}}|i}|i|p|i||qqW|S(Ns no host givens Content-types!application/x-www-form-urlencodedsContent-lengths%dtHost(R`RBR R R6R9RRKRIRR;RCRtselR?tsel_hosttsel_pathR RRVR/R%RH( R R`ROR/R R%RQRRRP((Rt do_request_s(     "  c Cs|i}|ptdn||}|i|it |i }|i |i d|dRR (R RR((RRs (RRR(((RRscCsph}xc|D][}|idd\}}|ddjo"|ddjo|dd!}n|||Parse list of key=value strings where keys are not duplicated.t=iit"iN(tparsedtlteltRtktv(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          RcBs)tZdZdZdZdZRS(NcCs_|i}|d djo1|dd!djod|_|ii|Sn|i|SdS(Nis//iRtftp(RRCRRR RRtopen_local_file(R RR((Rt file_openVs  % cCsBtidjo+tidtitift_ntiS(Nt localhost(RtnamesRRWt 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| |ijo!t t!|d| d|Snt"ddS(Ntusegmtis6Content-type: %s Content-length: %d Last-modified: %s s text/plaintrbsfile:sfile not on local host(#t email.UtilstemailRRBR RCtfilet url2pathnamet localfileRtstattstatstst_sizetsizetUtilst formatdatetst_mtimeRstmodifiedt mimetypest guess_typetmtypet mimetoolstMessagetStringIOR"t splitportR!RWRzR R|RRR ( R RRRR!RRRRR RR"((RRvgs     ),(RRRwRRyR|Rv(((RRTs  RcBstZdZdZRS(NcCs|i} | p tdnt| \} }|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$}qqW| i%||\}}d}t)i*|i+d}|o|d|7}n|dj o|djo|d|7}nt-|}t/i0|}t1|||i+SWn4ti2j o%} td| ft3i4dnXdS(Ns ftp errors no host givenRRiiiRtDRtatARgREsContent-type: %s sContent-length: %d i(s ftp errors no host given(RRRgRRER(5RRBR tIOErrorRR!RtftplibtFTP_PORTRmt splituserRt splitpasswdRRARWRzR]RR t splitattrRCRtattrsRtdirsRRR t connect_ftptfwRR.t splitvalueR%RtuppertretrfileRtretrlenR"RRR:RRtsfRRRt all_errorstsystexc_info(R RRRRRR!RRRRRR RRRR.R%R"R((Rtftp_open|sX        cCst|||||}|S(N(t ftpwrapperRRR R!RR(R RRR R!RR((RRs(RRRR(((RR{s 3tCacheFTPHandlercBs5tZdZdZdZdZdZRS(NcCs1h|_h|_d|_d|_d|_dS(Nii<i(R tcachettimeouttsoonesttdelayt max_conns(R ((RRs     cCs ||_dS(N(ttR R(R R((Rt setTimeoutscCs ||_dS(N(RR R(R R((Rt setMaxConnsscCs|||di|f}||ijoti|i |i |