
\[\i             (   @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& m' Z' m( Z( d d l) m* Z* m+ Z+ y d d l, Z, Wn e- k
 rd Z. Yn Xd Z. d d	 d
 d d d d d d d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' g  Z/ e j0 d d(  Z1 d a2 d e j3 d) d d* d d+ d d, d Z4 d- d   Z5 g  Z6 d d d d. d$  Z7 d/ d%   Z8 e
 j9 d0 e
 j:  Z; d1 d2   Z< Gd3 d   d  Z= Gd4 d	   d	  Z> d5 d    Z? Gd6 d
   d
  Z@ Gd7 d   d e@  ZA Gd8 d   d e@  ZB Gd9 d   d e@  ZC d: d;   ZD Gd< d   d e@  ZE Gd= d   d  ZF Gd> d   d eF  ZG Gd? d   d  ZH Gd@ d   d eH e@  ZI GdA d   d eH e@  ZJ e jK ZL GdB d   d  ZM GdC d   d e@ eM  ZN GdD d   d e@ eM  ZO GdE dF   dF e@  ZP GdG d   d eP  ZQ eR e jS dH  rbGdI dJ   dJ eP  ZT e/ jU dJ  n  GdK d   d e@  ZV GdL d   d e@  ZW dM dN   ZX dO dP   ZY GdQ d   d e@  ZZ dR dS   Z[ GdT d   d e@  Z\ GdU d   d e\  Z] GdV d   d e@  Z^ dW Z_ e j` dX k r8d dY la mb Zb mc Zc n dZ d"   Zb d[ d!   Zc i  Zd Gd\ d&   d&  Ze Gd] d'   d' ee  Zf d ag d^ d_   Zh d ai d` da   Zj d ak db dc   Zl d am dd de   Zn Gdf dg   dg  Zo dh di   Zp dj dk   Zq dl dm   Zr e js dn k rVd do lt mu Zu mv Zv dp dq   Zw dr ds   Zx dt du   Zy dv d#   Zz nN e j` dX k rdw dx   Z{ dy d#   Zz dz d{   Z| d| du   Zy n ep Zz eq Zy d S)}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AbstractBasicAuthHandlerHTTPBasicAuthHandlerProxyBasicAuthHandlerAbstractDigestAuthHandlerHTTPDigestAuthHandlerProxyDigestAuthHandlerHTTPHandlerFileHandler
FTPHandlerCacheFTPHandlerDataHandlerUnknownHandlerHTTPErrorProcessorurlopeninstall_openerbuild_openerpathname2urlurl2pathname
getproxiesurlretrieve
urlcleanup	URLopenerFancyURLopener   cafilecapath	cadefaultc      	      C   s   | s | s | rl t  s' t d   n  t j d t j d | d |  } t d | d d  } t |  } n" t d  k r t   a } n t } | j |  | |  S)NzSSL support not availableZ	cert_reqsr:   r;   contextcheck_hostnameT)		_have_ssl
ValueErrorsslZ_create_stdlib_contextZCERT_REQUIREDHTTPSHandlerr1   _openeropen)	urldatatimeoutr:   r;   r<   r=   Zhttps_handleropener rI   $/usr/lib/python3.4/urllib/request.pyr/      s    	c             C   s
   |  a  d  S)N)rC   )rH   rI   rI   rJ   r0      s    c             C   s  t  |   \ } } t j t |  |   D} | j   } | d k rb | rb t j j |  | f S| rz t | d  } n( t	 j
 d d  } | j } t j |  |  | | f }	 d }
 d } d } d } d	 | k r t | d
  } n  | r| | |
 |  n  x\ | j |
  } | s!Pn  | t |  7} | j |  | d 7} | r| | |
 |  qqWd QXWd QX| d k r| | k  rt d | | f |	   n  |	 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wbZdeleteFi         r   zcontent-lengthzContent-LengthNz1retrieval incomplete: got only %i out of %i bytesi    )r   
contextlibclosingr/   infoospathnormpathrD   tempfileZNamedTemporaryFilename_url_tempfilesappendintreadlenwriter   )rE   filename
reporthookrF   Zurl_typerT   fpheaderstfpresultbssizer[   blocknumblockrI   rI   rJ   r5      sD    	
"c              C   sW   x4 t  D], }  y t j |   Wq t k
 r2 Yq Xq Wt  d  d   =t rS d  a n  d  S)N)rX   rS   unlinkOSErrorrC   )Z	temp_filerI   rI   rJ   r6      s    	z:\d+$c             C   sY   |  j  } t |  d } | d k r: |  j d d  } n  t j d | d  } | j   S)zReturn request-host, as defined by RFC 2965.

    Variation from RFC: returned value is lowercased, for convenient
    comparison.

    rN    Host)full_urlr   
get_header_cut_port_resublower)requestrE   hostrI   rI   rJ   request_host   s    	rs   c               @   s*  e  Z d  Z d i  d d d d d  Z e d d    Z e j d d    Z e j d d    Z e d	 d
    Z e j d d
    Z e j d d
    Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d d  Z d d    Z d! d"   Z d S)#r   NFc       	      C   s   | |  _  i  |  _ i  |  _ d  |  _ | |  _ d  |  _ x* | j   D] \ } } |  j | |  qC W| d  k r~ t |   } n  | |  _	 | |  _
 | r | |  _ n  d  S)N)rl   ra   unredirected_hdrs_datarF   _tunnel_hostitems
add_headerrs   origin_req_hostunverifiablemethod)	selfrE   rF   ra   ry   rz   r{   keyvaluerI   rI   rJ   __init__   s    								zRequest.__init__c             C   s&   |  j  r d j |  j |  j   S|  j S)Nz{}#{})fragmentformat	_full_url)r|   rI   rI   rJ   rl     s    	zRequest.full_urlc             C   s8   t  |  |  _ t |  j  \ |  _ |  _ |  j   d  S)N)r   r   r   r   _parse)r|   rE   rI   rI   rJ   rl     s    c             C   s   d  |  _  d  |  _ d |  _ d  S)Nrj   )r   r   selector)r|   rI   rI   rJ   rl     s    		c             C   s   |  j  S)N)ru   )r|   rI   rI   rJ   rF   $  s    zRequest.datac             C   s>   | |  j  k r: | |  _  |  j d  r: |  j d  q: n  d  S)NzContent-length)ru   
has_headerremove_header)r|   rF   rI   rI   rJ   rF   (  s    	c             C   s   d  |  _  d  S)N)rF   )r|   rI   rI   rJ   rF   2  s    c             C   sw   t  |  j  \ |  _ } |  j d  k r= t d |  j   n  t |  \ |  _ |  _ |  j rs t |  j  |  _ n  d  S)Nzunknown url type: %r)	r   r   typer@   rl   r   rr   r   r
   )r|   restrI   rI   rJ   r   6  s    	zRequest._parsec             C   s+   |  j  d k	 r d n d } t |  d |  S)z3Return a string indicating the HTTP request method.NPOSTGETr{   )rF   getattr)r|   Zdefault_methodrI   rI   rJ   
get_method>  s    zRequest.get_methodc             C   s   |  j  S)N)rl   )r|   rI   rI   rJ   get_full_urlC  s    zRequest.get_full_urlc             C   sJ   |  j  d k r( |  j r( |  j |  _ n | |  _  |  j |  _ | |  _ d  S)Nhttps)r   rv   rr   rl   r   )r|   rr   r   rI   rI   rJ   	set_proxyF  s
    	zRequest.set_proxyc             C   s   |  j  |  j k S)N)r   rl   )r|   rI   rI   rJ   	has_proxyN  s    zRequest.has_proxyc             C   s   | |  j  | j   <d  S)N)ra   
capitalize)r|   r}   valrI   rI   rJ   rx   Q  s    zRequest.add_headerc             C   s   | |  j  | j   <d  S)N)rt   r   )r|   r}   r   rI   rI   rJ   add_unredirected_headerU  s    zRequest.add_unredirected_headerc             C   s   | |  j  k p | |  j k S)N)ra   rt   )r|   header_namerI   rI   rJ   r   Y  s    zRequest.has_headerc             C   s"   |  j  j | |  j j | |   S)N)ra   getrt   )r|   r   defaultrI   rI   rJ   rm   ]  s    	zRequest.get_headerc             C   s*   |  j  j | d   |  j j | d   d  S)N)ra   poprt   )r|   r   rI   rI   rJ   r   b  s    zRequest.remove_headerc             C   s/   |  j  j   } | j |  j  t | j    S)N)rt   copyupdatera   listrw   )r|   hdrsrI   rI   rJ   header_itemsf  s    zRequest.header_items)__name__
__module____qualname__r   propertyrl   setterdeleterrF   r   r   r   r   r   rx   r   r   rm   r   r   rI   rI   rI   rJ   r      s(   
c               @   sp   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 e j d
 d  Z	 d	 d d  Z
 d d   Z d	 S)r   c             C   sM   d t  } d | f g |  _ g  |  _ i  |  _ i  |  _ i  |  _ i  |  _ d  S)NzPython-urllib/%sz
