Your IP : 3.138.102.163


Current Path : /opt/alt/python34/lib64/python3.4/urllib/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/urllib/__pycache__/request.cpython-34.pyc

�
e f�l�(@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(ddl)m*Z*m+Z+yddl,Z,Wne-k
r�dZ.YnXdZ.dd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'g Z/ej0dd(�Z1da2dej3d)dd*dd+dd,dd-d�Z4d.d�Z5gZ6dddd/d$�Z7d0d%�Z8e
j9d1e
j:�Z;d2d3�Z<Gd4d�d�Z=Gd5d	�d	�Z>d6d �Z?Gd7d
�d
�Z@Gd8d�de@�ZAGd9d�de@�ZBGd:d�de@�ZCd;d<�ZDGd=d�de@�ZEGd>d�d�ZFGd?d�deF�ZGGd@d�d�ZHGdAd�deHe@�ZIGdBd�deHe@�ZJejKZLGdCd�d�ZMGdDd�de@eM�ZNGdEd�de@eM�ZOGdFdG�dGe@�ZPGdHd�deP�ZQeRejSdI�rhGdJdK�dKeP�ZTe/jUdK�nGdLd
�d
e@�ZVGdMd�de@�ZWdNdO�ZXdPdQ�ZYGdRd�de@�ZZdSdT�Z[GdUd�de@�Z\GdVd�de\�Z]GdWd�de@�Z^dXZ_ej`dYkr>ddZlambZbmcZcnd[d"�Zbd\d!�ZciZdGd]d&�d&�ZeGd^d'�d'ee�Zfdagd_d`�Zhdaidadb�Zjdakdcdd�Zldamdedf�ZnGdgdh�dh�Zodidj�Zpdkdl�Zqdmdn�Zrejsdokr\ddpltmuZumvZvdqdr�Zwdsdt�Zxdudv�Zydwd#�ZznNej`dYkr�dxdy�Z{dzd#�Zzd{d|�Z|d}dv�ZynepZzeqZydS)~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�	cadefault�contextc	Cs�|s|s|r�|dk	r-td��ntsBtd��ntjtjjd|d|�}td|�}t|�}nF|r�td|�}t|�}n"tdkr�t�a}nt}|j	|||�S)NzDYou can't pass both context and any of cafile, capath, and cadefaultzSSL support not availabler:r;r=)
�
ValueError�	_have_ssl�sslZcreate_default_contextZPurposeZSERVER_AUTH�HTTPSHandlerr1�_opener�open)	�url�data�timeoutr:r;r<r=Z
https_handler�opener�rH�3/opt/alt/python34/lib64/python3.4/urllib/request.pyr/�s$	cCs
|adS)N)rB)rGrHrHrIr0�scCs�t|�\}}tjt||���E}|j�}|dkrb|rbtjj|�|fS|rzt|d�}n(t	j
dd�}|j}tj
|�|��||f}	d
}
d}d}d}
d	|kr�t|d
�}n|r||
|
|�nx]|j|
�}|s!Pn|t|�7}|j|�|
d7}
|r||
|
|�qqWWdQXWdQX|dkr�||kr�td||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�wbZdeleteFi��rzcontent-lengthzContent-LengthNz1retrieval incomplete: got only %i out of %i bytesi ���)r�
contextlib�closingr/�info�os�path�normpathrC�tempfileZNamedTemporaryFile�name�_url_tempfiles�append�int�read�len�writer)rD�filename�
reporthookrEZurl_typerS�fp�headers�tfp�result�bs�sizerZ�blocknum�blockrHrHrIr5�sD	


#cCsWx4tD],}ytj|�Wqtk
r2YqXqWtdd�=trSdandS)z0Clean up temporary files from urlretrieve calls.N)rWrR�unlink�OSErrorrB)Z	temp_filerHrHrIr6�s

	
z:\d+$cCsY|j}t|�d}|dkr:|jdd�}ntjd|d�}|j�S)z�Return request-host, as defined by RFC 2965.

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

    rM��Host)�full_urlr�
get_header�_cut_port_re�sub�lower)�requestrD�hostrHrHrI�request_host�s	rrc@s*eZdZdiddddd�Zedd��Zejdd��Zejdd��Zed	d
��Zejdd
��Zejdd
��Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zddd�Zdd �Zd!d"�ZdS)#rNFc	Cs�||_i|_i|_d|_||_d|_x*|j�D]\}}|j||�qCW|dkr~t|�}n||_	||_
|r�||_ndS)N)rkr`�unredirected_hdrs�_datarE�_tunnel_host�items�
add_headerrr�origin_req_host�unverifiable�method)	�selfrDrEr`rxryrz�key�valuerHrHrI�__init__s								zRequest.__init__cCs&|jrdj|j|j�S|jS)Nz{}#{})�fragment�format�	_full_url)r{rHrHrIrks	zRequest.full_urlcCs8t|�|_t|j�\|_|_|j�dS)N)rr�rr�_parse)r{rDrHrHrIrk scCsd|_d|_d|_dS)Nri)r�r�selector)r{rHrHrIrk's		cCs|jS)N)rt)r{rHrHrIrE-szRequest.datacCs>||jkr:||_|jd�r:|jd�q:ndS)NzContent-length)rt�
has_header�
remove_header)r{rErHrHrIrE1s	cCs
d|_dS)N)rE)r{rHrHrIrE;scCswt|j�\|_}|jdkr=td|j��nt|�\|_|_|jrst|j�|_ndS)Nzunknown url type: %r)	rr��typer>rkrrqr�r
)r{�restrHrHrIr�?s	zRequest._parsecCs+|jdk	rdnd}t|d|�S)z3Return a string indicating the HTTP request method.N�POST�GETrz)rE�getattr)r{Zdefault_methodrHrHrI�
get_methodGszRequest.get_methodcCs|jS)N)rk)r{rHrHrI�get_full_urlLszRequest.get_full_urlcCsJ|jdkr(|jr(|j|_n||_|j|_||_dS)N�https)r�rurqrkr�)r{rqr�rHrHrI�	set_proxyOs
	zRequest.set_proxycCs|j|jkS)N)r�rk)r{rHrHrI�	has_proxyWszRequest.has_proxycCs||j|j�<dS)N)r`�
capitalize)r{r|�valrHrHrIrwZszRequest.add_headercCs||j|j�<dS)N)rsr�)r{r|r�rHrHrI�add_unredirected_header^szRequest.add_unredirected_headercCs||jkp||jkS)N)r`rs)r{�header_namerHrHrIr�bszRequest.has_headercCs"|jj||jj||��S)N)r`�getrs)r{r��defaultrHrHrIrlfs	zRequest.get_headercCs*|jj|d�|jj|d�dS)N)r`�poprs)r{r�rHrHrIr�kszRequest.remove_headercCs/|jj�}|j|j�t|j��S)N)rs�copy�updater`�listrv)r{�hdrsrHrHrI�header_itemsoszRequest.header_items)�__name__�
__module__�__qualname__r~�propertyrk�setter�deleterrEr�r�r�r�r�rwr�r�rlr�r�rHrHrHrIrs(
c@speZdZdd�Zdd�Zdd�Zdd�Zd	ejd
d�Z	d	dd
�Z
dd�Zd	S)rcCsMdt}d|fg|_g|_i|_i|_i|_i|_dS)NzPython-urllib/%sz
User-agent)�__version__�
addheaders�handlers�handle_open�handle_error�process_response�process_request)r{Zclient_versionrHrHrIr~us
				zOpenerDirector.__init__cCs�t|d�s(tdt|���nd}xxt|�D]j}|dkrSq;n|jd�}|d|�}||dd�}|jd	�r
