,bc@sdZddlZddlZddlZddlZddlZddlZddlZddlm Z dddddd d d d d ddddddddddddddddddgZ d Z d!Z ejd"krdd#lmZmZn:ejd$kr dd#lmZmZnd%Zd&Zeaeeed'Zeeeed(Zd)ZyddlZWn eZnXeZd*efd+YZiZdfd,YZdefd-YZ ea!d.Z"ea#d/Z$ea%d0Z&ea'd1Z(d2fd3YZ)d4fd5YZ*d6e*fd7YZ+d8e*fd9YZ,d:e*fd;YZ-ye.Wne/k rd<Z0n Xd=Z0d>Z1d?Z2ea3d@Z4ea5dAZ6ea7dBZ8ea9dCZ:ea;dDZ<ea=ddEZ>ea?dFZ@eaAdGZBdHZCeaDdIZEdJZFeGdKeFDZHejIdLZJdMZKdNZLdOZMiZNxfeOePdPeQeRePdPD]=\ZSZTeSdQkoeTeMkreTn dRjUeSeNeTR&topen_unknown_proxyt open_unknowntgetattrtsocketterrorR3tsystexc_info(R6tfullurlR)R,theaderstfpturltypeR(tproxyt proxyhostthosttselectortnametmsg((s/usr/lib/python2.7/urllib.pyR's:     cCs(t|\}}tdd|fdS(s/Overridable interface to open unknown URL type.s url errorsunknown url typeN(RR3(R6RaR)RXR(((s/usr/lib/python2.7/urllib.pyR[scCs,t|\}}tdd||fdS(s/Overridable interface to open unknown URL type.s url errorsinvalid proxy for %sN(RR3(R6ReRaR)RXR(((s/usr/lib/python2.7/urllib.pyRZscCstt|}|jr5||jkr5|j|St|\}}|dkr| sf|dkryC|j|}|j}|jtt |d|fSWqt k rqXn|j ||}z|j} |rt |d} nddl } t|\} } t | p#d\} } t | p;d\} } t| pSd\} } tjj| d}| j|\}}|jj|tj|d} z|| f}|jdk r||j|R\R%thttp_error_default( R6R(RcRRRbR)RitmethodR}((s/usr/lib/python2.7/urllib.pyRss  cCs#|jtd|||fdS(s>Default error handler: close the connection and raise IOError.s http errorN(RIR3(R6R(RcRRRb((s/usr/lib/python2.7/urllib.pyRs c CsFddl}d}d}t|trit|\}}|r`t|\}}t|}n|}n|\}}t|\}}t|\} } | }d}| jdkrd}nIt| \}} |rt|\}}n|r d| || f}n|st dn|rEt|}t j |j } nd} |rut|}t j |j } nd} |j |dd|jd |jd |j} |dk r| jd || jd d | jddt|n| jd|| r| jdd| n| r;| jdd| n|rT| jd|nx|jD]}| j|q^W| j|| j\}}}| j}|dkr|r|jnt dnd|kodknrt||d||S|dkr&|j|||||S|j||||||SdS(sUse HTTPS protocol.iNthttpss %s://%s%ss https errors no host giveniR;R<R#Rs Content-Types!application/x-www-form-urlencodedsContent-Lengths%dRsProxy-AuthorizationsBasic %sRRshttp protocol errorsgot a bad status lineii,shttps:(s https errors no host given(shttp protocol errorisgot a bad status lineN(RR%RRRRRRRR3RRRtHTTPSR;R<R#RRRuRBRRRRIRWR(R6R(R)RRRRgRhRRdRRRRRNRRRbRc((s/usr/lib/python2.7/urllib.pyt open_httpss~                  cCsut|tstd n|d dkrd|dd!dkrd|dd!jdkrd|j|S|j|Sd S( s/Use local file or FTP depending on form of URL.s file errors9proxy support for file protocol currently not implementedis//it/i s localhost/N(s file errors9proxy support for file protocol currently not implemented(RRR3Rtopen_ftpRU(R6R(((s/usr/lib/python2.7/urllib.pyt open_files  < cCsddl}ddl}ddl}yddlm}Wn!tk r[ddlm}nXt|\}}t|}ytj |} Wn.t k r} t | j | j | jnX| j} |jj| jdt} |j|d} |j|d| p d| | f}|s|}|d d krFd |}n#|d d kritd |ntt|d||St|\}}| rtj|ttfkr|}|d d krd |}ntt|d||St ddS(sUse local file.iN(tStringIOtusegmtis6Content-Type: %s Content-Length: %d Last-modified: %s s text/plainiRsfile://is./sAlocal file url may start with / or file:. Unknown url of type: %sRQslocal file errorsnot on local host(slocal file errorsnot on local host(t mimetypest mimetoolst email.utilst cStringIORt ImportErrorRR RDtstatRKR3terrnotstrerrorR,tst_sizetutilst formatdatetst_mtimetTruet guess_typetMessaget ValueErrorRWR'RR]t gethostbynameRR(R6R(RRtemailRRgRLt localnametstatsteRtmodifiedtmtypeRbturlfiletport((s/usr/lib/python2.7/urllib.pyRUsD$      !  cCst|tstdnddl}ddl}yddlm}Wn!tk rjddlm}nXt|\}}|stdnt |\}}t |\}}|rt |\}} nd} t |}|pd}| pd} tj|}|s%ddl} | j}n t|}t|\}} t |}|jd} | d | d} } | r| d  r| d } n| r| d  rd| d cBseZdZdZdZddZdZddZddZ ddZ ddZ dd Z dd Z dd Zdd Zdd ZddZdZRS(s?Derived class with handlers for errors we can handle (perhaps).cOs2tj|||i|_d|_d|_dS(Nii (RR4t auth_cachettriestmaxtries(R6RNtkwargs((s/usr/lib/python2.7/urllib.pyR4ns  cCst||d||S(s3Default error handling -- don't raise an exception.shttp:(RW(R6R(RcRRRb((s/usr/lib/python2.7/urllib.pyRtsc Cs|jd7_z{|jrg|j|jkrgt|drH|j}n |j}|||dd|S|j||||||}|SWdd|_XdS(s%Error 302 -- relocated (temporarily).ithttp_error_500is)Internal Server Error: Redirect RecursionNi(RRR>RRtredirect_internal( R6R(RcRRRbR)tmethR}((s/usr/lib/python2.7/urllib.pythttp_error_302xs    c Csd|kr|d}nd|kr2|d}ndS|jt|jd||}|j}|jdp|jdp|jdstd||d||n|j|S( Ntlocationturit:shttp://shttps://sftp://sredirect errors) - Redirection to url '%s' is not allowed(RIRRXRt startswithR3R'( R6R(RcRRRbR)tnewurlt newurl_lower((s/usr/lib/python2.7/urllib.pyRs         cCs|j||||||S(s*Error 301 -- also relocated (permanently).(R(R6R(RcRRRbR)((s/usr/lib/python2.7/urllib.pythttp_error_301scCs|j||||||S(s;Error 303 -- also relocated (essentially identical to 302).(R(R6R(RcRRRbR)((s/usr/lib/python2.7/urllib.pythttp_error_303scCsE|dkr(|j||||||S|j|||||SdS(s1Error 307 -- relocated, but turn POST into error.N(R%RR(R6R(RcRRRbR)((s/usr/lib/python2.7/urllib.pythttp_error_307s c Cs d|kr+tj||||||n|d}ddl}|jd|} | sxtj||||||n| j\} } | jdkrtj||||||nd|jd} |dkrt|| || St|| || |SdS(s_Error 401 -- authentication required. This function supports Basic authentication only.swww-authenticateiNs![ ]*([^ ]+)[ ]+realm="([^"]*)"tbasictretry_t _basic_auth( RRtretmatchtgroupsRRXR%R\( R6R(RcRRRbR)tstuffRRtschemetrealmRi((s/usr/lib/python2.7/urllib.pythttp_error_401s"    c Cs d|kr+tj||||||n|d}ddl}|jd|} | sxtj||||||n| j\} } | jdkrtj||||||nd|jd} |dkrt|| || St|| || |SdS(seError 407 -- proxy authentication required. This function supports Basic authentication only.sproxy-authenticateiNs![ ]*([^ ]+)[ ]+realm="([^"]*)"Rt retry_proxy_R( RRRRRRRXR%R\( R6R(RcRRRbR)RRRRRRi((s/usr/lib/python2.7/urllib.pythttp_error_407s"    cCs t|\}}d||}|jd}t|\}} t| \} } | jdd} | | } |j| || \} } | p| sdSt| dddt| ddd| } d| | |jd<|dkr|j|S|j||SdS(Nshttp://Rt@iRPRlR(RR"Rtfindtget_user_passwdR%RR'(R6R(RR)RgRhRReRdRft proxyselectorRRR((s/usr/lib/python2.7/urllib.pytretry_proxy_http_basic_auths   .  cCs t|\}}d||}|jd}t|\}} t| \} } | jdd} | | } |j| || \} } | p| sdSt| dddt| ddd| } d| | |jd<|dkr|j|S|j||SdS(Nshttps://RRiRPRlR(RR"RR R R%RR'(R6R(RR)RgRhRReRdRfR RRR((s/usr/lib/python2.7/urllib.pytretry_proxy_https_basic_auths   .  c Cst|\}}|jdd}||}|j|||\}}|pS|sZdSt|dddt|ddd|}d||} |dkr|j| S|j| |SdS(NRiRPRlRshttp://(RR R R%RR'( R6R(RR)RgRhRRRR((s/usr/lib/python2.7/urllib.pytretry_http_basic_auths  .  c Cst|\}}|jdd}||}|j|||\}}|pS|sZdSt|dddt|ddd|}d||} |dkr|j| S|j| |SdS(NRiRPRlRshttps://(RR R R%RR'( R6R(RR)RgRhRRRR((s/usr/lib/python2.7/urllib.pytretry_https_basic_auth s  .  icCs|d|j}||jkrD|r6|j|=qD|j|Sn|j||\}}|sh|r~||f|j|st__iter__tnext(RcRttreadlineR>R<R=R?R@(R6Rc((s/usr/lib/python2.7/urllib.pyR4s  cCs d|jjt||jfS(Ns<%s at %r whose fp = %r>(t __class__R8tidRc(R6((s/usr/lib/python2.7/urllib.pyt__repr__s cCsJd|_d|_d|_d|_|jr=|jjnd|_dS(N(R%RtRAR<R=RcRI(R6((s/usr/lib/python2.7/urllib.pyRIs     (R8R9RR4RDRI(((s/usr/lib/python2.7/urllib.pyR;s  R0cBs eZdZdZdZRS(s*Class to add a close hook to an open file.cGs&tj||||_||_dS(N(R;R4t closehookthookargs(R6RcRERF((s/usr/lib/python2.7/urllib.pyR4s cCsPz;|j}|j}|r:d|_d|_||nWdtj|XdS(N(RERFR%R;RI(R6RERF((s/usr/lib/python2.7/urllib.pyRIs    (R8R9RR4RI(((s/usr/lib/python2.7/urllib.pyR0s taddinfocBs eZdZdZdZRS(s.class to add an info() method to an open file.cCstj||||_dS(N(R;R4Rb(R6RcRb((s/usr/lib/python2.7/urllib.pyR4scCs|jS(N(Rb(R6((s/usr/lib/python2.7/urllib.pyRms(R8R9RR4Rm(((s/usr/lib/python2.7/urllib.pyRGs RWcBs5eZdZddZdZdZdZRS(s9class to add info() and geturl() methods to an open file.cCs/tj||||_||_||_dS(N(R;R4RbR(tcode(R6RcRbR(RH((s/usr/lib/python2.7/urllib.pyR4s  cCs|jS(N(Rb(R6((s/usr/lib/python2.7/urllib.pyRmscCs|jS(N(RH(R6((s/usr/lib/python2.7/urllib.pytgetcodescCs|jS(N(R((R6((s/usr/lib/python2.7/urllib.pytgeturlsN(R8R9RR%R4RmRIRJ(((s/usr/lib/python2.7/urllib.pyRWs    cCsdS(Ni((tx((s/usr/lib/python2.7/urllib.pyt _is_unicodescCs t|tS(N(Rtunicode(RK((s/usr/lib/python2.7/urllib.pyRL!scCsTt|rPy|jd}WqPtk rLtdt|dqPXn|S(stoBytes(u"URL") --> 'URL'.tASCIIsURL s contains non-ASCII characters(RLtencodet UnicodeErrortrepr(R(((s/usr/lib/python2.7/urllib.pyRV$s  cCsi|j}|d dkrB|ddkrB|dd!j}n|d dkre|dj}n|S(s8unwrap('') --> 'type://host/path'.itisURL:(R(R(((s/usr/lib/python2.7/urllib.pyR0s   cCsvtdkr*ddl}|jdantj|}|rl|jd}|j|t|dfSd|fS(s:splittype('type:opaquestring') --> 'type', 'opaquestring'.iNs ^([^/:]+):i(t _typeprogR%RtcompileRtgroupRRu(R(RRR((s/usr/lib/python2.7/urllib.pyR9s  cCstdkr*ddl}|jdantj|}|r|jd}|jd}|r|jd rd|}n||fSd|fS(s;splithost('//host[:port]/path') --> 'host[:port]', '/path'.iNs^//([^/?]*)(.*)$iiR(t _hostprogR%RRURRVR(R(RRt host_portRo((s/usr/lib/python2.7/urllib.pyRGs    cCsYtdkr*ddl}|jdantj|}|rO|jddSd|fS(sJsplituser('user[:passwd]@host[:port]') --> 'user[:passwd]', 'host[:port]'.iNs ^(.*)@(.*)$ii(t _userprogR%RRURRV(RgRR((s/usr/lib/python2.7/urllib.pyRXs  cCs_tdkr0ddl}|jd|jantj|}|rU|jddS|dfS(s/splitpasswd('user:passwd') -> 'user', 'passwd'.iNs^([^:]*):(.*)$ii(t _passwdprogR%RRUtSRRV(RRR((s/usr/lib/python2.7/urllib.pyRds  cCsntdkr*ddl}|jdantj|}|rd|j\}}|rd||fSn|dfS(s*splitport('host:port') --> 'host', 'port'.iNs^(.*):([0-9]*)$(t _portprogR%RRURR(RgRRR((s/usr/lib/python2.7/urllib.pyRqs   cCstdkr*ddl}|jdantj|}|r|jdd\}}|ryt|}Wntk rd}nX||fSn||fS(sSplit host and port, returning numeric port. Return given default port if no ':' found; defaults to -1. Return numerical port if a valid number are found after ':'. Return None if ':' but not a valid number.iNs ^(.*):(.*)$ii(t _nportprogR%RRURRVRsR(RgtdefportRRRtnport((s/usr/lib/python2.7/urllib.pyRs     cCsYtdkr*ddl}|jdantj|}|rO|jddS|dfS(s/splitquery('/path?query') --> '/path', 'query'.iNs^(.*)\?([^?]*)$ii(t _queryprogR%RRURRV(R(RR((s/usr/lib/python2.7/urllib.pyRs  cCsYtdkr*ddl}|jdantj|}|rO|jddS|dfS(s)splittag('/path#tag') --> '/path', 'tag'.iNs^(.*)#([^#]*)$ii(t_tagprogR%RRURRV(R(RR((s/usr/lib/python2.7/urllib.pyR s  cCs!|jd}|d|dfS(sksplitattr('/path;attr1=value1;attr2=value2;...') -> '/path', ['attr1=value1', 'attr2=value2', ...].Rii(R(R(twords((s/usr/lib/python2.7/urllib.pyRscCsYtdkr*ddl}|jdantj|}|rO|jddS|dfS(s-splitvalue('attr=value') --> 'attr', 'value'.iNs^([^=]*)=(.*)$ii(t _valueprogR%RRURRV(RRR((s/usr/lib/python2.7/urllib.pyRs  t0123456789ABCDEFabcdefccs?|]5}tD](}||tt||dfVq qdS(iN(t_hexdigtchrRs(t.0Rtb((s/usr/lib/python2.7/urllib.pys ss([-]+)cCsOt|rd|kr|Stj|}|dg}|j}xUtdt|dD];}|tt||jd|||dqZWdj |S|jd}t|dkr|S|dg}|j}x^|dD]R}y$|t |d ||dWqt k r=|d||qXqWdj |S(s"unquote('abc%20def') -> 'abc def'.t%iiitlatin1Rl( RLt_asciireRRMtrangeRuRRtdecodeRt _hextochrtKeyError(tstbitstresRMRtitem((s/usr/lib/python2.7/urllib.pyRs.    #     cCs|jdd}t|S(s%unquote('%7e/abc+def') -> '~/abc def't+t (RYR(Rp((s/usr/lib/python2.7/urllib.pyR ssAABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-iis%{:02X}RcCs|s%|dkr!tdn|S|tf}yt|\}}Wnftk rtj}|jg|D]}||f^qn|j}t|}||ft| 'abc%20def' Each part of a URL, e.g. the path info, the query, etc., has a different set of reserved characters that must be quoted. RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists the following reserved characters. reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | "," Each of these characters is reserved in some component of a URL, but not necessarily in all of them. By default, the quote function is intended for quoting the path section of a URL. Thus, it will not encode '/'. This character is reserved, but in typical usage the quote function is being called on a path where the existing slash characters are used as reserved characters. sNone object cannot be quotedRlN( R%t TypeErrort always_safeR/Rot _safe_maptcopytupdatet __getitem__trstripRtmap(RpRPtcachekeytquotertsafe_maptc((s/usr/lib/python2.7/urllib.pyRs     &  RlcCs<d|kr/t||d}|jddSt||S(s9Quote the query fragment of a URL; replacing ' ' with '+'RuRt(RRY(RpRP((s/usr/lib/python2.7/urllib.pyRs ic Cst|dr|j}nby-t|rJt|dt rJtnWn2tk rtj\}}}td|nXg}|sxc|D]E\}}tt |}tt |}|j |d|qWnx|D]\}}tt |}t|t r7t|}|j |d|qt |rst|j dd}|j |d|qyt|Wn8tk rtt |}|j |d|qXx/|D]'}|j |dtt |qWqWdj |S(svEncode a sequence of two-element tuples or dictionary into a URL query string. If any values in the query arg are sequences and doseq is true, each sequence element is converted to a separate parameter. If the query arg is a sequence of two-element tuples, the order of the parameters in the output will match the order of parameters in the input. titemsis1not a valid non-string sequence or mapping objectRRNRYt&(R>RRuRttupleRvR_R`RRRMRLROR( tquerytdoseqttytvattbtlRRtelt((s/usr/lib/python2.7/urllib.pyR s<        )cCsi}xPtjjD]?\}}|j}|r|ddkr|||d proxy server URL mappings. Scan the environment for variables named _proxy; this seems to be the standard convention. In order to prefer lowercase variables, we process the environment in two passes, first matches any and second matches only lower case proxies. If you need a different way, you can pass a proxies dictionary to the [Fancy]URLopener constructor. it_proxytREQUEST_METHODRN(RDtenvironRRtpopR%(R"RiR((s/usr/lib/python2.7/urllib.pytgetproxies_environment_s   c Cs|dkrt}ny|d}Wntk r:dSX|dkrKdSt|\}}g|jdD]}|j^qm}xd|D]\}|rtj|}d|}tj||tj stj||tj rdSqqWdS(sTest if proxies should not be used for a particular host. Checks the proxies dict for the value of no_proxy, which should be a list of comma separated DNS suffixes, or '*' for all hosts. tnoit*iRs (.+\.)?%s$N( R%RRoRRRRtescapeRR( RgR"tno_proxythostonlyRRet no_proxy_listRitpattern((s/usr/lib/python2.7/urllib.pytproxy_bypass_environments$    (   tdarwin(t_get_proxy_settingst _get_proxiesc Csddl}ddl}ddlm}t|\}}d}t}d|kri|dritSnd}x'|jdd D]} | sqn|jd| } | dk r|dkry|j |}||}Wq|j k rqqXn|| j d } | j d } | dkrMd | j d j dd } nt | d } d | } || ?| | ?krtSq||| rtSqWtS(s Return True iff this host shouldn't be accessed using a proxy This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. iN(tfnmatchcSsy|jd}tt|}t|dkrM|ddddgd }n|dd>|dd>B|dd>B|d BS( Nt.iiiiiiii(RR}RsRu(tipAddrtparts((s/usr/lib/python2.7/urllib.pytip2nums Rtexclude_simplet exceptionss(\d+(?:\.\d+)*)(/\d+)?iiii ((RR]RRRRR%R@RRR^RVtcountRsR8( RgRR]RRRRtproxy_settingsthostIPRtmtbasetmask((s/usr/lib/python2.7/urllib.pytproxy_bypass_macosx_sysconfs@          # cCstS(sReturn a dictionary of scheme -> proxy server URL mappings. This function uses the MacOSX framework SystemConfiguration to fetch the proxy information. (R(((s/usr/lib/python2.7/urllib.pytgetproxies_macosx_sysconfscCs*t}|rt||St|SdS(sReturn True, if a host should be bypassed. Checks proxy settings gathered from the environment, if specified, or from the MacOSX framework SystemConfiguration. N(RRR(RgR"((s/usr/lib/python2.7/urllib.pyRs  cCstptS(N(RR(((s/usr/lib/python2.7/urllib.pyRsc Csmi}yddl}Wntk r*|SXy!|j|jd}|j|dd}|rAt|j|dd}d|krx|jdD]Y}|jdd \}}ddl}|jd |sd ||f}n||| proxy server URL mappings. Win32 uses the registry to store proxies. iNs;Software\Microsoft\Windows\CurrentVersion\Internet Settingst ProxyEnableit ProxyServerRRis ^([^/:]+)://s%s://%sishttp:Rs http://%ss https://%sRsftp://%sR$( t_winregRtOpenKeytHKEY_CURRENT_USERt QueryValueExRRRRtCloset WindowsErrorRRv( R"RtinternetSettingst proxyEnablet proxyServertptprotocoltaddressR((s/usr/lib/python2.7/urllib.pytgetproxies_registrys:         cCstptS(sReturn a dictionary of scheme -> proxy server URL mappings. Returns settings gathered from the environment, if specified, or the registry. (RR(((s/usr/lib/python2.7/urllib.pyR&sc Csyddl}ddl}Wntk r0dSXyK|j|jd}|j|dd}t|j|dd}Wntk rdSX| s| rdSt|\}}|g}y/t j |}||kr|j |nWnt j k rnXy/t j |} | |kr2|j | nWnt j k rInX|jd}x|D]} | dkrd|krd Sn| jdd } | jd d } | jd d} x*|D]"} |j| | |jrd SqWq`WdS(Niis;Software\Microsoft\Windows\CurrentVersion\Internet SettingsRt ProxyOverrideRsRis\.Rs.*t?(RRRRRRRRRR]RRMR^tgetfqdnRRYRR( RgRRRRt proxyOverridetrawHostRtaddrtfqdnttesttval((s/usr/lib/python2.7/urllib.pytproxy_bypass_registry/sT                cCs*t}|rt||St|SdS(sReturn True, if the host should be bypassed. Checks proxy settings gathered from the environment, if specified, or the registry. N(RRR(RgR"((s/usr/lib/python2.7/urllib.pyRcs  cCsd}x$tdD]}|t|}qW|d}tj}t|}t|}tj}||kr{dGHnt|GHt|GHt|GHt||dGdGHdS(NRliisWrong!itsec(RlRfRRRRQtround(RpRtt0tqstuqstt1((s/usr/lib/python2.7/urllib.pyttest1us         cCsd|||fGHdS(Ns0Block number: %d, Block size: %d, Total size: %d((Rt blocksizet totalsize((s/usr/lib/python2.7/urllib.pyR-s(hRtstringR]RDRR_RRturlparseRRt__all__RRRit nturl2pathR R t rourl2pathR%R&RRRtsslR8RRR3R2R1RRRRRRRRRRRR;R0RGRWRMt NameErrorRLRVRRTRRWRRYRRZRR\RR]RR`RRaR RRcRRetdictRnRURkRR RwRxtziptxrangeRt bytearrayRRtformatR/RRR RRtplatformt_scproxyRRRRRRRRRR-(((s/usr/lib/python2.7/urllib.pyts                   ] "                 45 '  @ $  ;   . 4