User-agent)__version__
addheadershandlershandle_openhandle_errorprocess_responseprocess_request)r|   Zclient_versionrI   rI   rJ   r   l  s    
				zOpenerDirector.__init__c             C   s  t  | d  s( t d t |    n  d } xxt |  D]j} | d k rS q; n  | j d  } | d  |  } | | d d   } | j d	  r
| j d  | d } | | d d   } y t |  } Wn t k
 r Yn X|  j j	 | i   }	 |	 |  j | <n] | d
 k r(| } |  j
 }	 n? | d k rF| } |  j }	 n! | d k r; | } |  j }	 n q; |	 j | g   }
 |
 rt j |
 |  n |
 j |  d } q; W| rt j |  j |  | j |   n  d  S)N
add_parentz%expected BaseHandler instance, got %rFredirect_requestdo_open
proxy_open_rN   errorrD   responserq   T)zredirect_requestzdo_openz
proxy_open)hasattr	TypeErrorr   dirfind
startswithrZ   r@   r   r   r   r   r   
setdefaultbisectZinsortrY   r   r   )r|   handlerZaddedmethiprotocolZ	conditionjkindlookupr   rI   rI   rJ   add_handlerw  sJ    
zOpenerDirector.add_handlerc             C   s   d  S)NrI   )r|   rI   rI   rJ   close  s    zOpenerDirector.closec       	      G   sR   | j  | f   } x9 | D]1 } t | |  } | |   } | d  k	 r | Sq Wd  S)N)r   r   )	r|   chainr   	meth_nameargsr   r   funcrc   rI   rI   rJ   _call_chain  s    zOpenerDirector._call_chainNc       
      C   s   t  | t  r! t | |  } n | } | d  k	 r? | | _ n  | | _ | j } | d } x8 |  j j | g   D]! } t | |  } | |  } qq W|  j	 | |  }	 | d } x; |  j
 j | g   D]$ } t | |  } | | |	  }	 q W|	 S)NZ_requestZ	_response)
isinstancestrr   rF   rG   r   r   r   r   _openr   )
r|   fullurlrF   rG   reqr   r   Z	processorr   r   rI   rI   rJ   rD     s"    		

zOpenerDirector.openc             C   sp   |  j  |  j d d |  } | r% | S| j } |  j  |  j | | d |  } | rW | S|  j  |  j d d |  S)Nr   Zdefault_openr   unknownunknown_open)r   r   r   )r|   r   rF   rc   r   rI   rI   rJ   r     s    	zOpenerDirector._openc             G   s   | d
 k r< |  j  d } | d } d | } d } | } n |  j  } | d } d } | | | f | } |  j |   } | r | S| r | d d	 f | } |  j |   Sd  S)Nhttpr      zhttp_error_%srN   Z_errorr   r   http_error_default)zhttpzhttps)r   r   )r|   protor   dictr   Zhttp_errZ	orig_argsrc   rI   rI   rJ   r     s     

		
zOpenerDirector.error)r   r   r   r   r   r   r   socket_GLOBAL_DEFAULT_TIMEOUTrD   r   r   rI   rI   rI   rJ   r   k  s   /c           	   G   sC  t    } t t t t t t t t t	 g	 } t
 t j d  rL | j t  n  t   } xo | D]g } x^ |  D]V } t | t  r t | |  r | j |  q qi t | |  ri | j |  qi qi Wq\ Wx | D] } | j |  q Wx | D] } | j |    q Wx6 |  D]. } t | t  r.|   } n  | 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)r   r   r-   r(   r   r   r*   r)   r.   r,   r   r   clientrY   rB   setr   r   
issubclassaddremover   )r   rH   Zdefault_classesskipklassZcheckhrI   rI   rJ   r1     s0    						c               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	r   i  c             C   s   | |  _  d  S)N)parent)r|   r   rI   rI   rJ   r      s    zBaseHandler.add_parentc             C   s   d  S)NrI   )r|   rI   rI   rJ   r   #  s    zBaseHandler.closec             C   s#   t  | d  s d S|  j | j k  S)Nhandler_orderT)r   r   )r|   otherrI   rI   rJ   __lt__'  s    zBaseHandler.__lt__N)r   r   r   r   r   r   r   rI   rI   rI   rJ   r     s   c               @   s.   e  Z d  Z d Z d Z d d   Z e Z d S)r.   zProcess HTTP error responses.i  c             C   sd   | j  | j | j   } } } d | k o7 d k  n s` |  j j d | | | | |  } n  | S)N   i,  r   )codemsgrR   r   r   )r|   rq   r   r   r   r   rI   rI   rJ   http_response4  s
     	z HTTPErrorProcessor.http_responseN)r   r   r   __doc__r   r   https_responserI   rI   rI   rJ   r.   0  s   c               @   s   e  Z d  Z d d   Z d S)r   c             C   s   t  | j | | | |   d  S)N)r   rl   )r|   r   r`   r   r   r   rI   rI   rJ   r   B  s    z*HTTPDefaultErrorHandler.http_error_defaultN)r   r   r   r   rI   rI   rI   rJ   r   A  s   c               @   sH   e  Z d  Z d Z d Z d d   Z d d   Z e Z Z Z	 d Z
 d S)	r      
   c       	         s   | j    } | d k r$ | d k p9 | d k o9 | d k sZ t | j | | | |   n  | j d d	  } d   t   f d d   | j j   D  } t | d | d | j d 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.
        -  .  /  3  r   HEADr    z%20content-lengthcontent-typec             3   s3   |  ]) \ } } | j      k r | | f Vq d  S)N)rp   ).0kv)CONTENT_HEADERSrI   rJ   	<genexpr>d  s    	z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>ra   ry   rz   T)r   r   r   r   )zGETr   )r   r   r   )r   r   )	r   r   rl   replacer   ra   rw   r   ry   )	r|   r   r`   r   r   ra   newurlmZ
newheadersrI   )r   rJ   r   M  s    
(		z$HTTPRedirectHandler.redirect_requestc       
      C   s  d | k r | d } n d | k r2 | d } n d  St  |  } | j d k rv t | | d | | f | |   n  | j s t |  } d | d	 <n  t |  } t | j |  } |  j | | | | | |  } | d  k r d  St	 | d
  r[| j
 }	 | _
 |	 j | d  |  j k s3t |	  |  j k rot | j | |  j | | |   qon i  }	 | _
 | _
 |	 j | d  d |	 | <| j   | j   |  j j | d | j S)Nlocationurir   r   ftprj   z+%s - Redirection to url '%s' is not allowed/r   redirect_dictr   rN   rG   )zhttpzhttpszftprj   )r   schemer   rT   r   r   r   rl   r   r   r   r   max_repeatsr\   max_redirectionsinf_msgr[   r   r   rD   rG   )