|jd�|d}||dd�}yt|�}Wntk
r�YnX|jj	|i�}	|	|j|<n]|d
kr(|}|j
}	n?|dkrF|}|j}	n!|dkr;|}|j}	nq;|	j
|g�}
|
r�tj|
|�n
|
j|�d
}q;W|r�tj|j|�|j|�ndS)N�
add_parentz%expected BaseHandler instance, got %rF�redirect_request�do_open�
proxy_open�_rM�errorrC�responserpT)zredirect_requestzdo_openz
proxy_open)�hasattr�	TypeErrorr��dir�find�
startswithrYr>r�r�r�r�r��
setdefault�bisectZinsortrXr�r�)r{�handlerZadded�meth�i�protocolZ	condition�j�kind�lookupr�rHrHrI�add_handler�sJ


zOpenerDirector.add_handlercCsdS)NrH)r{rHrHrI�close�szOpenerDirector.closec	GsR|j|f�}x9|D]1}t||�}||�}|dk	r|SqWdS)N)r�r�)	r{�chainr��	meth_name�argsr�r��funcrbrHrHrI�_call_chain�s
zOpenerDirector._call_chainNc
Cs�t|t�r!t||�}n|}|dk	r?||_n||_|j}|d}x8|jj|g�D]!}t||�}||�}qqW|j	||�}	|d}x;|j
j|g�D]$}t||�}|||	�}	q�W|	S)NZ_requestZ	_response)�
isinstance�strrrErFr�r�r�r��_openr�)
r{�fullurlrErF�reqr�r�Z	processorr�r�rHrHrIrC�s"		

zOpenerDirector.opencCsp|j|jdd|�}|r%|S|j}|j|j||d|�}|rW|S|j|jdd|�S)Nr�Zdefault_openr��unknown�unknown_open)r�r�r�)r{r�rErbr�rHrHrIr��s	
zOpenerDirector._opencGs�|d
kr<|jd}|d}d|}d}|}n|j}|d}d}|||f|}|j|�}|r�|S|r�|dd	f|}|j|�SdS)N�httpr��z
http_error_%srMZ_errorrr��http_error_default)zhttpzhttps)r�r�)r{�protor��dictr�Zhttp_errZ	orig_argsrbrHrHrIr��s 


		
zOpenerDirector.error)r�r�r�r~r�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrCr�r�rHrHrHrIrts/c	GsCt�}ttttttttt	g	}t
tjd�rL|j
t�nt�}xo|D]g}x^|D]V}t|t�r�t||�r�|j|�q�qit||�ri|j|�qiqiWq\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)rrr-r(rrr*r)r.r,r�r��clientrXrA�setr�r��
issubclass�add�remover�)r�rGZdefault_classes�skip�klassZcheck�hrHrHrIr1s0						




c@s:eZdZdZdd�Zdd�Zdd�ZdS)	ri�cCs
||_dS)N)�parent)r{r�rHrHrIr�)szBaseHandler.add_parentcCsdS)NrH)r{rHrHrIr�,szBaseHandler.closecCs#t|d�sdS|j|jkS)N�
handler_orderT)r�r�)r{�otherrHrHrI�__lt__0szBaseHandler.__lt__N)r�r�r�r�r�r�r�rHrHrHrIr&sc@s.eZdZdZdZdd�ZeZdS)r.zProcess HTTP error responses.i�cCsd|j|j|j�}}}d|ko7dkns`|jjd|||||�}n|S)N��i,r�)�code�msgrQr�r�)r{rpr�r�r�r�rHrHrI�
http_response=s
 	z HTTPErrorProcessor.http_responseN)r�r�r��__doc__r�r��https_responserHrHrHrIr.9sc@seZdZdd�ZdS)rcCst|j||||��dS)N)rrk)r{r�r_r�r�r�rHrHrIr�Ksz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�r�rHrHrHrIrJsc@sHeZdZdZdZdd�Zdd�ZeZZZ	dZ
dS)	r��
c	s�|j�}|dkr$|dkp9|dko9|dksZt|j||||��n|jdd	�}d�t�fdd
�|jj�D��}t|d|d|jdd�S)a�Return a Request or None in response to a redirect.

        This is called by the http_error_30x methods when a
        redirection response is received.  If a redirection should
        take place, return a new Request to allow http_error_30x to
        perform the redirect.  Otherwise, raise HTTPError if no-one
        else should try to handle this url.  Return None if you can't
        but another Handler might.
        �-�.�/�3r��HEADr�� z%20�content-length�content-typec3s3|])\}}|j��kr||fVqdS)N)ro)�.0�k�v)�CONTENT_HEADERSrHrI�	<genexpr>ms	z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>r`rxryT)r�r�r�r�)zGETr�)r�r�r�)r�r�)	r�rrk�replacer�r`rvrrx)	r{r�r_r�r�r`�newurl�mZ