r|   r   r`   r   r   ra   r   urlpartsnewZvisitedrI   rI   rJ   http_error_302o  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  rI   rI   rI   rJ   r   E  s   "7c       	      C   s   t  |   \ } } | j d  s0 d } |  } nY | j d  sR t d |    n  | j d d  } | d k ry d } n  | d |  } t |  \ } } | d k	 r t |  \ } } n
 d } } | | | | f S)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//zproxy URL with no authority: %rr   rN   rO   )r   r   r@   r   r   r   )	proxyr   Zr_scheme	authorityendZuserinfohostportuserpasswordrI   rI   rJ   _parse_proxy  s    		
r  c               @   s1   e  Z d  Z d Z d d d  Z d d   Z d S)r   d   Nc          	   C   s   | d  k r t    } n  t | d  s3 t d   | |  _ x@ | j   D]2 \ } } t |  d | | | |  j d d   qI Wd  S)Nkeyszproxies must be a mappingz%s_openc             S   s   | |  | |  S)NrI   )rr  r   r   rI   rI   rJ   <lambda>  s    z'ProxyHandler.__init__.<locals>.<lambda>)r4   r   AssertionErrorproxiesrw   setattrr   )r|   r  r   rE   rI   rI   rJ   r     s    	zProxyHandler.__init__c             C   s  | j  } t |  \ } } } } | d  k r6 | } n  | j rR t | j  rR d  S| r | r d t |  t |  f }	 t j |	 j    j d  }
 | j	 d d |
  n  t |  } | j
 | |  | | k s | d k r d  S|  j j | d | j Sd  S)Nz%s:%sasciizProxy-authorizationzBasic r   rG   )r   r  rr   proxy_bypassr
   base64	b64encodeencodedecoderx   r   r   rD   rG   )r|   r   r  r   Z	orig_typeZ
proxy_typer  r  r  Z	user_passZcredsrI   rI   rJ   r     s     		zProxyHandler.proxy_open)r   r   r   r   r   r   rI   rI   rI   rJ   r     s   
c               @   sO   e  Z d  Z d d   Z d d   Z d d   Z d d d	  Z d
 d   Z d S)r    c             C   s   i  |  _  d  S)N)passwd)r|   rI   rI   rJ   r     s    zHTTPPasswordMgr.__init__c                s   t  | t  r | g } n  |  j k r: i   j | <n  xG d D]?   t    f d d   | D  } | | f  j | | <qA Wd  S)NTFc                s"   g  |  ] }  j  |     q SrI   )
reduce_uri)r   u)default_portr|   rI   rJ   
<listcomp>  s   	 z0HTTPPasswordMgr.add_password.<locals>.<listcomp>)TF)r   r   r  tuple)r|   realmr   r  r  Zreduced_urirI   )r  r|   rJ   add_password  s    zHTTPPasswordMgr.add_passwordc       	      C   s   |  j  j | i   } xd d D]\ } |  j | |  } xA | j   D]3 \ } } x$ | D] } |  j | |  rT | SqT WqA Wq Wd S)NTF)TF)NN)r  r   r  rw   	is_suburi)	r|   r"  authuriZdomainsr  Zreduced_authuriZurisZauthinfor   rI   rI   rJ   find_user_password  s    z"HTTPPasswordMgr.find_user_passwordTc       
      C   s   t  |  } | d r= | d } | d } | d p7 d } n d } | } d } t |  \ } } | r | d k r | d k	 r i d d 6d d	 6j |  }	 |	 d k	 r d
 | |	 f } q n  | | f S)z@Accept authority or URI and extract only the authority and path.rN   r   r   r   NP   r   i  r   z%s:%d)r   r   r   )
r|   r   r  partsr   r	  rT   rr   portZdportrI   rI   rJ   r    s    


zHTTPPasswordMgr.reduce_uric             C   si   | | k r d S| d | d k r( d St  j | d | d f  } t |  t | d  k re d Sd S)zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        Tr   FrN   )	posixpathcommonprefixr\   )r|   basetestZcommonrI   rI   rJ   r$  %  s    zHTTPPasswordMgr.is_suburiN)r   r   r   r   r#  r&  r  r$  rI   rI   rI   rJ   r      s
   
c               @   s   e  Z d  Z d d   Z d S)r!   c             C   sD   t  j |  | |  \ } } | d  k	 r1 | | f St  j |  d  |  S)N)r    r&  )r|   r"  r%  r  r  rI   rI   rJ   r&  6  s
    
z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r   r   r   r&  rI   rI   rI   rJ   r!   4  s   c               @   sL   e  Z d  Z e j d e j  Z d d d  Z d d   Z d d   Z	 d S)	r"   z1(?:.*,)*[ 	]*([^ 	]+)[ 	]+realm=(["']?)([^"']*)\2Nc             C   s4   | d  k r t    } n  | |  _ |  j j |  _ d  S)N)r    r  r#  )r|   Zpassword_mgrrI   rI   rJ   r   L  s    	z!AbstractBasicAuthHandler.__init__c       	      C   s   | j  | d   } | r | j   d } | j   d k rM t d |   q t j j |  } | r | j   \ } } } | d k r t j	 d t
 d  n  | j   d k r |  j | | |  Sq n  d  S)	Nr   basiczDAbstractBasicAuthHandler does not support the following scheme: '%s'"'zBasic Auth Realm was unquotedr   )r/  r0  )r   splitrp   r@   r"   rxsearchgroupswarningswarnUserWarningretry_http_basic_auth)	r|   authreqrr   r   ra   r   Zmor	   r"  rI   rI   rJ   http_error_auth_reqedR  s    	z.AbstractBasicAuthHandler.http_error_auth_reqedc             C   s   |  j  j | |  \ } } | d  k	 r d | | f } d t j | j    j d  } | j |  j d   | k rx d  S| j |  j |  |  j	 j
 | d | j Sd  Sd  S)Nz%s:%szBasic r  rG   )r  r&  r  r  r  r  rm   auth_headerr   r   rD   rG   )r|   rr   r   r"  r  pwrawauthrI   rI   rJ   r8  h  s    "z.AbstractBasicAuthHandler.retry_http_basic_auth)
r   r   r   recompileIr2  r   r:  r8  rI   rI   rI   rJ   r"   >  s
   	c               @   s"   e  Z d  Z d Z d d   Z d S)r#   Authorizationc             C   s%   | j  } |  j d | | |  } | S)Nzwww-authenticate)rl   r:  )r|   r   r`   r   r   ra   rE   r   rI   rI   rJ   http_error_401y  s    		z#HTTPBasicAuthHandler.http_error_401N)r   r   r   r;  rC  rI   rI   rI   rJ   r#   u  s   c               @   s"   e  Z d  Z d Z d d   Z d S)r$   zProxy-authorizationc             C   s%   | j  } |  j d | | |  } | S)Nzproxy-authenticate)rr   r:  )r|   r   r`   r   r   ra   r	  r   rI   rI   rJ   http_error_407  s    		z$ProxyBasicAuthHandler.http_error_407N)r   r   r   r;  rD  rI   rI   rI   rJ   r$     s   c               @   ss   e  Z d  Z d d d  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z	 d d   Z
 d S)r%   Nc             C   sO   | d  k r t    } n  | |  _ |  j j |  _ d |  _ d |  _ d  |  _ d  S)Nr   )r    r  r#  retriednonce_count
last_nonce)r|   r  rI   rI   rJ   r     s    			z"AbstractDigestAuthHandler.__init__c             C   s   d |  _  d  S)Nr   )rE  )r|   rI   rI   rJ   reset_retry_count  s    z+AbstractDigestAuthHandler.reset_retry_countc             C   s   | j  | d   } |  j d k r? t | j d d | d    n |  j d 7_ | r | j   d } | j   d k r |  j | |  S| j   d k r t d |   q n  d  S)	N   i  zdigest auth failedrN   r   Zdigestr.  zEAbstractDigestAuthHandler does not support the following scheme: '%s')r   rE  r   rl   r1  rp   retry_http_digest_authr@   )r|   r;  rr   r   ra   r9  r   rI   rI   rJ   r:    s    z/AbstractDigestAuthHandler.http_error_auth_reqedc             C   s   | j  d d  \ } } t t d  t |    } |  j | |  } | r d | } | j j |  j d   | k rw d  S| j |  j |  |  j	 j
 | d | j } | Sd  S)Nr   rN   z	Digest %srG   )r1  parse_keqv_listfilterparse_http_listget_authorizationra   r   r;  r   r   rD   rG   )r|   r   r>  tokenZ	challengechalZauth_valZresprI   rI   rJ   rJ    s    
z0AbstractDigestAuthHandler.retry_http_digest_authc             C   sX   d |  j  | t j   f } | j d  t d  } t j |  j   } | d  d  S)Nz	%s:%s:%s:r  rM      )rF  timeZctimer  _randombyteshashlibsha1	hexdigest)r|   noncesbdigrI   rI   rJ   
get_cnonce  s    z$AbstractDigestAuthHandler.get_cnoncec             C   s_  yK | d } | d } | j  d  } | j  d d  } | j  d d   } Wn t k
 rc d  SYn X|  j |  \ } }	 | d  k r d  S|  j j | | j  \ }
 } |
 d  k r d  S| j d  k	 r |  j | j |  } n d  } d |
 | | f } d | j   | j	 f } | d	 k r| |  j
 k r=|  j d
 7_ n d
 |  _ | |  _
 d |  j } |  j |  } d | | | | | |  f } |	 | |  |  } nD | d  k r|	 | |  d | | |  f  } n t d |   d |
 | | | j	 | f } | r| d | 7} n  | r0| d | 7} n  | d | 7} | r[| d | | f 7} n  | S)Nr"  rW  qop	algorithmMD5opaquez%s:%s:%sz%s:%sr>  rN   z%08xz%s:%s:%s:%s:%szqop '%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&  rl   rF   get_entity_digestr   r   rG  rF  r[  r   )r|   r   rP  r"  rW  r\  r]  r_  HKDr  r<  ZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr,  rI   rI   rJ   rN    sV    

			(
z+AbstractDigestAuthHandler.get_authorizationc                sR   | d k r d d     n | d k r6 d d     n    f d d   }   | f S)Nr^  c             S   s   t  j |  j d   j   S)Nr  )rT  Zmd5r  rV  )xrI   rI   rJ   r    s    z?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAc             S   s   t  j |  j d   j   S)Nr  )rT  rU  r  rV  )re  rI   rI   rJ   r    s    c                s     d |  | f  S)Nz%s:%srI   )rX  d)rc  rI   rJ   r    s    rI   )r|   r]  rd  rI   )rc  rJ   ra    s    z-AbstractDigestAuthHandler.get_algorithm_implsc             C   s   d  S)NrI   )r|   rF   rP  rI   rI   rJ   rb    s    z+AbstractDigestAuthHandler.get_entity_digest)r   r   r   r   rH  r:  rJ  r[  rN  ra  rb  rI   rI   rI   rJ   r%     s   	<
c               @   s.   e  Z d  Z d Z d Z d Z d d   Z d S)r&   zAn authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    rB  i  c             C   s9   t  | j  d } |  j d | | |  } |  j   | S)NrN   zwww-authenticate)r   rl   r:  rH  )r|   r   r`   r   r   ra   rr   retryrI   rI   rJ   rC  *  s
    	
z$HTTPDigestAuthHandler.http_error_401N)r   r   r   r   r;  r   rC  rI   rI   rI   rJ   r&      s   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)r'   zProxy-Authorizationi  c             C   s/   | j  } |  j d | | |  } |  j   | S)Nzproxy-authenticate)rr   r:  rH  )r|   r   r`   r   r   ra   rr   rg  rI   rI   rJ   rD  7  s
    		
z%ProxyDigestAuthHandler.http_error_407N)r   r   r   r;  r   rD  rI   rI   rI   rJ   r'   2  s   c               @   sC   e  Z d  Z d d d  Z d d   Z d d   Z d d	   Z d
 S)AbstractHTTPHandlerr   c             C   s   | |  _  d  S)N)_debuglevel)r|   
debuglevelrI   rI   rJ   r   @  s    zAbstractHTTPHandler.__init__c             C   s   | |  _  d  S)N)ri  )r|   levelrI   rI   rJ   set_http_debuglevelC  s    z'AbstractHTTPHandler.set_http_debuglevelc             C   s  | j  } | s t d   n  | j d  k	 r| j } t | t  rZ d } t |   n  | j d  s| | j d d  n  | j d  sy t |  } WnC t k
 r t | t	 j
  r t d t |  | f   n  YqX| j d d t |  | j  qn  | } | j   rDt | j  \ } } t |  \ } }	 n  | j d  sf| j d |  n  xH |  j j D]: \ }
 } |
 j   }
 | j |
  ss| j |
 |  qsqsW| S)	Nzno host givenzLPOST data should be bytes or an iterable of bytes. It cannot be of type str.zContent-typez!application/x-www-form-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drk   )rr   r   rF   r   r   r   r   r   
memoryviewcollectionsIterabler@   r   r\   itemsizer   r   r   r   r   r   r   )r|   rq   rr   rF   r   ZmvZsel_hostr   ZselZsel_pathrW   r~   rI   rI   rJ   do_request_F  sB    		
	!zAbstractHTTPHandler.do_request_c       
         s  | j  } | s t d   n  | | d | j | } t | j      j t   f d d   | j j   D   d   d <t d d     j   D    | j r i  } d } |   k r   | | | <  | =n  | j	 | j d	 | n  yj y& | j
 | j   | j | j    Wn1 t k
 rK} z t |   WYd
 d
 } ~ Xn X| j   }	 Wn | j     Yn X| j r| j j   d
 | _ n  | j   |	 _ |	 j |	 _ |	 S)zReturn an HTTPResponse object for the request, using http_class.

        http_class must implement the HTTPConnection API from http.client.
        zno host givenrG   c             3   s-   |  ]# \ } } |   k r | | f Vq d  S)NrI   )r   r   r   )ra   rI   rJ   r   {  s    	z.AbstractHTTPHandler.do_open.<locals>.<genexpr>r   
Connectionc             s   s'   |  ] \ } } | j    | f Vq d  S)N)title)r   rW   r   rI   rI   rJ   r     s    zProxy-Authorizationra   N)rr   r   rG   r   rt   r   ra   rw   rv   Z
set_tunnelrq   r   r   rF   ri   getresponser   Zsockr   rE   reasonr   )
r|   Z
http_classr   Zhttp_conn_argsrr   r   Ztunnel_headersZproxy_auth_hdrerrr  rI   )ra   rJ   r   n  s<    	/
	
&
	zAbstractHTTPHandler.do_openN)r   r   r   r   rl  rq  r   rI   rI   rI   rJ   rh  >  s   (rh  c               @   s%   e  Z d  Z d d   Z e j Z d S)r(   c             C   s   |  j  t j j |  S)N)r   r   r   HTTPConnection)r|   r   rI   rI   rJ   	http_open  s    zHTTPHandler.http_openN)r   r   r   rx  rh  rq  http_requestrI   rI   rI   rJ   r(     s   r   c               @   s:   e  Z d  Z d d d d d  Z d d   Z e j Z d S)rB   r   Nc             C   s&   t  j |  |  | |  _ | |  _ d  S)N)rh  r   _context_check_hostname)r|   rj  r=   r>   rI   rI   rJ   r     s    	zHTTPSHandler.__init__c             C   s(   |  j  t j j | d |  j d |  j S)Nr=   r>   )r   r   r   r   rz  r{  )r|   r   rI   rI   rJ   
https_open  s    zHTTPSHandler.https_open)r   r   r   r   r|  rh  rq  https_requestrI   rI   rI   rJ   rB     s   rB   c               @   sC   e  Z d  Z d d d  Z d d   Z d d   Z e Z e Z d S)r   Nc             C   s7   d d  l  } | d  k r* | j j   } n  | |  _ d  S)Nr   )Zhttp.cookiejar	cookiejarZ	CookieJar)r|   r~  r   rI   rI   rJ   r     s    zHTTPCookieProcessor.__init__c             C   s   |  j  j |  | S)N)r~  Zadd_cookie_header)r|   rq   rI   rI   rJ   ry    s    z HTTPCookieProcessor.http_requestc             C   s   |  j  j | |  | S)N)r~  Zextract_cookies)r|   rq   r   rI   rI   rJ   r     s    z!HTTPCookieProcessor.http_response)r   r   r   r   ry  r   r}  r   rI   rI   rI   rJ   r     s
   c               @   s   e  Z d  Z d d   Z d S)r-   c             C   s   | j  } t d |   d  S)Nzunknown url type: %s)r   r   )r|   r   r   rI   rI   rJ   r     s    	zUnknownHandler.unknown_openN)r   r   r   r   rI   rI   rI   rJ   r-     s   c             C   sp   i  } xc |  D][ } | j  d d  \ } } | d d k r^ | d d k r^ | d d  } n  | | | <q W| S)z>Parse list of key=value strings where keys are not duplicated.=rN   r   r/  rO   rO   )r1  )lZparsedZeltr   r   rI   rI   rJ   rK    s     rK  c             C   s   g  } d } d } } x |  D] } | r? | | 7} d } q n  | r | d k r] d } q n | d k rr d } n  | | 7} q n  | d k r | j  |  d } q n  | d k r d } n  | | 7} q W| r | j  |  n  d d   | D S)	ap  Parse 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.
    rj   F\Tr/  ,c             S   s   g  |  ] } | j     q SrI   )strip)r   partrI   rI   rJ   r     s   	 z#parse_http_list.<locals>.<listcomp>)rY   )rX  Zresr  escaper	   ZcurrI   rI   rJ   rM    s4    	

	
	rM  c               @   s:   e  Z d  Z d d   Z d Z d d   Z d d   Z d S)r)   c             C   s   | j  } | d  d  d k rt | d d  d k rt | j rt | j d k rt | j |  j   k r t d   q n |  j |  Sd  S)Nr   z//r9   r   	localhostz-file:// scheme is supported only on localhost)r   rr   	get_namesr   open_local_file)r|   r   rE   rI   rI   rJ   	file_open  s    	5zFileHandler.file_openNc             C   s}   t  j d  k rv y7 t t j d  d t j t j    d  t  _ Wqv t j k
 rr t j d  f t  _ Yqv Xn  t  j S)Nr  r   )r)   namesr!  r   gethostbyname_exgethostnamegaierrorgethostbyname)r|   rI   rI   rJ   r     s    $zFileHandler.get_namesc             C   sa  d d  l  } d d  l } | j } | j } t |  } y t j |  } | j } | j j	 | j
 d d }	 | j |  d }
 | j d |
 p d | |	 f  } | r t |  \ } } n  | s | rt |  |  j   k r| r d | | } n
 d | } t t | d  | |  SWn1 t k
 rP} z t |   WYd  d  } ~ Xn Xt d   d  S)	Nr   usegmtTz6Content-type: %s