newheadersrH)r�rIr�Vs
(		z$HTTPRedirectHandler.redirect_requestc
Cs�d|kr|d}nd|kr2|d}ndSt|�}|jdkrvt||d||f||��n|js�t|�}d|d	<nt|�}t|j|�}|j||||||�}|dkr�dSt	|d
�r[|j
}	|_
|	j|d�|jks3t
|	�|jkrot|j||j|||��qoni}	|_
|_
|	j|d�d|	|<|j�|j�|jj|d
|j�S)N�location�urir�r��ftpriz+%s - Redirection to url '%s' is not allowed�/r��
redirect_dictrrMrF)zhttpzhttpszftpri)r�schemerrSr�rrrkr�r�r�r��max_repeatsr[�max_redirections�inf_msgrZr�r�rCrF)
r{r�r_r�r�r`r��urlparts�newZvisitedrHrHrI�http_error_302xs<


	


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_307rrHrHrHrIrNs"7c	Cs�t|�\}}|jd�s0d}|}nY|jd�sRtd|��n|jdd�}|dkryd}n|d|�}t|�\}}|dk	r�t|�\}}n
d}}||||fS)aReturn (scheme, user, password, host/port) given a URL or an authority.

    If a URL is supplied, it must have an authority (host:port) component.
    According to RFC 3986, having an authority component means the URL must
    have two slashes after the scheme.
    r�Nz//zproxy URL with no authority: %rr�rMrN)rr�r>r�rr)	�proxyr�Zr_scheme�	authority�endZuserinfo�hostport�user�passwordrHrHrI�_parse_proxy�s		
r
c@s1eZdZdZddd�Zdd�ZdS)r�dNc	Cs�|dkrt�}nt|d�s3td��||_x@|j�D]2\}}t|d||||jdd��qIWdS)N�keyszproxies must be a mappingz%s_opencSs||||�S)NrH)�rrr�r�rHrHrI�<lambda>�sz'ProxyHandler.__init__.<locals>.<lambda>)r4r��AssertionError�proxiesrv�setattrr�)r{rr�rDrHrHrIr~�s	
zProxyHandler.__init__cCs|j}t|�\}}}}|dkr6|}n|jrRt|j�rRdS|r�|r�dt|�t|�f}	tj|	j��jd�}
|j	dd|
�nt|�}|j
||�||ks�|dkr�dS|jj|d|j
�SdS)Nz%s:%s�asciizProxy-authorizationzBasic r�rF)r�r
rq�proxy_bypassr
�base64�	b64encode�encode�decoderwr�r�rCrF)r{r�rr�Z	orig_typeZ
proxy_typerrr
Z	user_passZcredsrHrHrIr��s 		zProxyHandler.proxy_open)r�r�r�r�r~r�rHrHrHrIr�s
c@sOeZdZdd�Zdd�Zdd�Zddd	�Zd
d�ZdS)
r cCs
i|_dS)N)�passwd)r{rHrHrIr~�szHTTPPasswordMgr.__init__cs�t|t�r|g}n|�jkr:i�j|<nxGdD]?�t��fdd�|D��}||f�j||<qAWdS)NTFcs"g|]}�j|���qSrH)�
reduce_uri)r��u)�default_portr{rHrI�
<listcomp>
s	z0HTTPPasswordMgr.add_password.<locals>.<listcomp>)TF)r�r�r�tuple)r{�realmr�rrZreduced_urirH)rr{rI�add_passwords
zHTTPPasswordMgr.add_passwordc	Cs�|jj|i�}xddD]\}|j||�}xA|j�D]3\}}x$|D]}|j||�rT|SqTWqAWqWdS)NTF)TF)NN)rr�rrv�	is_suburi)	r{r!�authuriZdomainsrZreduced_authuriZurisZauthinfor�rHrHrI�find_user_password
s

z"HTTPPasswordMgr.find_user_passwordTc
Cs�t|�}|dr=|d}|d}|dp7d}nd}|}d}t|�\}}|r�|dkr�|dk	r�idd6dd	6j|�}	|	dk	r�d
||	f}q�n||fS)z@Accept authority or URI and extract only the authority and path.rMrr�r�N�Pr�i�r�z%s:%d)rr
r�)
r{r�r�partsr�rrSrq�portZdportrHrHrIrs 




	zHTTPPasswordMgr.reduce_uricCsi||krdS|d|dkr(dStj|d|df�}t|�t|d�kredSdS)zcCheck if test is below base in a URI tree

        Both args must be URIs in reduced form.
        TrFrM)�	posixpath�commonprefixr[)r{�base�testZcommonrHrHrIr#.szHTTPPasswordMgr.is_suburiN)r�r�r�r~r"r%rr#rHrHrHrIr �s

c@seZdZdd�ZdS)r!cCsDtj|||�\}}|dk	r1||fStj|d|�S)N)r r%)r{r!r$rrrHrHrIr%?s

z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r�r�r�r%rHrHrHrIr!=sc@sLeZdZejdej�Zddd�Zdd�Zdd�Z	dS)	r"z1(?:.*,)*[ 	]*([^ 	]+)[ 	]+realm=(["']?)([^"']*)\2NcCs4|dkrt�}n||_|jj|_dS)N)r rr")r{Zpassword_mgrrHrHrIr~Us	z!AbstractBasicAuthHandler.__init__c	Cs�|j|d�}|r�|j�d}|j�dkrMtd|��q�tjj|�}|r�|j�\}}}|dkr�tj	dt
d�n|j�dkr�|j|||�Sq�ndS)	Nr�basiczDAbstractBasicAuthHandler does not support the following scheme: '%s'�"�'zBasic Auth Realm was unquotedr�)r.r/)r��splitror>r"�rx�search�groups�warnings�warn�UserWarning�retry_http_basic_auth)	r{�authreqrqr�r`r�Zmor	r!rHrHrI�http_error_auth_reqed[s
	
z.AbstractBasicAuthHandler.http_error_auth_reqedcCs�|jj||�\}}|dk	r�d||f}dtj|j��jd�}|j|jd�|krxdS|j|j|�|j	j
|d|j�SdSdS)Nz%s:%szBasic rrF)rr%rrrrrl�auth_headerr�r�rCrF)r{rqr�r!r�pw�raw�authrHrHrIr7qs"z.AbstractBasicAuthHandler.retry_http_basic_auth)
r�r�r��re�compile�Ir1r~r9r7rHrHrHrIr"Gs
	c@s"eZdZdZdd�ZdS)r#�
AuthorizationcCs%|j}|jd|||�}|S)Nzwww-authenticate)rkr9)r{r�r_r�r�r`rDr�rHrHrI�http_error_401�s		z#HTTPBasicAuthHandler.http_error_401N)r�r�r�r:rBrHrHrHrIr#~sc@s"eZdZdZdd�ZdS)r$zProxy-authorizationcCs%|j}|jd|||�}|S)Nzproxy-authenticate)rqr9)r{r�r_r�r�r`rr�rHrHrI�http_error_407�s		z$ProxyBasicAuthHandler.http_error_407N)r�r�r�r:rCrHrHrHrIr$�sc@sseZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r%NcCsO|dkrt�}n||_|jj|_d|_d|_d|_dS)Nr)r rr"�retried�nonce_count�
last_nonce)r{rrHrHrIr~�s			z"AbstractDigestAuthHandler.__init__cCs
d|_dS)Nr)rD)r{rHrHrI�reset_retry_count�sz+AbstractDigestAuthHandler.reset_retry_countcCs�|j|d�}|jdkr?t|jdd|d��n|jd7_|r�|j�d}|j�dkr�|j||�S|j�dkr�td|��q�ndS)	N�i�zdigest auth failedrMrZdigestr-zEAbstractDigestAuthHandler does not support the following scheme: '%s')r�rDrrkr0ro�retry_http_digest_authr>)r{r:rqr�r`r8r�rHrHrIr9�sz/AbstractDigestAuthHandler.http_error_auth_reqedcCs�|jdd�\}}ttdt|���}|j||�}|r�d|}|jj|jd�|krwdS|j|j|�|j	j
|d|j�}|SdS)Nr�rMz	Digest %srF)r0�parse_keqv_list�filter�parse_http_list�get_authorizationr`r�r:r�r�rCrF)r{r�r=�tokenZ	challenge�chalZauth_valZresprHrHrIrI�s
z0AbstractDigestAuthHandler.retry_http_digest_authcCsXd|j|tj�f}|jd�td�}tj|�j�}|dd�S)Nz	%s:%s:%s:rrL�)rE�timeZctimer�_randombytes�hashlib�sha1�	hexdigest)r{�nonce�s�b�digrHrHrI�
get_cnonce�sz$AbstractDigestAuthHandler.get_cnoncecCs_yK|d}|d}|jd�}|jdd�}|jdd�}Wntk
rcdSYnX|j|�\}}	|dkr�dS|jj||j�\}
}|
dkr�dS|jdk	r�|j|j|�}nd}d|
||f}
d|j�|j	f}|d	kr�||j
kr=|jd
7_nd
|_||_
d|j}|j|�}d||||||�f}|	||
�|�}nD|dkr�|	||
�d|||�f�}nt
d
|��d|
|||j	|f}|r|d|7}n|r0|d|7}n|d|7}|r[|d||f7}n|S)Nr!rV�qop�	algorithm�MD5�opaquez%s:%s:%sz%s:%sr=rMz%08xz%s:%s:%s:%s:%szqop '%s' is not supported.z>username="%s", realm="%s", nonce="%s", uri="%s", response="%s"z
, opaque="%s"z
, digest="%s"z, algorithm="%s"z, qop=auth, nc=%s, cnonce="%s")r��KeyError�get_algorithm_implsrr%rkrE�get_entity_digestr�r�rFrErZr)r{r�rOr!rVr[r\r^�H�KDrr;ZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr+rHrHrIrM�sV


	
		
(
z+AbstractDigestAuthHandler.get_authorizationcsR|dkrdd��n|dkr6dd��n�fdd�}�|fS)Nr]cSstj|jd��j�S)Nr)rSZmd5rrU)�xrHrHrIrsz?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAcSstj|jd��j�S)Nr)rSrTrrU)rdrHrHrIrscs�d||f�S)Nz%s:%srH)rW�d)rbrHrIr!srH)r{r\rcrH)rbrIr`sz-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrH)r{rErOrHrHrIra$sz+AbstractDigestAuthHandler.get_entity_digest)r�r�r�r~rGr9rIrZrMr`rarHrHrHrIr%�s	<
c@s.eZdZdZdZdZdd�ZdS)r&z�An authentication protocol defined by RFC 2069

    Digest authentication improves on basic authentication because it
    does not transmit passwords in the clear.
    rAi�cCs9t|j�d}|jd|||�}|j�|S)NrMzwww-authenticate)rrkr9rG)r{r�r_r�r�r`rq�retryrHrHrIrB3s
	
z$HTTPDigestAuthHandler.http_error_401N)r�r�r�r�r:r�rBrHrHrHrIr&)sc@s(eZdZdZdZdd�ZdS)r'zProxy-Authorizationi�cCs/|j}|jd|||�}|j�|S)Nzproxy-authenticate)rqr9rG)r{r�r_r�r�r`rqrfrHrHrIrC@s
		
z%ProxyDigestAuthHandler.http_error_407N)r�r�r�r:r�rCrHrHrHrIr';sc@sCeZdZddd�Zdd�Zdd�Zdd	�Zd
S)�AbstractHTTPHandlerrcCs
||_dS)N)�_debuglevel)r{�
debuglevelrHrHrIr~IszAbstractHTTPHandler.__init__cCs
||_dS)N)rh)r{�levelrHrHrI�set_http_debuglevelLsz'AbstractHTTPHandler.set_http_debuglevelc
Cs�|j}|std��n|jdk	r|j}t|t�rZd}t|��n|jd�s||jdd�n|jd�syt|�}WnCtk
r�t|t	j
�r�tdt|�|f��nYqX|jddt
|�|j�qn|}|j�rDt|j�\}}t|�\}}	n|jd�sf|jd|�nxH|jjD]:\}
}|
j�}
|j|
�ss|j|
|�qsqsW|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-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drj)rqrrEr�r�r�r�r��
memoryview�collections�Iterabler>r�r[�itemsizer�rr�rr�r�r�)r{rprqrEr�ZmvZsel_hostr�ZselZsel_pathrVr}rHrHrI�do_request_OsB		

	!zAbstractHTTPHandler.do_request_c