Content-length: %d
Last-modified: %s
z
text/plainzfile://rbzfile not on local host)email.utils	mimetypesrr   r   r3   rS   statst_sizeutils
formatdatest_mtime
guess_typemessage_from_stringr   _safe_gethostbynamer  r   rD   ri   r   )r|   r   emailr  rr   r^   Z	localfilestatsre   modifiedmtypera   r)  ZorigurlexprI   rI   rJ   r  +  s0    			
zFileHandler.open_local_file)r   r   r   r  r  r  r  rI   rI   rI   rJ   r)     s   
c             C   s1   y t  j |   SWn t  j k
 r, d  SYn Xd  S)N)r   r  r  )rr   rI   rI   rJ   r  G  s    r  c               @   s(   e  Z d  Z d d   Z d d   Z d S)r*   c             C   s  d d  l  } d d  l } | j } | s6 t d   n  t |  \ } } | d  k r` | j } n t |  } t |  \ } } | r t |  \ } } n d  } t	 |  } | p d } | p d } y t
 j |  } Wn1 t k
 r	} z t |   WYd  d  } ~ Xn Xt | j  \ }	 }
 |	 j d  } t t t	 |   } | d  d  | d } } | r| d r| d d   } n  y|  j | | | | | | j  } | rd pd } xM |
 D]E } t |  \ } } | j   d k r| d k r| j   } qqW| j | |  \ } } d } | j | j  d } | rS| d | 7} n  | d  k	 r|| d k r|| d | 7} n  t j |  } t | | | j  SWnQ | j k
 r} z. t d |  } | j t j    d   WYd  d  } ~ Xn Xd  S)Nr   zftp error: no host givenrj   r   rN   rA  Dr   aAr   rf  zContent-type: %s
zContent-length: %d
zftp error: %rr   rO   rO   )r  r  r   rA  rf  r  )!ftplibr  rr   r   r   FTP_PORTrZ   r   r   r
   r   r  ri   r   r   r1  r   mapconnect_ftprG   r   rp   upperretrfiler  rl   r  r  r   
all_errorswith_tracebacksysexc_info)r|   r   r  r  rr   r)  r  r  r   rT   attrsdirsrK   Zfwr   attrr~   r`   retrlenra   r  r  excrI   rI   rJ   ftp_openN  s\    	!zFTPHandler.ftp_openc          	   C   s   t  | | | | | | d d S)N
persistentF)
ftpwrapper)r|   r  r  rr   r)  r  rG   rI   rI   rJ   r    s    zFTPHandler.connect_ftpN)r   r   r   r  r  rI   rI   rI   rJ   r*   M  s   5c               @   sX   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d d   Z d S)r+   c             C   s1   i  |  _  i  |  _ d |  _ d |  _ d |  _ d  S)Nr   <   rQ  )cacherG   soonestdelay	max_conns)r|   rI   rI   rJ   r     s
    				zCacheFTPHandler.__init__c             C   s   | |  _  d  S)N)r  )r|   trI   rI   rJ   
setTimeout  s    zCacheFTPHandler.setTimeoutc             C   s   | |  _  d  S)N)r  )r|   r   rI   rI   rJ   setMaxConns  s    zCacheFTPHandler.setMaxConnsc             C   s   | | | d j  |  | f } | |  j k rJ t j   |  j |  j | <n< t | | | | | |  |  j | <t j   |  j |  j | <|  j   |  j | S)Nr   )joinr  rR  r  rG   r  check_cache)r|   r  r  rr   r)  r  rG   r}   rI   rI   rJ   r    s    
zCacheFTPHandler.connect_ftpc             C   s   t  j    } |  j | k rx xZ t |  j j    D]@ \ } } | | k  r1 |  j | j   |  j | =|  j | =q1 q1 Wn  t t |  j j     |  _ t	 |  j  |  j
 k rxJ t |  j j    D]3 \ } } | |  j k r |  j | =|  j | =Pq q Wt t |  j j     |  _ n  d  S)N)rR  r  r   rG   rw   r  r   minvaluesr\   r  )r|   r  r   r   rI   rI   rJ   r    s    "
"

zCacheFTPHandler.check_cachec             C   sB   x! |  j  j   D] } | j   q W|  j  j   |  j j   d  S)N)r  r  r   clearrG   )r|   connrI   rI   rJ   clear_cache  s    zCacheFTPHandler.clear_cacheN)	r   r   r   r   r  r  r  r  r  rI   rI   rI   rJ   r+     s   c               @   s   e  Z d  Z d d   Z d S)r,   c             C   s   | j  } | j d d  \ } } | j d d  \ } } t |  } | j d  rv t j |  } | d  d  } n  | s d } n  t j d | t |  f  } t	 t
 j |  | |  S)	N:rN   r  z;base64   ztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
i)rl   r1  r   endswithr  decodebytesr  r  r\   r   ioBytesIO)r|   r   rE   r   rF   Z	mediatypera   rI   rI   rJ   	data_open  s    
			zDataHandler.data_openN)r   r   r   r  rI   rI   rI   rJ   r,     s   r   nt)r3   r2   c             C   s
   t  |   S)zOS-specific conversion from a relative URL of the 'file' scheme
        to a file system path; not recommended for general use.)r
   )pathnamerI   rI   rJ   r3     s    c             C   s
   t  |   S)zOS-specific conversion from a file system path to a relative URL
        of the 'file' scheme; not recommended for general use.)r	   )r  rI   rI   rJ   r2     s    c               @   s4  e  Z d  Z d Z d Z d e Z d d d  Z d d   Z d d	   Z	 d
 d   Z
 d d   Z d d d  Z d d d  Z d d d  Z d d d d d  Z d d   Z d d d  Z d d d  Z d d   Z e r d d   Z d d  d!  Z n  d" d#   Z d$ d%   Z d& d'   Z d d( d)  Z d S)*r7   a,  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).NzPython-urllib/%sc             K   s   d i |  j  j d 6} t j | t d d | d  k rE t   } n  t | d  s` t d   | |  _ | j	 d  |  _
 | j	 d  |  _ d	 |  j f g |  _ g  |  _ t j |  _ d  |  _ t |  _ d  S)
NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methodsclass
stacklevelr9   r  zproxies must be a mappingkey_file	cert_filez
User-Agent)	__class__r   r5  r6  DeprecationWarningr4   r   r  r  r   r  r  versionr   _URLopener__tempfilesrS   rh   _URLopener__unlink	tempcacheftpcache)r|   r  Zx509r   rI   rI   rJ   r     s    			zURLopener.__init__c             C   s   |  j    d  S)N)r   )r|   rI   rI   rJ   __del__  s    zURLopener.__del__c             C   s   |  j    d  S)N)cleanup)r|   rI   rI   rJ   r     s    zURLopener.closec             C   ss   |  j  rV x7 |  j  D], } y |  j |  Wq t k
 r> Yq Xq W|  j  d  d   =n  |  j ro |  j j   n  d  S)N)r  r  ri   r  r  )r|   rK   rI   rI   rJ   r  "  s    			zURLopener.cleanupc             G   s   |  j  j |  d S)zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r   rY   )r|   r   rI   rI   rJ   	addheader0  s    zURLopener.addheaderc             C   s  t  t |   } t | d d } |  j rn | |  j k rn |  j | \ } } t | d  } t | | |  St |  \ } } | s d } n  | |  j k r |  j | } t |  \ } }	 t |	  \ }
 } |
 | f } n d } d | } | |  _	 | j
 d d  } t |  |  sD| r1|  j | | |  S|  j | |  Sn  y9 | d k rft |  |  |  St |  |  | |  SWna t t f k
 r  YnG t k
 r} z' t d	 |  j t j   d
   WYd d } ~ Xn Xd S)z6Use URLopener().open(file) instead of open(file, 'r').safez%/:=&?~#+!$,;'@()*[]|r  rK   NZopen_-r   zsocket errorr   )r   r   r	   r  rD   r   r   r  r   r   r   r   open_unknown_proxyopen_unknownr   r   r   ri   r  r  r  )r|   r   rF   r^   ra   r`   urltyperE   r  	proxyhostrr   r   rW   r   rI   rI   rJ   rD   6  s<    	
	zURLopener.openc             C   s(   t  |  \ } } t d d |   d S)z/Overridable interface to open unknown URL type.z	url errorzunknown url typeN)r   ri   )r|   r   rF   r   rE   rI   rI   rJ   r  Z  s    zURLopener.open_unknownc             C   s,   t  |  \ } } t d d | |   d S)z/Overridable interface to open unknown URL type.z	url errorzinvalid proxy for %sN)r   ri   )r|   r  r   rF   r   rE   rI   rI   rJ   r  _  s    zURLopener.open_unknown_proxyc              C   s  t  t |   } |  j r5 | |  j k r5 |  j | St |  \ } } | d k r | sf | d k r yC |  j |  } | j   } | j   t t |  d  | f SWq t	 k
 r }	 z WYd d }	 ~	 Xq Xn  |  j
 | |  } z| j   }
 | rt
 | d  } n d d l } t |  \ } } t | p7d  \ } } t | pOd  \ } } t | pgd  \ } } t j j |  d } | j |  \ } } |  j j |  t j | d  } z | |
 f } |  j d k	 r| |  j | <n  d } d } d } d } d	 |
 k r%t |
 d
  } n  | r>| | | |  n  x\ | j |  } | sZPn  | t |  7} | j |  | d 7} | rA| | | |  qAqAWd | j   XWd | j   X| d k r| | k  rt d | | f |   n  | S)ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.NrK   rN   rL   r   rj   i   rM   zcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi    rO   )r   r   r  r   r  rR   r   r3   r   ri   rD   rV   r   r   rS   rT   splitextZmkstempr  rY   fdopenrZ   r[   r\   r]   r   )r|   rE   r^   r_   rF   r   Zurl1r`   r   r   ra   rb   rV   ZgarbagerT   suffixfdrc   rd   re   r[   rf   rg   rI   rI   rJ   retrievee  sl    

zURLopener.retrievec             C   s  d } d } t  | t  r] t |  \ } } | rT t |  \ } } t |  } n  | } n | \ } } t |  \ } } t |  \ }	 }
 |
 } d } |	 j   d k r d } n^ t |
  \ } }
 | r t |  \ } } n  | r d |	 | |
 f } n  t |  r| } n  | s*t d d   n  | r]t |  } t	 j
 | j    j d  } n d } | rt |  } t	 j
 | j    j d  } n d } | |  } i  } | rd | | d <n  | rd | | d	 <n  | r| | d
 <n  d | d <x! |  j D] \ } } | | | <qW| d k	 rLd | d <| j d | | |  n | j d | d | y | j   } Wn$ t j j k
 rt d   Yn Xd | j k od k  n rt | | j d | | j  S|  j | | j | j | j | j |  Sd S)a  Make an HTTP connection using connection_class.

        This is an internal method that should be called from
        open_http() or open_https().

        Arguments:
        - connection_factory should take a host name and return an
          HTTPConnection instance.
        - url is the url to retrieval or a host, relative-path pair.
        - data is payload for a POST request or None.
        Nr   z	%s://%s%sz
http errorzno host givenr  zBasic %szProxy-AuthorizationrB  rk   r   rr  z!application/x-www-form-urlencodedzContent-Typer   r   ra   z$http protocol error: bad status liner   i,  zhttp:)r   r   r   r   r
   r   rp   r  ri   r  r  r  r  r   rq   rt  r   r   ZBadStatusLiner   Zstatusr   r   
http_errorr`   ru  )r|   Zconnection_factoryrE   rF   Zuser_passwdZproxy_passwdrr   r   Zrealhostr  r   Z
proxy_authr>  Z	http_connra   headerr~   r   rI   rI   rJ   _open_generic_http  sr    			 !!


	zURLopener._open_generic_httpc             C   s   |  j  t j j | |  S)zUse HTTP protocol.)r  r   r   rw  )r|   rE   rF   rI   rI   rJ   	open_http  s    zURLopener.open_httpc       
      C   s   d | } t  |  |  rw t |  |  } | d k rO | | | | | |  }	 n | | | | | | |  }	 |	 rw |	 Sn  |  j | | | | |  S)zHandle http errors.

        Derived class can override this, or provide specific handlers
        named http_error_DDD where DDD is the 3-digit error code.zhttp_error_%dN)r   r   r   )
r|   rE   r`   errcodeerrmsgra   rF   rW   r{   rc   rI   rI   rJ   r    s    
 zURLopener.http_errorc             C   s&   | j    t | | | | d   d S)z>Default error handler: close the connection and raise OSError.N)r   r   )r|   rE   r`   r  r  ra   rI   rI   rJ   r     s    
zURLopener.http_error_defaultc             C   s"   t  j j | d |  j d |  j S)Nr  r  )r   r   r   r  r  )r|   rr   rI   rI   rJ   _https_connection  s    	zURLopener._https_connectionc             C   s   |  j  |  j | |  S)zUse HTTPS protocol.)r  r  )r|   rE   rF   rI   rI   rJ   
open_https"  s    zURLopener.open_httpsc             C   s   t  | t  s t d   n  | d d  d k ru | d d  d k ru | d d  j   d k ru t d	   n |  j |  Sd S)
z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr   z//r9   r      z
localhost/z-file:// scheme is supported only on localhost)r   r   r   rp   r@   r  )r|   rE   rI   rI   rJ   	open_file&  s
    HzURLopener.open_filec             C   s  d d l  } d d l } t |  \ } } t |  } y t j |  } Wn: t k
 r } z t | j | j	   WYd d } ~ Xn X| j
 }	 | j j | j d d }
 | j |  d } | j d | p d |	 |
 f  } | s'| } | d d  d k rd	 | } n  t t | d
  | |  St |  \ } } | rt j |  t   f t   k r| } | d d  d k rd	 | } n) | d d  d k rt d |   n  t t | d
  | |  St d   d S)zUse local file.r   Nr  Tz6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainrN   r   zfile://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   r3   rS   r  ri   r   strerrorr^   r  r  r  r  r  r  r   rD   r   r   r  r  thishostr@   )r|   rE   r  r  rr   rK   Z	localnamer  ere   r  r  ra   Zurlfiler)  rI   rI   rJ   r  /  s:    (	"zURLopener.open_local_filec             C   s  t  | t  s t d   n  d d l } t |  \ } } | sQ t d   n  t |  \ } } t |  \ } } | r t |  \ } } n d } t |  } t | p d  } t | p d  } t	 j
 |  } | s d d l } | j } n t |  } t |  \ } }	 t |  } | j d  }
 |
 d d  |
 d }
 } |
 rk|
 d rk|
 d d  }
 n  |
 r|
 d rd |
 d <n  | | | d j |
  f } t |  j  t k rxJ t |  j  D]6 } | | k r|  j | } |  j | =| j   qqWn  y9| |  j k r:t | | | | |
  |  j | <n  | sId } n d	 } xM |	 D]E } t |  \ } } | j   d
 k rV| d k rV| j   } qVqVW|  j | j | |  \ } } | j d |  d } d } | r| d | 7} n  | d k	 r| d k r| d | 7} n  t j |  } t | | d |  SWnK t   k
 r} z( t d |  j  t! j"   d   WYd d } ~ Xn Xd S)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedr   Nzftp error: no host givenrj   r   rN   r  rA  r   r  r  r   rf  zftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr   rO   rO   )r  r  r   rA  rf  r  )#r   r   r   r  r   r   r   r   r
   r   r  r  r  rZ   r   r1  r  r\   r  MAXFTPCACHEr   r   r  r   rp   r  r  r  r  r  r   	ftperrorsr  r  r  )r|   rE   r  rr   rT   r)  r  r  r  r  r  rK   r}   r   r   r   r  r~   r`   r  r  ra   r  rI   rI   rJ   open_ftpO  sp        
" 	zURLopener.open_ftpc       	      C   s  t  | t  s t d   n  y | j d d  \ } } Wn! t k
 r] t d d   Yn X| sm d } n  | j d  } | d k r d	 | | d
  k r | | d d
  } | d
 |  } n d } g  } | j d t j	 d t j
 t j      | j d |  | d k r>t j | j d   j d  } n t |  } | j d t |   | j d  | j |  d j |  } t j |  } t j |  } t | | |  S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr  rN   z
data errorzbad data URLztext/plain;charset=US-ASCII;r   r  Nrj   zDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %sr  r  zlatin-1zContent-Length: %d
)r   r   r   r1  r@   ri   rfindrY   rR  ZstrftimeZgmtimer  r  r  r  r
   r\   r  r  r  r  StringIOr   )	r|   rE   rF   r   Zsemiencodingr   ra   frI   rI   rJ   	open_data  s6    	"$zURLopener.open_data)r   r   r   r   r  r   r  r   r  r   r  r  rD   r  r  r  r  r  r  r   r?   r  r  r  r  r  r  rI   rI   rI   rJ   r7     s.   
$B\	 :c               @   s   e  Z d  Z d Z d d   Z d d   Z d d d  Z d	 d
   Z d d d  Z d d d  Z	 d d d  Z
 d d d d  Z d d d d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d d   Z d! d"   Z d S)#r8   z?Derived class with handlers for errors we can handle (perhaps).c             O   s2   t  j |  | |  i  |  _ d |  _ d |  _ d  S)Nr   r   )r7   r   
auth_cachetriesmaxtries)r|   r   kwargsrI   rI   rJ   r     s    		zFancyURLopener.__init__c             C   s   t  | | d | |  S)z3Default error handling -- don't raise an exception.zhttp:)r   )r|   rE   r`   r  r  ra   rI   rI   rJ   r     s    z!FancyURLopener.http_error_defaultNc       	      C   s   |  j  d 7_  |  j rm |  j  |  j k rm t |  d  rE |  j } n	 |  j } d |  _  | | | d d |  S|  j | | | | | |  } d |  _  | S)z%Error 302 -- relocated (temporarily).rN   http_error_500r   i  z)Internal Server Error: Redirect Recursion)r  r  r   r  r   redirect_internal)	r|   rE   r`   r  r  ra   rF   r   rc   rI   rI   rJ   r    s    		
		zFancyURLopener.http_error_302c       	      C   s   d | k r | d } n d | k r2 | d } n d  S| j    t |  j d | |  } t |  } | j d	 k r t | | | d | | |   n  |  j |  S)
Nr   r   r  r   r   r   rj   z( Redirection to url '%s' is not allowed.)zhttpzhttpszftprj   )r   r   r   r   r   r   rD   )	r|   rE   r`   r  r  ra   rF   r   r  rI   rI   rJ   r    s    
	z FancyURLopener.redirect_internalc             C   s   |  j  | | | | | |  S)z*Error 301 -- also relocated (permanently).)r  )r|   rE   r`   r  r  ra   rF   rI   rI   rJ   r    s    zFancyURLopener.http_error_301c             C   s   |  j  | | | | | |  S)z;Error 303 -- also relocated (essentially identical to 302).)r  )r|   rE   r`   r  r  ra   rF   rI   rI   rJ   r    s    zFancyURLopener.http_error_303c             C   sE   | d k r( |  j  | | | | | |  S|  j | | | | |  Sd S)z1Error 307 -- relocated, but turn POST into error.N)r  r   )r|   rE   r`   r  r  ra   rF   rI   rI   rJ   r    s    zFancyURLopener.http_error_307Fc             C   s$  d | k r+ t  j |  | | | | |  n  | d } t j d |  }	 |	 sl t  j |  | | | | |  n  |	 j   \ }
 } |
 j   d k r t  j |  | | | | |  n  | s t  j |  | | | | |  n  d |  j d } | d k rt |  |  | |  St |  |  | | |  Sd S)z_Error 401 -- authentication required.
        This function supports Basic authentication only.zwww-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r.  Zretry__basic_authN)r7   r   r?  matchr4  rp   r   r   )r|   rE   r`   r  r  ra   rF   rg  stuffr  r   r"  rW   rI   rI   rJ   rC    s&    