s�|j}|std��n||d|j|�}t|j���jt�fdd�|jj�D���d�d<tdd��j�D���|jr�i}d}|�kr��|||<�|=n|j	|jd	|�nyjy&|j
|j�|j|j
��Wn1tk
rK}zt|��WYd
d
}~XnX|j�}	Wn|j��YnX|jr�|jj�d
|_n|j�|	_|	j|	_|	S)z�Return an HTTPResponse object for the request, using http_class.

        http_class must implement the HTTPConnection API from http.client.
        z
no host givenrFc3s-|]#\}}|�kr||fVqdS)NrH)r�r�r�)r`rHrIr��s	z.AbstractHTTPHandler.do_open.<locals>.<genexpr>r��
Connectioncss'|]\}}|j�|fVqdS)N)�title)r�rVr�rHrHrIr��szProxy-Authorizationr`N)rqrrFr�rsr�r`rvruZ
set_tunnelrpr�r�rErh�getresponser�Zsockr�rD�reasonr�)
r{Z
http_classr�Zhttp_conn_argsrqr�Ztunnel_headersZproxy_auth_hdr�errrrH)r`rIr�ws<	/
	
&
	
zAbstractHTTPHandler.do_openN)r�r�r�r~rkrpr�rHrHrHrIrgGs(rgc@s%eZdZdd�ZejZdS)r(cCs|jtjj|�S)N)r�r�r��HTTPConnection)r{r�rHrHrI�	http_open�szHTTPHandler.http_openN)r�r�r�rwrgrp�http_requestrHrHrHrIr(�sr�c@s:eZdZddddd�Zdd�ZejZdS)rArNcCs&tj||�||_||_dS)N)rgr~�_context�_check_hostname)r{rir=�check_hostnamerHrHrIr~�s	zHTTPSHandler.__init__cCs(|jtjj|d|jd|j�S)Nr=r{)r�r�r�r�ryrz)r{r�rHrHrI�
https_open�szHTTPSHandler.https_open)r�r�r�r~r|rgrp�
https_requestrHrHrHrIrA�srAc@sCeZdZddd�Zdd�Zdd�ZeZeZdS)rNcCs7ddl}|dkr*|jj�}n||_dS)Nr)Zhttp.cookiejar�	cookiejarZ	CookieJar)r{r~r�rHrHrIr~�szHTTPCookieProcessor.__init__cCs|jj|�|S)N)r~Zadd_cookie_header)r{rprHrHrIrx�sz HTTPCookieProcessor.http_requestcCs|jj||�|S)N)r~Zextract_cookies)r{rpr�rHrHrIr��sz!HTTPCookieProcessor.http_response)r�r�r�r~rxr�r}r�rHrHrHrIr�s
c@seZdZdd�ZdS)r-cCs|j}td|��dS)Nzunknown url type: %s)r�r)r{r�r�rHrHrIr��s	zUnknownHandler.unknown_openN)r�r�r�r�rHrHrHrIr-�scCspi}xc|D][}|jdd�\}}|ddkr^|ddkr^|dd�}n|||<q
W|S)z>Parse list of key=value strings where keys are not duplicated.�=rMrr.rNrN)r0)�lZparsedZeltr�r�rHrHrIrJ�s
 rJcCs�g}d}d}}x�|D]�}|r?||7}d}qn|r�|dkr]d}qn|dkrrd}n||7}qn|dkr�|j|�d}qn|dkr�d}n||7}qW|r�|j|�ndd�|D�S)	apParse lists as described by RFC 2068 Section 2.

    In particular, parse comma-separated lists where the elements of
    the list may include quoted-strings.  A quoted-string could
    contain a comma.  A non-quoted string could have quotes in the
    middle.  Neither commas nor quotes count if they are escaped.
    Only double-quotes count, not single-quotes.
    riF�\Tr.�,cSsg|]}|j��qSrH)�strip)r��partrHrHrIrs	z#parse_http_list.<locals>.<listcomp>)rX)rW�resr��escaper	ZcurrHrHrIrL�s4	


	

	rLc@s:eZdZdd�ZdZdd�Zdd�ZdS)r)cCs�|j}|dd�dkrt|dd�dkrt|jrt|jdkrt|j|j�kr�td��q�n
|j|�SdS)Nr�z//r9r��	localhostz-file:// scheme is supported only on localhost)r�rq�	get_namesr�open_local_file)r{r�rDrHrHrI�	file_opens	5zFileHandler.file_openNcCs}tjdkrvy7ttjd�dtjtj��d�t_Wqvtjk
rrtjd�ft_YqvXntjS)Nr�r�)r)�namesr r��gethostbyname_ex�gethostname�gaierror�
gethostbyname)r{rHrHrIr�)s$zFileHandler.get_namescCsaddl}ddl}|j}|j}t|�}y�tj|�}|j}|jj	|j
dd�}	|j|�d}
|jd|
p�d||	f�}|r�t
|�\}}n|s�|rt|�|j�kr|r�d||}
n
d|}
tt|d�||
�SWn1tk
rP}zt|��WYdd}~XnXtd��dS)	Nr�usegmtTz6Content-type: %s
Content-length: %d
Last-modified: %s
z
text/plainzfile://�rbzfile not on local host)�email.utils�	mimetypesrqr�r3rR�stat�st_size�utils�
formatdate�st_mtime�
guess_type�message_from_stringr
�_safe_gethostbynamer�rrCrhr)r{r��emailr�rqr]Z	localfile�statsrd�modified�mtyper`r(Zorigurl�exprHrHrIr�4s0			
zFileHandler.open_local_file)r�r�r�r�r�r�r�rHrHrHrIr)s
cCs1ytj|�SWntjk
r,dSYnXdS)N)r�r�r�)rqrHrHrIr�Psr�c@s(eZdZdd�Zdd�ZdS)r*cCs�ddl}ddl}|j}|s6td��nt|�\}}|dkr`|j}nt|�}t|�\}}|r�t|�\}}nd}t	|�}|p�d}|p�d}yt
j|�}Wn1tk
r	}zt|��WYdd}~XnXt
|j�\}	}
|	jd�}ttt	|��}|dd�|d}}|r�|dr�|dd�}ny|j||||||j�}
|r�dp�d}xM|
D]E}t|�\}}|j�dkr�|dkr�|j�}q�q�W|
j||�\}}d}|j|j�d}|rS|d
|7}n|dk	r||dkr||d|7}ntj|�}t|||j�SWnQ|jk
r�}z.td|�}|jtj �d��WYdd}~XnXdS)Nrzftp error: no host givenrir�rMr@�Dr��a�Ar�rezContent-type: %s
zContent-length: %d
z
ftp error: %rr�rNrN)r�r�r�r@rer�)!�ftplibr�rqrr
�FTP_PORTrYrrr
r�r�rhrr�r0r��map�connect_ftprFrro�upper�retrfiler�rkr�r�r�
all_errors�with_traceback�sys�exc_info)r{r�r�r�rqr(rrr�rS�attrs�dirsrJZfwr��attrr}r_�retrlenr`r�r��excrHrHrI�ftp_openWs\	!
zFTPHandler.ftp_openc	Cst||||||dd�S)N�
persistentF)�
ftpwrapper)r{rrrqr(r�rFrHrHrIr��szFTPHandler.connect_ftpN)r�r�r�r�r�rHrHrHrIr*Vs5c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r+cCs1i|_i|_d|_d|_d|_dS)Nr�<rP)�cacherF�soonest�delay�	max_conns)r{rHrHrIr~�s
				zCacheFTPHandler.__init__cCs
||_dS)N)r�)r{�trHrHrI�
setTimeout�szCacheFTPHandler.setTimeoutcCs
||_dS)N)r�)r{r�rHrHrI�setMaxConns�szCacheFTPHandler.setMaxConnscCs�|||dj|�|f}||jkrJtj�|j|j|<n<t||||||�|j|<tj�|j|j|<|j�|j|S)Nr�)�joinr�rQr�rFr��check_cache)r{rrrqr(r�rFr|rHrHrIr��s
zCacheFTPHandler.connect_ftpcCs tj�}|j|krxxZt|jj��D]@\}}||kr1|j|j�|j|=|j|=q1q1Wntt|jj���|_t	|j�|j
krxJt|jj��D]3\}}||jkr�|j|=|j|=Pq�q�Wtt|jj���|_ndS)N)rQr�r�rFrvr�r��min�valuesr[r�)r{r�r�r�rHrHrIr��s"
"

zCacheFTPHandler.check_cachecCsBx!|jj�D]}|j�qW|jj�|jj�dS)N)r�r�r��clearrF)r{�connrHrHrI�clear_cache�s
zCacheFTPHandler.clear_cacheN)	r�r�r�r~r�r�r�r�r�rHrHrHrIr+�sc@seZdZdd�ZdS)r,cCs�|j}|jdd�\}}|jdd�\}}t|�}|jd�rvtj|�}|dd�}n|s�d}ntjd|t|�f�}t	t
j|�||�S)	N�:rMr�z;base64�ztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
i����)rkr0r�endswithr�decodebytesr�r�r[r�io�BytesIO)r{r�rDr�rEZ	mediatyper`rHrHrI�	data_open�s
			zDataHandler.data_openN)r�r�r�r�rHrHrHrIr,�sr��nt)r3r2cCs
t|�S)zOS-specific conversion from a relative URL of the 'file' scheme
        to a file system path; not recommended for general use.)r
)�pathnamerHrHrIr3�scCs
t|�S)zOS-specific conversion from a file system path to a relative URL
        of the 'file' scheme; not recommended for general use.)r	)r�rHrHrIr2�sc@s4eZdZdZdZdeZddd�Zdd�Zdd	�Z	d
d�Z
dd
�Zddd�Zddd�Z
ddd�Zddddd�Zdd�Zddd�Zddd�Zdd�Zer�dd�Zdd d!�Znd"d#�Zd$d%�Zd&d'�Zdd(d)�ZdS)*r7a,Class to open URLs.
    This is a class rather than just a subroutine because we may need
    more than one set of global protocol-specific options.
    Note -- this is a base class for those who don't want the
    automatic handling of errors type 302 (relocated) and 401
    (authorization needed).NzPython-urllib/%scKs�di|jjd6}tj|tdd�|dkrEt�}nt|d�s`td��||_|j	d�|_
|j	d�|_d	|jfg|_
g|_tj|_d|_t|_dS)
NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methods�class�
stacklevelr9rzproxies must be a mapping�key_file�	cert_filez
User-Agent)�	__class__r�r4r5�DeprecationWarningr4r�rrr�r�r��versionr��_URLopener__tempfilesrRrg�_URLopener__unlink�	tempcache�ftpcache)r{rZx509r�rHrHrIr~s			zURLopener.__init__cCs|j�dS)N)r�)r{rHrHrI�__del__%szURLopener.__del__cCs|j�dS)N)�cleanup)r{rHrHrIr�(szURLopener.closecCss|jrVx7|jD],}y|j|�Wqtk
r>YqXqW|jdd�=n|jro|jj�ndS)N)r�r�rhr�r�)r{rJrHrHrIr�+s	
		zURLopener.cleanupcGs|jj|�dS)zdAdd a header to be used by the HTTP interface only
        e.g. u.addheader('Accept', 'sound/basic')N)r�rX)r{r�rHrHrI�	addheader9szURLopener.addheadercCs�tt|��}t|dd�}|jrn||jkrn|j|\}}t|d�}t|||�St|�\}}|s�d}n||jkr�|j|}t|�\}}	t|	�\}
}|
|f}nd}d|}||_	|j
dd�}t||�sD|r1|j|||�S|j
||�Sny9|dkrft||�|�St||�||�SWnattfk
r��YnGtk
r�}
z'td	|
�jtj�d
��WYdd}
~
XnXdS)z6Use URLopener().open(file) instead of open(file, 'r').�safez%/:=&?~#+!$,;'@()*[]|r�rJNZopen_�-r�zsocket errorr�)rrr	r�rCrrrrr�r�r��open_unknown_proxy�open_unknownr�rrrhr�r�r�)r{r�rEr]r`r_�urltyperDr�	proxyhostrqr�rVr�rHrHrIrC?s<	

	zURLopener.opencCs(t|�\}}tdd|��dS)z/Overridable interface to open unknown URL type.z	url errorzunknown url typeN)rrh)r{r�rEr�rDrHrHrIr�cszURLopener.open_unknowncCs,t|�\}}tdd||��dS)z/Overridable interface to open unknown URL type.z	url errorzinvalid proxy for %sN)rrh)r{rr�rEr�rDrHrHrIr�hszURLopener.open_unknown_proxyc Cs�tt|��}|jr5||jkr5|j|St|�\}}|dkr�|sf|dkr�yC|j|�}|j�}|j�tt|�d�|fSWq�t	k
r�}	zWYdd}	~	Xq�Xn|j
||�}z�|j�}
|r
t
|d�}n�ddl}t|�\}
}t|p7d�\}
}t|pOd�\}}
t
|pgd�\}}
tjj|�d}|j|�\}}|jj|�tj|d�}z�||
f}|jdk	r�||j|<nd}d
}d}d}d	|
kr%t|
d
�}n|r>||||�nx]|j|�}|sZPn|t|�7}|j|�|d7}|rA||||�qAqAWWd|j�XWd|j�X|dkr�||kr�td||f|��n|S)ztretrieve(url) returns (filename, headers) for a local object
        or (tempfilename, headers) for a remote object.NrJrMrKrriirLzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rN)rrr�rr�rQr�r3rrhrCrUrrrRrS�splitextZmkstempr�rX�fdopenrYrZr[r\r)r{rDr]r^rEr�Zurl1r_r�r�r`rarUZgarbagerS�suffix�fdrbrcrdrZrerfrHrHrI�retrievensl


zURLopener.retrievecCsd}d}t|t�r]t|�\}}|rTt|�\}}t|�}n|}n�|\}}t|�\}}t|�\}	}
|
}d}|	j�dkr�d}n^t|
�\}}
|r�t|�\}}n|r�d|	||
f}nt|�r|}n|s*tdd��n|r]t|�}t	j
|j��jd�}nd}|r�t|�}t	j
|j��jd�}nd}||�}
i}|r�d||d<n|r�d||d	<n|r�||d
<nd|d<x!|j
D]\}}|||<qW|dk	rLd
|d<|
jd|||�n|
jd|d|�y|
j�}Wn$tjjk
r�td��YnXd|jko�dknr�t||jd||j�S|j||j|j|j|j|�SdS)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 givenrzBasic %szProxy-AuthorizationrArjr�rqz!application/x-www-form-urlencodedzContent-Typer�r�r`z$http protocol error: bad status liner�i,zhttp:)r�r�rrr
rrorrhrrrrr�rprsr�r�Z
BadStatusLinerZstatusrr��
http_errorr_rt)r{Zconnection_factoryrDrEZuser_passwdZproxy_passwdrqr�Zrealhostr�r�Z
proxy_authr=Z	http_connr`�headerr}r�rHrHrI�_open_generic_http�sr
			!!



	zURLopener._open_generic_httpcCs|jtjj||�S)zUse HTTP protocol.)r�r�r�rv)r{rDrErHrHrI�	open_httpszURLopener.open_httpc
Cs�d|}t||�rwt||�}|dkrO||||||�}	n|||||||�}	|	rw|	Sn|j|||||�S)z�Handle 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{rDr_�errcode�errmsgr`rErVrzrbrHrHrIr�s
zURLopener.http_errorcCs&|j�t||||d��dS)z>Default error handler: close the connection and raise OSError.N)r�r)r{rDr_r�r�r`rHrHrIr� s
zURLopener.http_error_defaultcCs"tjj|d|jd|j�S)Nr�r�)r�r�r�r�r�)r{rqrHrHrI�_https_connection&s	zURLopener._https_connectioncCs|j|j||�S)zUse HTTPS protocol.)r�r�)r{rDrErHrHrI�
open_https+szURLopener.open_httpscCs�t|t�std��n|dd�dkru|dd�dkru|dd�j�dkrutd	��n
|j|�SdS)
z/Use local file or FTP depending on form of URL.zEfile error: proxy support for file protocol currently not implementedNr�z//r9r��z
localhost/z-file:// scheme is supported only on localhost)r�r�rror>r�)r{rDrHrHrI�	open_file/s
HzURLopener.open_filecCs�ddl}ddl}t|�\}}t|�}ytj|�}Wn:tk
r�}zt|j|j	��WYdd}~XnX|j
}	|jj|j
dd�}
|j|�d}|jd|p�d|	|
f�}|s'|}
|dd�dkrd	|}
ntt|d
�||
�St|�\}}|r�tj|�t�ft�kr�|}
|dd�dkr�d	|}
n)|dd�dkr�td
|��ntt|d
�||
�Std��dS)zUse local file.rNr�Tz6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainrMr�zfile://r�r�z./zAlocal file url may start with / or file:. Unknown url of type: %sz#local file error: not on local host)r�r�rr3rRr�rhr�strerrorr]r�r�r�r�r�r�rrCr
r�r�r��thishostr>)r{rDr�r�rqrJZ	localnamer��erdr�r�r`Zurlfiler(rHrHrIr�8s:(	
"
zURLopener.open_local_filecCs�t|t�std��nddl}t|�\}}|sQtd��nt|�\}}t|�\}}|r�t|�\}}nd}t|�}t|p�d�}t|p�d�}t	j
|�}|s�ddl}|j}nt
|�}t|�\}}	t|�}|jd�}
|
dd�|
d}
}|
rk|
drk|
dd�}
n|
r�|
dr�d|
d<n|||dj|
�f}t|j�tkrxJt|j�D]6}
|
|kr�|j|
}|j|
=|j�q�q�Wny9||jkr:t|||||
�|j|<n|sId}nd	}xM|	D]E}t|�\}}|j�d
krV|dkrV|j�}qVqVW|j|j||�\}}|jd|�d}d}|r�|d|7}n|dk	r|dkr|d|7}ntj|�}t||d|�SWnKt�k
r�}z(td|�j t!j"�d��WYdd}~XnXdS)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNzftp error: no host givenrir�rMr�r@r�r�r�r�rezftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr�rNrN)r�r�r�r@rer�)#r�r�rr�rr
rrr
r�r�r�r�rYrr0r�r[r��MAXFTPCACHEr�r�r�rror�r�r�r�r�r�	ftperrorsr�r�r�)r{rDr�rqrSr(rrr�r�r�rJr|r�r�r�r�r}r_r�r�r`r�rHrHrI�open_ftpXsp


"	
zURLopener.open_ftpc	Cs�t|t�std��ny|jdd�\}}Wn!tk
r]tdd��YnX|smd}n|jd�}|dkr�d	||d
�kr�||dd
�}|d
|�}nd}g}|jdtj	d
tj
tj����|jd|�|dkr>tj|j
d��jd�}nt|�}|jdt|��|jd�|j|�dj|�}tj|�}tj|�}t|||�S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr�rMz
data errorzbad data URLztext/plain;charset=US-ASCII�;rrNrizDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %d�
)r�r�rr0r>rh�rfindrXrQZstrftimeZgmtimerr�rrr
r[r�r�r�r��StringIOr)	r{rDrEr�Zsemi�encodingr�r`�frHrHrI�	open_data�s6
	"$

zURLopener.open_data)r�r�r�r�r�r�r�r~r�r�r�r�rCr�r�r�r�r�r�r�r?r�r�r�r�r�r�rHrHrHrIr7�s.
$B\	 :c@s�eZdZdZdd�Zdd�Zddd�Zd	d
�Zddd�Zdd
d�Z	ddd�Z
dddd�Zdddd�Zddd�Z
ddd�Zddd�Zddd�Zddd �Zd!d"�ZdS)#r8z?Derived class with handlers for errors we can handle (perhaps).cOs2tj|||�i|_d|_d|_dS)Nrr�)r7r~�
auth_cache�tries�maxtries)r{r��kwargsrHrHrIr~�s		zFancyURLopener.__init__cCst||d||�S)z3Default error handling -- don't raise an exception.zhttp:)r)r{rDr_r�r�r`rHrHrIr��sz!FancyURLopener.http_error_defaultNc	Cs�|jd7_|jrm|j|jkrmt|d�rE|j}n	|j}d|_|||dd|�S|j||||||�}d|_|S)z%Error 302 -- relocated (temporarily).rM�http_error_500ri�z)Internal Server Error: Redirect Recursion)r�rr�rr��redirect_internal)	r{rDr_r�r�r`rEr�rbrHrHrIr�s		
		zFancyURLopener.http_error_302c	Cs�d|kr|d}nd|kr2|d}ndS|j�t|jd||�}t|�}|jd	kr�t|||d|||��n|j|�S)