zFancyURLopener.http_error_401c             C   s$  d | k r+ t  j |  | | | | |  n  | d } t j d |  }	 |	 sl t  j |  | | | | |  n  |	 j   \ }
 } |
 j   d k r t  j |  | | | | |  n  | s t  j |  | | | | |  n  d |  j d } | d k rt |  |  | |  St |  |  | | |  Sd S)zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.zproxy-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r.  Zretry_proxy_r  N)r7   r   r?  r  r4  rp   r   r   )r|   rE   r`   r  r  ra   rF   rg  r  r  r   r"  rW   rI   rI   rJ   rD    s&    

zFancyURLopener.http_error_407c             C   s  t  |  \ } } d | | } |  j d } t |  \ } }	 t  |	  \ }	 }
 |	 j d  d } |	 | d   }	 |  j |	 | |  \ } } | p | s d  Sd t | d d t | d d |	 f }	 d |	 |
 |  j d <| d  k r |  j |  S|  j | |  Sd  S)Nzhttp://r   @rN   z%s:%s@%sr  rj   )r   r  r   r   get_user_passwdr	   rD   )r|   rE   r"  rF   rr   r   r   r  r  r  proxyselectorr   r  r  rI   rI   rJ   retry_proxy_http_basic_auth-  s      z*FancyURLopener.retry_proxy_http_basic_authc             C   s  t  |  \ } } d | | } |  j d } t |  \ } }	 t  |	  \ }	 }
 |	 j d  d } |	 | d   }	 |  j |	 | |  \ } } | p | s d  Sd t | d d t | d d |	 f }	 d |	 |
 |  j d <| d  k r |  j |  S|  j | |  Sd  S)Nzhttps://r   r  rN   z%s:%s@%sr  rj   )r   r  r   r   r  r	   rD   )r|   rE   r"  rF   rr   r   r   r  r  r  r  r   r  r  rI   rI   rJ   retry_proxy_https_basic_auth?  s      z+FancyURLopener.retry_proxy_https_basic_authc       
      C   s   t  |  \ } } | j d  d } | | d   } |  j | | |  \ } } | pY | s` d  Sd t | d d t | d d | f } d | | }	 | d  k r |  j |	  S|  j |	 |  Sd  S)Nr  rN   z%s:%s@%sr  rj   zhttp://)r   r   r  r	   rD   )