Nr�r�r�r�r�r�riz( Redirection to url '%s' is not allowed.)zhttpzhttpszftpri)r�rr�rr�rrC)	r{rDr_r�r�r`rEr�rrHrHrIr�s


	z FancyURLopener.redirect_internalcCs|j||||||�S)z*Error 301 -- also relocated (permanently).)r)r{rDr_r�r�r`rErHrHrIr�szFancyURLopener.http_error_301cCs|j||||||�S)z;Error 303 -- also relocated (essentially identical to 302).)r)r{rDr_r�r�r`rErHrHrIr�szFancyURLopener.http_error_303cCsE|dkr(|j||||||�S|j|||||�SdS)z1Error 307 -- relocated, but turn POST into error.N)rr�)r{rDr_r�r�r`rErHrHrIr�szFancyURLopener.http_error_307Fc
Cs$d|kr+tj||||||�n|d}tjd|�}	|	sltj||||||�n|	j�\}
}|
j�dkr�tj||||||�n|s�tj||||||�nd|jd}|dkrt||�||�St||�|||�SdS)z_Error 401 -- authentication required.
        This function supports Basic authentication only.zwww-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r-Zretry_�_basic_authN)r7r�r>�matchr3ror�r�)
r{rDr_r�r�r`rErf�stuffrr�r!rVrHrHrIrBs&

zFancyURLopener.http_error_401c
Cs$d|kr+tj||||||�n|d}tjd|�}	|	sltj||||||�n|	j�\}
}|
j�dkr�tj||||||�n|s�tj||||||�nd|jd}|dkrt||�||�St||�|||�SdS)zeError 407 -- proxy authentication required.
        This function supports Basic authentication only.zproxy-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r-Zretry_proxy_rN)r7r�r>rr3ror�r�)
r{rDr_r�r�r`rErfrrr�r!rVrHrHrIrCs&

zFancyURLopener.http_error_407cCst|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	jd�d}|	|d�}	|j|	||�\}}
|p�|
s�dSdt|dd�t|
dd�|	f}	d|	|
|jd<|dkr�|j|�S|j||�SdS)Nzhttp://r��@rMz%s:%s@%sr�ri)rrrr��get_user_passwdr	rC)r{rDr!rErqr�r�rr�r��
proxyselectorr�rrrHrHrI�retry_proxy_http_basic_auth6s 

z*FancyURLopener.retry_proxy_http_basic_authcCst|�\}}d||}|jd}t|�\}}	t|	�\}	}
|	jd�d}|	|d�}	|j|	||�\}}
|p�|
s�dSdt|dd�t|
dd�|	f}	d|	|
|jd<|dkr�|j|�S|j||�SdS)Nzhttps://r�rrMz%s:%s@%sr�ri)rrrr�rr	rC)r{rDr!rErqr�r�rr�r�r	r�rrrHrHrI�retry_proxy_https_basic_authHs 

z+FancyURLopener.retry_proxy_https_basic_authc
Cs�t|�\}}|jd�d}||d�}|j|||�\}}|pY|s`dSdt|dd�t|dd�|f}d||}	|dkr�|j|	�S|j|	|�SdS)NrrMz%s:%s@%sr�rizhttp://)rr�rr	rC)
r{rDr!rErqr�r�rrr�rHrHrIr7Zs
z$FancyURLopener.retry_http_basic_authc
Cs�t|�\}}|jd�d}||d�}|j|||�\}}|pY|s`dSdt|dd�t|dd�|f}d||}	|dkr�|j|	�S|j|	|�SdS)NrrMz%s:%s@%sr�rizhttps://)rr�rr	rC)
r{rDr!rErqr�r�rrr�rHrHrI�retry_https_basic_authhs
z%FancyURLopener.retry_https_basic_authrcCs�|d|j�}||jkrD|r6|j|=qD|j|Sn|j||�\}}|sh|r~||f|j|<n||fS)Nr)ror��prompt_user_passwd)r{rqr!r�r|rrrHrHrIrvs
zFancyURLopener.get_user_passwdcCspddl}y@td||f�}|jd|||f�}||fSWntk
rkt�dSYnXdS)z#Override this in a GUI environment!rNzEnter username for %s at %s: z#Enter password for %s in %s at %s: )NN)�getpass�input�KeyboardInterrupt�print)r{rqr!rrrrHrHrIr
�s	
z!FancyURLopener.prompt_user_passwd)r�r�r�r�r~r�rrrrrrBrCr
rr7rrr
rHrHrHrIr8�s$cCs"tdkrtjd�antS)z8Return the IP address of the magic hostname 'localhost'.Nr�)�
_localhostr�r�rHrHrHrIr��sr�cCsgtdkrcy#ttjtj��d�aWqctjk
r_ttjd�d�aYqcXntS)z,Return the IP addresses of the current host.Nr�r�)�	_thishostr r�r�r�r�rHrHrHrIr��s#!r�cCs(tdkr$ddl}|jantS)z1Return the set of errors raised by the FTP class.Nr)�
_ftperrorsr�r�)r�rHrHrIr��sr�cCs"tdkrtjd�antS)z%Return an empty email Message object.Nri)�
_noheadersr�r�rHrHrHrI�	noheaders�src@speZdZdZdddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r�z;Class used by open_ftp() for cache of open FTP connections.NTc	Csr||_||_||_||_||_||_d|_||_y|j�Wn|j	��YnXdS)Nr)
rrrqr(r�rF�refcount�	keepalive�initr�)r{rrrqr(r�rFr�rHrHrIr~�s								
zftpwrapper.__init__cCs�ddl}d|_|j�|_|jj|j|j|j�|jj|j	|j
�dj|j�}|jj
|�dS)Nrr�)r��busyZFTPr�Zconnectrqr(rFZloginrrr�r��cwd)r{r�Z_targetrHrHrIr�s	zftpwrapper.initc-Cs:ddl}|j�|dkr1d}d}nd|}d}y|jj|�Wn/|jk
r�|j�|jj|�YnXd}|r0|r0y&d|}|jj|�\}}Wq0|jk
r,}zGt|�dd�d	krt	d
|�j
tj�d��nWYdd}~Xq0Xn|s�|jjd�|r�|jj
�}	zVy|jj|�Wn;|jk
r�}zt	d
|�|�WYdd}~XnXWd|jj|	�Xd|}nd
}|jj|�\}}nd|_t|jd�|j�}
|jd7_|j�|
|fS)Nrrer�zTYPE ArMzTYPE zRETR r9Z550z
ftp error: %rr�zLIST ZLISTr�)rer�)r��endtransferr�Zvoidcmdr�rZntransfercmdZ
error_permr�rr�r�r��pwdrrr�makefile�
file_closerr�)r{rJr�r��cmd�isdirr�r�rtrZftpobjrHrHrIr��sN
	



,*
	
zftpwrapper.retrfilecCs
d|_dS)Nr)r)r{rHrHrIr	szftpwrapper.endtransfercCs)d|_|jdkr%|j�ndS)NFr)rr�
real_close)r{rHrHrIr�	s	zftpwrapper.closecCsC|j�|jd8_|jdkr?|jr?|j�ndS)NrMr)rrrr")r{rHrHrIr	s
zftpwrapper.file_closecCs7|j�y|jj�Wnt�k
r2YnXdS)N)rr�r�r�)r{rHrHrIr"	s

zftpwrapper.real_close)r�r�r�r�r~rr�rr�rr"rHrHrHrIr��s	-r�cCs�i}x\tjj�D]K\}}|j�}|r|dd�dkr|||dd�<qqWdtjkr�|jdd�n|S)aReturn 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�_proxyZREQUEST_METHODr�i����i����)rR�environrvror�)rrVr}rHrHrI�getproxies_environment	s	r&cCs�tjjdd�p'tjjdd�}|dkr:dSt|�\}}dd�|jd�D�}x6|D].}|ro|j|�s�|j|�rodSqoWd	S)
z�Test 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_proxyriZNO_PROXY�*rMcSsg|]}|j��qSrH)r�)r�rrHrHrIr:	s	z,proxy_bypass_environment.<locals>.<listcomp>r�r)rRr%r�r
r0r�)rqr'�hostonlyr(Z
no_proxy_listrVrHrHrI�proxy_bypass_environment-	s*
$r*c
Cs�ddlm}t|�\}}dd�}d|krK|drKdSnd}x+|jd	f�D]}|svqdntjd
|�}|dk	rh|dkr�ytj|�}||�}Wq�tk
r�wdYq�Xn||jd��}	|jd�}
|
dkr-d
|jd�j	d�d}
nt
|
dd��}
d|
}
||
?|	|
?kr{dSqd|||�rddSqdWdS)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)�fnmatchcSs�|jd�}ttt|��}t|�dkrY|ddddgdd�}n|dd>|dd>B|dd>B|d	BS)
N�.r�r�rMrPr�rLr9)r0r�r�rYr[)ZipAddrr'rHrHrI�ip2numT	s
#z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr,Zexclude_simpleTN�
exceptionsz(\d+(?:\.\d+)*)(/\d+)?rMr�rL� F)r+r
r�r>rr�r�rh�group�countrY)rq�proxy_settingsr+r)r(r.ZhostIPr}r�r+�maskrHrHrI�_proxy_bypass_macosx_sysconfD	s:

#
r5�darwin)�_get_proxy_settings�_get_proxiescCst�}t||�S)N)r7r5)rqr3rHrHrI�proxy_bypass_macosx_sysconf�	s	r9cCst�S)z�Return a dictionary of scheme -> proxy server URL mappings.

        This function uses the MacOSX framework SystemConfiguration
        to fetch the proxy information.
        )r8rHrHrHrI�getproxies_macosx_sysconf�	sr:cCs!t�rt|�St|�SdS)N)r&r*r9)rqrHrHrIr�	s	
rcCst�pt�S)N)r&r:rHrHrHrIr4�	scCsli}yddl}Wntk
r.|SYnXy|j|jd�}|j|d�d}|r?t|j|d�d�}d|kr�x�|jd�D]M}|jdd�\}}tjd	|�s�d
||f}n|||<q�Wq?|dd�dkr||d
<q?d||d
<d||d<d||d<n|j	�Wnt
ttfk
rgYnX|S)zxReturn a dictionary of scheme -> proxy server URL mappings.

        Win32 uses the registry to store proxies.

        rNz;Software\Microsoft\Windows\CurrentVersion\Internet Settings�ProxyEnableZProxyServerrr�rMz^([^/:]+)://z%s://%srHzhttp:r�z	http://%sz
https://%sr�zftp://%sr�)
�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr�r0r>rZCloserhr>r�)rr<�internetSettings�proxyEnableZproxyServer�pr�ZaddressrHrHrI�getproxies_registry�	s8
			

rDcCst�pt�S)z�Return a dictionary of scheme -> proxy server URL mappings.

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

        )r&rDrHrHrHrIr4�	sc&Cs�yddl}Wntk
r(dSYnXyK|j|jd�}|j|d�d}t|j|d�d�}Wntk
r�dSYnX|s�|r�dSt|�\}}|g}y/tj	|�}||kr�|j
|�nWntk
r�YnXy/tj|�}||kr,|j
|�nWntk
rAYnX|jd�}x�|D]�}	|	dkr}d|kr}dSn|	j
dd	�}	|	j
d
d�}	|	j
dd�}	x*|D]"}
tj|	|
tj�r�dSq�WqXWdS)
Nrz;Software\Microsoft\Windows\CurrentVersion\Internet Settingsr;Z
ProxyOverrider�z<local>r,rMz\.r(z.*�?)r<r=r>r?r@r�rhr
r�r�rXZgetfqdnr0r�r>rr@)rqr<rArBZ
proxyOverrideZrawHostr(ZaddrZfqdnr,r�rHrHrI�proxy_bypass_registry�	sR
			

		



rFcCs!t�rt|�St|�SdS)z�Return a dictionary of scheme -> proxy server URL mappings.

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

        N)r&r*rF)rqrHrHrIr
s	
)}r�rr�r�rSZhttp.clientr�r�rRr)r>r�r�rQrmrUrOr4Zurllib.errorrrrZurllib.parserrrrr	r
rrr
rrrrrrrrrZurllib.responserrr@r=r?�__all__r�r�rBr�r/r0rWr5r6r?�ASCIIrmrrrrr1rr.rrr
rr r!r"r#r$�urandomrRr%r&r'rgr(r�r�rArXrr-rJrLr)r�r*r+r,r�rVZ
nturl2pathr3r2r�r7r8rr�rr�rr�rrr�r&r*r5�platformZ_scproxyr7r8r9r:rr4rDrFrHrHrHrI�<module>Ds�v
					!?n�$h*@
7	�q

+4:5!���

_<
-	2

?>