r|   rE   r"  rF   rr   r   r   r  r  r   rI   rI   rJ   r8  Q  s     z$FancyURLopener.retry_http_basic_authc       
      C   s   t  |  \ } } | j d  d } | | d   } |  j | | |  \ } } | pY | s` d  Sd t | d d t | d d | f } d | | }	 | d  k r |  j |	  S|  j |	 |  Sd  S)Nr  rN   z%s:%s@%sr  rj   zhttps://)r   r   r  r	   rD   )
r|   rE   r"  rF   rr   r   r   r  r  r   rI   rI   rJ   retry_https_basic_auth_  s     z%FancyURLopener.retry_https_basic_authr   c             C   s   | d | j    } | |  j k rD | r6 |  j | =qD |  j | Sn  |  j | |  \ } } | sh | r~ | | f |  j | <n  | | f S)Nr  )rp   r  prompt_user_passwd)r|   rr   r"  r  r}   r  r  rI   rI   rJ   r  m  s     zFancyURLopener.get_user_passwdc             C   sp   d d l  } y@ t d | | f  } | j  d | | | f  } | | f SWn t k
 rk t   d SYn Xd S)z#Override this in a GUI environment!r   NzEnter username for %s at %s: z#Enter password for %s in %s at %s: )NN)getpassinputKeyboardInterruptprint)r|   rr   r"  r  r  r  rI   rI   rJ   r  x  s    	z!FancyURLopener.prompt_user_passwd)r   r   r   r   r   r   r  r  r  r  r  rC  rD  r	  r
  r8  r  r  r  rI   rI   rI   rJ   r8     s$   c               C   s"   t  d k r t j d  a  n  t  S)z8Return the IP address of the magic hostname 'localhost'.Nr  )
_localhostr   r  rI   rI   rI   rJ   r    s    r  c               C   sg   t  d k rc y# t t j t j    d  a  Wqc t j k
 r_ t t j d  d  a  Yqc Xn  t  S)z,Return the IP addresses of the current host.Nr   r  )	_thishostr!  r   r  r  r  rI   rI   rI   rJ   r    s    #!r  c              C   s(   t  d k r$ d d l }  |  j a  n  t  S)z1Return the set of errors raised by the FTP class.Nr   )
_ftperrorsr  r  )r  rI   rI   rJ   r    s    r  c               C   s"   t  d k r t j d  a  n  t  S)z%Return an empty email Message object.Nrj   )
_noheadersr  r  rI   rI   rI   rJ   	noheaders  s    r  c               @   sp   e  Z d  Z d Z d d d d  Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z	 d d   Z
 d S)r  z;Class used by open_ftp() for cache of open FTP connections.NTc             C   sV   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d |  _ | |  _ |  j   d  S)Nr   )	r  r  rr   r)  r  rG   refcount	keepaliveinit)r|   r  r  rr   r)  r  rG   r  rI   rI   rJ   r     s    								zftpwrapper.__init__c             C   s   d d  l  } d |  _ | j   |  _ |  j j |  j |  j |  j  |  j j |  j	 |  j
  d j |  j  } |  j j |  d  S)Nr   r   )r  busyZFTPr   Zconnectrr   r)  rG   Zloginr  r  r  r  cwd)r|   r  Z_targetrI   rI   rJ   r    s    	zftpwrapper.initc          -   C   s:  d d  l  } |  j   | d k r1 d } d } n d | } d } y |  j j |  Wn/ | j k
 r |  j   |  j j |  Yn Xd  } | r0| r0y& d | } |  j j |  \ } } Wq0| j k
 r,} zG t |  d  d  d	 k rt	 d
 |  j
 t j   d   n  WYd  d  } ~ Xq0Xn  | s|  j j d  | r|  j j   }	 zV y |  j j |  Wn; | j k
 r} z t	 d
 |  |  WYd  d  } ~ Xn XWd  |  j j |	  Xd | } n d } |  j j |  \ } } n  d |  _ t | j d  |  j  }
 |  j d 7_ | j   |
 | f S)Nr   rf  r  zTYPE ArN   zTYPE zRETR r9   Z550zftp error: %rr   zLIST ZLISTr  )rf  r  )r  endtransferr   Zvoidcmdr  r  ZntransfercmdZ
error_permr   r   r  r  r  pwdr  r  r   Zmakefile
file_closer  r   )r|   rK   r   r  cmdisdirr  r  ru  r  ZftpobjrI   rI   rJ   r    sN    
  	
 

,*	
zftpwrapper.retrfilec             C   s   d |  _  d  S)Nr   )r  )r|   rI   rI   rJ   r    s    zftpwrapper.endtransferc             C   s)   d |  _  |  j d k r% |  j   n  d  S)NFr   )r  r  
real_close)r|   rI   rI   rJ   r     s    	zftpwrapper.closec             C   sC   |  j    |  j d 8_ |  j d k r? |  j r? |  j   n  d  S)NrN   r   )r  r  r  r   )r|   rI   rI   rJ   r    s    
zftpwrapper.file_closec             C   s7   |  j    y |  j j   Wn t   k
 r2 Yn Xd  S)N)r  r   r   r  )r|   rI   rI   rJ   r   	  s
    
zftpwrapper.real_close)r   r   r   r   r   r  r  r  r   r  r   rI   rI   rI   rJ   r    s   	-r  c              C   si   i  }  x\ t  j j   D]K \ } } | j   } | r | d d  d k r | |  | d d  <q q W|  S)a  Return a dictionary of scheme -> proxy server URL mappings.

    Scan the environment for variables named <scheme>_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_proxyii)rS   environrw   rp   )r  rW   r~   rI   rI   rJ   getproxies_environment		  s    	r$  c             C   s   t  j j d d  p' t  j j d d  } | d k r: d St |   \ } } d d   | j d  D } x6 | D]. } | ro | j |  s |  j |  ro d Sqo Wd	 S)
zTest if proxies should not be used for a particular host.

    Checks the environment for a variable named no_proxy, which should
    be a list of DNS suffixes separated by commas, or '*' for all hosts.
    no_proxyrj   ZNO_PROXY*rN   c             S   s   g  |  ] } | j     q SrI   )r  )r   r  rI   rI   rJ   r   &	  s   	 z,proxy_bypass_environment.<locals>.<listcomp>r  r   )rS   r#  r   r   r1  r  )rr   r%  hostonlyr)  Zno_proxy_listrW   rI   rI   rJ   proxy_bypass_environment	  s    *$r(  c             C   s  d d l  m  } t |   \ } } d d   } d |  k rK | d rK d Sn  d } x+| j d	 f   D]} | sv qd n  t j d
 |  } | d k	 rh| d k r y t j |  } | |  } Wq t k
 r wd Yq Xn  | | j d   }	 | j d  }
 |
 d k r-d | j d  j	 d  d }
 n t
 |
 d d   }
 d |
 }
 | |
 ?|	 |
 ?k r{d Sqd | |  |  rd d Sqd Wd S)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   )fnmatchc             S   s   |  j  d  } t t t |   } t |  d k rY | d d d d g d  d  } n  | d d >| d d >B| d d >B| d	 BS)
N.r   r      rN   rQ  r   rM   r9   )r1  r   r  rZ   r\   )ZipAddrr(  rI   rI   rJ   ip2num@	  s
    #z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr*  Zexclude_simpleTN
exceptionsz(\d+(?:\.\d+)*)(/\d+)?rN   r   rM       F)r)  r   r   r?  r  r   r  ri   groupcountrZ   )rr   proxy_settingsr)  r'  r)  r,  ZhostIPr~   r   r,  maskrI   rI   rJ   _proxy_bypass_macosx_sysconf0	  s:    
 #
r3  darwin)_get_proxy_settings_get_proxiesc             C   s   t    } t |  |  S)N)r5  r3  )rr   r1  rI   rI   rJ   proxy_bypass_macosx_sysconfo	  s    	r7  c               C   s   t    S)zReturn a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )r6  rI   rI   rI   rJ   getproxies_macosx_sysconfs	  s    r8  c             C   s!   t    r t |   St |   Sd  S)N)r$  r(  r7  )rr   rI   rI   rJ   r  }	  s    	
r  c               C   s   t    p t   S)N)r$  r8  rI   rI   rI   rJ   r4   	  s    c              C   sl  i  }  y d d l  } Wn t k
 r. |  SYn Xy| j | j d  } | j | d  d } | r?t | j | d  d  } d | k r x | j d  D]M } | j d d  \ } } t j d	 |  s d
 | | f } n  | |  | <q Wq?| d d  d k r| |  d <q?d | |  d <d | |  d <d | |  d <n  | j	   Wn t
 t t f k
 rgYn X|  S)zxReturn a dictionary of scheme -> proxy server URL mappings.

        Win32 uses the registry to store proxies.

        r   Nz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsProxyEnableZProxyServerr  r  rN   z^([^/:]+)://z%s://%srI  zhttp:r   z	http://%sz
https://%sr   zftp://%sr   )winregImportErrorOpenKeyHKEY_CURRENT_USERQueryValueExr   r1  r?  r  ZCloseri   r@   r   )r  r:  internetSettingsproxyEnableZproxyServerpr   ZaddressrI   rI   rJ   getproxies_registry	  s6    		rB  c               C   s   t    p t   S)zReturn a dictionary of scheme -> proxy server URL mappings.

        Returns settings gathered from the environment, if specified,
        or the registry.

        )r$  rB  rI   rI   rI   rJ   r4   	  s    c          &   C   s  y d d  l  } Wn t k
 r( d SYn XyK | j | j d  } | j | d  d } t | j | d  d  } Wn t k
 r d SYn X| s | r d St |   \ } } | g }  y/ t j	 |  } | | k r |  j
 |  n  Wn t k
 r Yn Xy/ t j |  } | | k r,|  j
 |  n  Wn t k
 rAYn X| j d  } x | D] }	 |	 d k r}d | k r}d Sn  |	 j d d	  }	 |	 j d
 d  }	 |	 j d d  }	 x* |  D]" }
 t j |	 |
 t j  rd SqWqXWd S)Nr   z;Software\Microsoft\Windows\CurrentVersion\Internet Settingsr9  ZProxyOverrider  z<local>r*  rN   z\.r&  z.*?)r:  r;  r<  r=  r>  r   ri   r   r   r  rY   Zgetfqdnr1  r   r?  r  rA  )rr   r:  r?  r@  ZproxyOverrideZrawHostr)  ZaddrZfqdnr-  r   rI   rI   rJ   proxy_bypass_registry	  sP    				rD  c             C   s!   t    r t |   St |   Sd S)zReturn a dictionary of scheme -> proxy server URL mappings.

        Returns settings gathered from the environment, if specified,
        or the registry.

        N)r$  r(  rD  )rr   rI   rI   rJ   r  	  s    	
)}r   r  r   r  rT  Zhttp.clientr   r  rS   r*  r?  r   r  rR  rn  rV   rP   r5  Zurllib.errorr   r   r   Zurllib.parser   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Zurllib.responser   r   rA   r;  r?   __all__r  r   rC   r   r/   r0   rX   r5   r6   r@  ASCIIrn   rs   r   r   r1   r   r.   r   r   r  r   r    r!   r"   r#   r$   urandomrS  r%   r&   r'   rh  r(   r   r   rB   rY   r   r-   rK  rM  r)   r  r*   r+   r,   r  rW   Z
nturl2pathr3   r2   r  r7   r8   r  r  r  r  r  r  r  r  r  r$  r(  r3  platformZ_scproxyr5  r6  r7  r8  r  r4   rB  rD  rI   rI   rI   rJ   <module>D   s   v					?n$h*@
7	q
+4:5! 

[<
-	2