Your IP : 18.217.14.208


Current Path : /opt/alt/python35/lib64/python3.5/urllib/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/urllib/__pycache__/request.cpython-35.opt-2.pyc



���]{�)@s�ddlZddlZddlZddlZddlZddlZddlZddlZddl	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.ddd	d
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'g!Z/ej0dd(�Z1da2de
j3d)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
�d
e@�ZBGd:d�de@�ZCd;d<�ZDGd=d
�d
e@�ZEGd>d�d�ZFGd?d�deF�ZGGd@d�deG�ZHGdAd�d�ZIGdBd�deIe@�ZJGdCd�deIe@�ZKejLZMGdDd�d�ZNGdEd�de@eN�ZOGdFd�de@eN�ZPGdGdH�dHe@�ZQGdId�deQ�ZReSejTdJ�r�GdKdL�dLeQ�ZUe/jVdL�GdMd�de@�ZWGdNd�de@�ZXdOdP�ZYdQdR�ZZGdSd�de@�Z[dTdU�Z\GdVd�de@�Z]GdWd�de]�Z^GdXd�de@�Z_dYZ`ejadZkrZdd[lbmcZcmdZdnd\d"�Zcd]d!�ZdiZeGd^d&�d&�ZfGd_d'�d'ef�Zgdahd`da�Zidajdbdc�Zkdalddde�Zmdandfdg�ZoGdhdi�di�Zpdjdk�Zqddldm�Zrdndo�Zsejtdpkr{ddqlumvZvmwZwdrds�Zxdtdu�Zydvdw�Zzdxd#�Z{nNejadZkr�dydz�Z|d{d#�Z{d|d}�Z}d~dw�ZzneqZ{erZzdS)�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�HTTPPasswordMgrWithPriorAuth�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��ts<td��tjtjjd|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�HTTPSHandlerr2�_opener�open)	�url�data�timeoutr;r<r=r>Z
https_handler�opener�rI�,/opt/alt/python35/lib64/python3.5/request.pyr0�s$		cCs
|adS)N)rC)rHrIrIrJr1�scCs�t|�\}}tjt||���:}|j�}|dkrb|rbtjj|�|fS|rzt|d�}n(t	j
dd�}|j}tj
|�|��||f}	d}
d
}d}d}
d	|kr�t|d
�}|r�||
|
|�xW|j|
�}|sP|t|�7}|j|�|
d7}
|r||
|
|�qWWdQRXWdQRX|dkr�||kr�td||f|	��|	S)N�file�wb�deleteFi��rzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi ���)r�
contextlib�closingr0�info�os�path�normpathrD�tempfile�NamedTemporaryFile�name�_url_tempfiles�append�int�read�len�writer)rE�filename�
reporthookrFZurl_typerU�fp�headers�tfp�result�bs�sizer]�blocknum�blockrIrIrJr6�sD	


"cCsTx4tD],}ytj|�Wqtk
r2YqXqWtdd�=trPdadS)N)rZrT�unlink�OSErrorrC)Z	temp_filerIrIrJr7�s

	
z:\d+$cCsV|j}t|�d}|dkr7|jdd�}tjd|d�}|j�S)NrO��Host)�full_urlr�
get_header�_cut_port_re�sub�lower)�requestrE�hostrIrIrJ�request_host�s	ruc@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|�}||_	||_
|r�||_dS)N)rnrc�unredirected_hdrs�_datarF�_tunnel_host�items�
add_headerru�origin_req_host�unverifiable�method)	�selfrErFrcr{r|r}�key�valuerIrIrJ�__init__
s								zRequest.__init__cCs&|jrdj|j|j�S|jS)Nz{}#{})�fragment�format�	_full_url)r~rIrIrJrns	zRequest.full_urlcCs8t|�|_t|j�\|_|_|j�dS)N)rr�rr��_parse)r~rErIrIrJrn"scCsd|_d|_d|_dS)Nrl)r�r��selector)r~rIrIrJrn)s		cCs|jS)N)rw)r~rIrIrJrF/szRequest.datacCs8||jkr4||_|jd�r4|jd�dS)NzContent-length)rw�
has_header�
remove_header)r~rFrIrIrJrF3s	cCs
d|_dS)N)rF)r~rIrIrJrF=scCsqt|j�\|_}|jdkr:td|j��t|�\|_|_|jrmt|j�|_dS)Nzunknown url type: %r)	rr��typer?rnrrtr�r
)r~�restrIrIrJr�As	zRequest._parsecCs+|jdk	rdnd}t|d|�S)N�POST�GETr})rF�getattr)r~Zdefault_methodrIrIrJ�
get_methodIszRequest.get_methodcCs|jS)N)rn)r~rIrIrJ�get_full_urlNszRequest.get_full_urlcCsJ|jdkr(|jr(|j|_n||_|j|_||_dS)N�https)r�rxrtrnr�)r~rtr�rIrIrJ�	set_proxyQs
	zRequest.set_proxycCs|j|jkS)N)r�rn)r~rIrIrJ�	has_proxyYszRequest.has_proxycCs||j|j�<dS)N)rc�
capitalize)r~r�valrIrIrJrz\szRequest.add_headercCs||j|j�<dS)N)rvr�)r~rr�rIrIrJ�add_unredirected_header`szRequest.add_unredirected_headercCs||jkp||jkS)N)rcrv)r~�header_namerIrIrJr�dszRequest.has_headercCs"|jj||jj||��S)N)rc�getrv)r~r��defaultrIrIrJrohs	zRequest.get_headercCs*|jj|d�|jj|d�dS)N)rc�poprv)r~r�rIrIrJr�mszRequest.remove_headercCs/|jj�}|j|j�t|j��S)N)rv�copy�updaterc�listry)r~�hdrsrIrIrJ�header_itemsqszRequest.header_items)�__name__�
__module__�__qualname__r��propertyrn�setter�deleterrFr�r�r�r�r�rzr�r�ror�r�rIrIrIrJrs(
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_versionrIrIrJr�ws
				zOpenerDirector.__init__cCs�t|d�s%tdt|���d}xut|�D]g}|dkrMq8|jd�}|d|�}||dd�}|jd	�r|jd�|d}||dd�}yt|�}Wntk
r�YnX|jj	|i�}	|	|j|<n]|d
kr"|}|j
}	n?|dkr@|}|j}	n!|dkr8|}|j}	nq8|	j
|g�}
|
r�tj|
|�n
|
j|�d
}q8W|r�tj|j|�|j|�dS)N�
add_parentz%expected BaseHandler instance, got %rF�redirect_request�do_open�
proxy_open�_rO�errorrD�responsersT)r�r�r�)�hasattr�	TypeErrorr��dir�find�
startswithr\r?r�r�r�r�r��
setdefault�bisectZinsortr[r�r�)r~�handlerZadded�meth�i�protocolZ	condition�j�kind�lookupr�rIrIrJ�add_handler�sJ


zOpenerDirector.add_handlercCsdS)NrI)r~rIrIrJ�close�szOpenerDirector.closec	GsR|j|f�}x9|D]1}t||�}||�}|dk	r|SqWdS)N)r�r�)	r~�chainr��	meth_name�argsr�r��funcrerIrIrJ�_call_chain�s
zOpenerDirector._call_chainNc
Cs�t|t�r!t||�}n|}|dk	r<||_||_|j}|d}x8|jj|g�D]!}t||�}||�}qnW|j	||�}	|d}x;|j
j|g�D]$}t||�}|||	�}	q�W|	S)NZ_requestZ	_response)�
isinstance�strrrFrGr�r�r�r��_openr�)
r~�fullurlrFrG�reqr�r�Z	processorr�r�rIrIrJrD�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�rFrer�rIrIrJr��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_%srO�_errorrr��http_error_default)r�r�)r�r�)r~�protor��dictr�Zhttp_errZ	orig_argsrerIrIrJr��s 


		
zOpenerDirector.error)r�r�r�r�r�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrDr�r�rIrIrIrJrvs/c	Gs7t�}ttttttttt	g	}t
tjd�rI|j
t�t�}xi|D]a}xX|D]P}t|t�r�t||�r�|j|�qft||�rf|j|�qfWqYWx|D]}|j|�q�Wx|D]}|j|��q�Wx3|D]+}t|t�r"|�}|j|�qW|S)N�HTTPSConnection)rrr.r)rrr+r*r/r-r�r��clientr[rB�setr�r��
issubclass�add�remover�)r�rHZdefault_classes�skip�klass�check�hrIrIrJr2s0					
	




	c@s:eZdZdZdd�Zdd�Zdd�ZdS)	ri�cCs
||_dS)N)�parent)r~r�rIrIrJr�+szBaseHandler.add_parentcCsdS)NrI)r~rIrIrJr�.szBaseHandler.closecCs#t|d�sdS|j|jkS)N�
handler_orderT)r�r�)r~�otherrIrIrJ�__lt__2szBaseHandler.__lt__N)r�r�r�r�r�r�r�rIrIrIrJr(sc@s(eZdZdZdd�ZeZdS)r/i�cCsa|j|j|j�}}}d|ko7dkns]|jjd|||||�}|S)N��i,r�)�code�msgrSr�r�)r~rsr�r�r�r�rIrIrJ�
http_response?s
 	z HTTPErrorProcessor.http_responseN)r�r�r�r�r��https_responserIrIrIrJr/;sc@seZdZdd�ZdS)rcCst|j||||��dS)N)rrn)r~r�rbr�r�r�rIrIrJr�Msz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�r�rIrIrIrJrLsc@sHeZdZdZdZdd�Zdd�ZeZZZ	dZ
dS)	r��
c	s�|j�}|dkr$|dkp9|dko9|dksWt|j||||��|jdd	�}d�t�fdd
�|jj�D��}t|d|d|jdd�S)N�-�.�/�3r��HEADr�� z%20�content-length�content-typec3s3|])\}}|j��kr||fVqdS)N)rr)�.0�k�v)�CONTENT_HEADERSrIrJ�	<genexpr>ss	z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>rcr{r|T)r�r�r�r�)r�r�)r�r�r�)r�r�)	r�rrn�replacer�rcryrr{)	r~r�rbr�r�rc�newurl�mZ
newheadersrI)r�rJr�Xs
(		z$HTTPRedirectHandler.redirect_requestc
Cs�d|kr|d}nd|kr2|d}ndSt|�}|jdkrst||d||f||��|jr�|jr�t|�}d|d	<t|�}t|d
ddtj	�}t
|j|�}|j||||||�}|dkrdSt
|d
�rw|j}	|_|	j|d�|jksRt|	�|jkr�t|j||j|||��ni}	|_|_|	j|d�d|	|<|j�|j�|jj|d|j�S)N�location�urir�r��ftprlz+%s - Redirection to url '%s' is not allowed�/r��encodingz
iso-8859-1�safe�
redirect_dictrrOrG)r�r�r�rl)r�schemerrUZnetlocr�rr	�string�punctuationrrnr�r�rr��max_repeatsr^�max_redirections�inf_msgr]r�r�rDrG)
r~r�rbr�r�rcr��urlparts�newZvisitedrIrIrJ�http_error_302~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�rrr�r�http_error_301�http_error_303�http_error_307rrIrIrIrJrPs&<c	Cs�t|�\}}|jd�s0d}|}nS|jd�sOtd|��|jdd�}|dkrsd}|d|�}t|�\}}|dk	r�t|�\}}n
d}}||||fS)Nr�z//zproxy URL with no authority: %rr�rOrP)rr�r?r�rr)	�proxyrZr_scheme�	authority�endZuserinfo�hostport�user�passwordrIrIrJ�_parse_proxy�s	
rc@s1eZdZdZddd�Zdd�ZdS)r�dNc	Cse|dkrt�}||_x@|j�D]2\}}t|d||||jdd��q+WdS)Nz%s_opencSs||||�S)NrI)�rrr�r�rIrIrJ�<lambda>�sz'ProxyHandler.__init__.<locals>.<lambda>)r5�proxiesry�setattrr�)r~rr�rErIrIrJr��s		
zProxyHandler.__init__cCs�|j}t|�\}}}}|dkr3|}|jrOt|j�rOdS|r�|r�dt|�t|�f}	tj|	j��jd�}
|j	dd|
�t|�}|j
||�||ks�|dkr�dS|jj|d|j
�SdS)Nz%s:%s�asciizProxy-authorizationzBasic r�rG)r�rrt�proxy_bypassr
�base64�	b64encode�encode�decoderzr�r�rDrG)r~r�rr�Z	orig_typeZ
proxy_typerrrZ	user_passZcredsrIrIrJr��s 	zProxyHandler.proxy_open)r�r�r�r�r�r�rIrIrIrJr�s
c@sOeZdZdd�Zdd�Zdd�Zddd	�Zd
d�ZdS)
r cCs
i|_dS)N)�passwd)r~rIrIrJr�
szHTTPPasswordMgr.__init__cs�t|t�r|g}|�jkr4i�j|<xGdD]?�t��fdd�|D��}||f�j||<q;WdS)NTFcs"g|]}�j|���qSrI)�
reduce_uri)r��u)�default_portr~rIrJ�
<listcomp>s	z0HTTPPasswordMgr.add_password.<locals>.<listcomp>)TF)r�r�r!�tuple)r~�realmr�rr!�reduced_urirI)r$r~rJ�add_password
s	

zHTTPPasswordMgr.add_passwordc	Cs�|jj|i�}xddD]\}|j||�}xA|j�D]3\}}x$|D]}|j||�rT|SqTWqAWqWdS)NTF)TF)NN)r!r�r"ry�	is_suburi)	r~r'�authuriZdomainsr$�reduced_authuriZurisZauthinfor�rIrIrJ�find_user_passwords

z"HTTPPasswordMgr.find_user_passwordTc
Cs�t|�}|dr=|d}|d}|dp7d}nd}|}d}t|�\}}|r�|dkr�|dk	r�ddddij|�}	|	dk	r�d	||	f}||fS)
NrOrr�r�r��Pr�i�z%s:%d)rr
r�)
r~r�r$�partsrrrUrt�portZdportrIrIrJr""s 


	zHTTPPasswordMgr.reduce_uricCsi||krdS|d|dkr(dStj|d|df�}t|�t|d�kredSdS)NTrFrO)�	posixpath�commonprefixr^)r~�base�test�commonrIrIrJr*9szHTTPPasswordMgr.is_suburiN)r�r�r�r�r)r-r"r*rIrIrIrJr s

c@seZdZdd�ZdS)r!cCsDtj|||�\}}|dk	r1||fStj|d|�S)N)r r-)r~r'r+rrrIrIrJr-Js

z2HTTPPasswordMgrWithDefaultRealm.find_user_passwordN)r�r�r�r-rIrIrIrJr!HscsReZdZ�fdd�Zd�fdd�Zddd�Zdd	�Z�S)
r"cs i|_t�j||�dS)N)�
authenticated�superr�)r~r��kwargs)�	__class__rIrJr�Ts	z%HTTPPasswordMgrWithPriorAuth.__init__FcsR|j||�|dk	r5t�jd|||�t�j||||�dS)N)�update_authenticatedr7r))r~r'r�rr!�is_authenticated)r9rIrJr)Xsz)HTTPPasswordMgrWithPriorAuth.add_passwordcCs]t|t�r|g}x>dD]6}x-|D]%}|j||�}||j|<q,WqWdS)NTF)TF)r�r�r"r6)r~r�r;r$r#r(rIrIrJr:_s	

z1HTTPPasswordMgrWithPriorAuth.update_authenticatedcCsXxQdD]I}|j||�}x.|jD]#}|j||�r)|j|Sq)WqWdS)NTF)TF)r"r6r*)r~r+r$r,r�rIrIrJr;is

z-HTTPPasswordMgrWithPriorAuth.is_authenticated)r�r�r�r�r)r:r;rIrI)r9rJr"Rs
c@speZdZejdej�Zddd�Zdd�Zdd�Z	d	d
�Z
dd�Ze
ZeZ
dS)
r#z1(?:.*,)*[ 	]*([^ 	]+)[ 	]+realm=(["']?)([^"']*)\2NcCs1|dkrt�}||_|jj|_dS)N)r r!r))r~Zpassword_mgrrIrIrJr�s		z!AbstractBasicAuthHandler.__init__c	Cs�|j|d�}|r�|j�d}|j�dkrMtd|��nqtjj|�}|r�|j�\}}}|dkr�tj	dt
d�|j�dkr�|j|||�SdS)	Nr�basiczDAbstractBasicAuthHandler does not support the following scheme: '%s'�"�'zBasic Auth Realm was unquotedr�)r=r>)r��splitrrr?r#�rx�search�groups�warnings�warn�UserWarning�retry_http_basic_auth)	r~�authreqrtr�rcr�mor	r'rIrIrJ�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 rrG)r!r-rrrr ro�auth_headerr�r�rDrG)r~rtr�r'r�pw�raw�authrIrIrJrF�s"z.AbstractBasicAuthHandler.retry_http_basic_authcCs�t|jd�s)|jj|j�r-|S|jd�s�|jjd|j�\}}dj||�j�}tj	|�j
�}|jddj|j���|S)Nr;�
Authorizationz{0}:{1}zBasic {})
r�r!r;rnr�r-r�rrZstandard_b64encoder r��strip)r~r�rr!ZcredentialsZauth_strrIrIrJ�http_request�s	z%AbstractBasicAuthHandler.http_requestcCsdt|jd�r`d|jko,dknrJ|jj|jd�n|jj|jd�|S)Nr;r�i,TF)r�r!r�r:rn)r~r�r�rIrIrJr��s
z&AbstractBasicAuthHandler.http_response)r�r�r��re�compile�Ir@r�rIrFrPr��
https_requestr�rIrIrIrJr#qs	
c@s"eZdZdZdd�ZdS)r$rNcCs%|j}|jd|||�}|S)Nzwww-authenticate)rnrI)r~r�rbr�r�rcrEr�rIrIrJ�http_error_401�s		z#HTTPBasicAuthHandler.http_error_401N)r�r�r�rJrUrIrIrIrJr$�sc@s"eZdZdZdd�ZdS)r%zProxy-authorizationcCs%|j}|jd|||�}|S)Nzproxy-authenticate)rtrI)r~r�rbr�r�rcrr�rIrIrJ�http_error_407�s		z$ProxyBasicAuthHandler.http_error_407N)r�r�r�rJrVrIrIrIrJr%�sc@sseZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dS)r&NcCsL|dkrt�}||_|jj|_d|_d|_d|_dS)Nr)r r!r)�retried�nonce_count�
last_nonce)r~r!rIrIrJr��s				z"AbstractDigestAuthHandler.__init__cCs
d|_dS)Nr)rW)r~rIrIrJ�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|��dS)	N�i�zdigest auth failedrOr�digestr<zEAbstractDigestAuthHandler does not support the following scheme: '%s')r�rWrrnr?rr�retry_http_digest_authr?)r~rJrtr�rcrGrrIrIrJrI�sz/AbstractDigestAuthHandler.http_error_auth_reqedcCs�|jdd�\}}ttdt|���}|j||�}|r�d|}|jj|jd�|krwdS|j|j|�|j	j
|d|j�}|SdS)Nr�rOz	Digest %srG)r?�parse_keqv_list�filter�parse_http_list�get_authorizationrcr�rJr�r�rDrG)r~r�rM�tokenZ	challenge�chalZauth_valZresprIrIrJr]
s
z0AbstractDigestAuthHandler.retry_http_digest_authcCsXd|j|tj�f}|jd�td�}tj|�j�}|dd�S)Nz	%s:%s:%s:rrN�)rX�time�ctimer�_randombytes�hashlib�sha1�	hexdigest)r~�nonce�s�b�digrIrIrJ�
get_cnoncesz$AbstractDigestAuthHandler.get_cnoncecCsVyK|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}|r*|d|7}|d|7}|rR|d||f7}|S)Nr'rk�qop�	algorithm�MD5�opaquez%s:%s:%sz%s:%srMrOz%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_implsr!r-rnrF�get_entity_digestr�r�rYrXror)r~r�rcr'rkrprqrs�H�KDrrKZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr3rIrIrJra!sV


	
		
(
z+AbstractDigestAuthHandler.get_authorizationcsb|dkrdd��n+|dkr6dd��ntd|���fdd�}�|fS)NrrcSstj|jd��j�S)Nr)rh�md5rrj)�xrIrIrJr`sz?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAcSstj|jd��j�S)Nr)rhrirrj)rzrIrIrJrbsz.Unsupported digest authentication algorithm %rcs�d||f�S)Nz%s:%srI)rl�d)rwrIrJrgs)r?)r~rqrxrI)rwrJru]s
z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrI)r~rFrcrIrIrJrvjsz+AbstractDigestAuthHandler.get_entity_digest)r�r�r�r�rZrIr]rorarurvrIrIrIrJr&�s	<
c@s(eZdZdZdZdd�ZdS)r'rNi�cCs9t|j�d}|jd|||�}|j�|S)NrOzwww-authenticate)rrnrIrZ)r~r�rbr�r�rcrt�retryrIrIrJrUys
	
z$HTTPDigestAuthHandler.http_error_401N)r�r�r�rJr�rUrIrIrIrJr'osc@s(eZdZdZdZdd�ZdS)r(zProxy-Authorizationi�cCs/|j}|jd|||�}|j�|S)Nzproxy-authenticate)rtrIrZ)r~r�rbr�r�rcrtr|rIrIrJrV�s
		
z%ProxyDigestAuthHandler.http_error_407N)r�r�r�rJr�rVrIrIrIrJr(�sc@sCeZdZddd�Zdd�Zdd�Zdd	�Zd
S)�AbstractHTTPHandlerrcCs
||_dS)N)�_debuglevel)r~�
debuglevelrIrIrJr��szAbstractHTTPHandler.__init__cCs
||_dS)N)r~)r~�levelrIrIrJ�set_http_debuglevel�sz'AbstractHTTPHandler.set_http_debuglevelc
Cs�|j}|std��|jdk	r�|j}t|t�rTd}t|��|jd�ss|jdd�|jd�s�yt|�}Wn@tk
r�t|t	j
�r�tdt|�|f��Yn"X|jddt
|�|j�|}|j�r/t|j�\}}t|�\}}	|jd�sN|jd|�xE|jjD]7\}
}|
j�}
|j|
�s[|j|
|�q[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-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drm)rtrrFr�r�r�r�r��
memoryview�collections�Iterabler?r�r^�itemsizer�rr�rr�r�r�)r~rsrtrFr�ZmvZsel_hostrZselZsel_pathrYr�rIrIrJ�do_request_�sB		
	zAbstractHTTPHandler.do_request_c
s�|j}|std��||d|j|�}|j|j�t|j���jt�fdd�|jj	�D���d�d<tdd��j	�D���|j
r�i}d}|�kr��|||<�|=|j|j
d	|�yjy&|j|j
�|j|j��Wn1tk
rR}zt|��WYdd}~XnX|j�}	Wn|j��YnX|jr�|jj�d|_|j�|	_|	j|	_|	S)
Nz
no host givenrGc3s-|]#\}}|�kr||fVqdS)NrI)r�r�r�)rcrIrJr��s	z.AbstractHTTPHandler.do_open.<locals>.<genexpr>r��
Connectioncss'|]\}}|j�|fVqdS)N)�title)r�rYr�rIrIrJr��szProxy-Authorizationrc)rtrrGZset_debuglevelr~r�rvr�rcryrxZ
set_tunnelrsr�r�rFrk�getresponser��sockr�rE�reasonr�)
r~Z
http_classr�Zhttp_conn_argsrtr�Ztunnel_headersZproxy_auth_hdr�errrrI)rcrJr��s>	/
	&
	
	zAbstractHTTPHandler.do_openN)r�r�r�r�r�r�r�rIrIrIrJr}�s(r}c@s%eZdZdd�ZejZdS)r)cCs|jtjj|�S)N)r�r�r��HTTPConnection)r~r�rIrIrJ�	http_openszHTTPHandler.http_openN)r�r�r�r�r}r�rPrIrIrIrJr)�sr�c@s:eZdZddddd�Zdd�ZejZdS)rBrNcCs&tj||�||_||_dS)N)r}r��_context�_check_hostname)r~rr>�check_hostnamerIrIrJr�
s	zHTTPSHandler.__init__cCs(|jtjj|d|jd|j�S)Nr>r�)r�r�r�r�r�r�)r~r�rIrIrJ�
https_openszHTTPSHandler.https_open)r�r�r�r�r�r}r�rTrIrIrIrJrBsrBc@sCeZdZddd�Zdd�Zdd�ZeZeZdS)rNcCs4ddl}|dkr'|jj�}||_dS)Nr)Zhttp.cookiejar�	cookiejarZ	CookieJar)r~r�r�rIrIrJr�szHTTPCookieProcessor.__init__cCs|jj|�|S)N)r�Zadd_cookie_header)r~rsrIrIrJrPsz HTTPCookieProcessor.http_requestcCs|jj||�|S)N)r�Zextract_cookies)r~rsr�rIrIrJr�"sz!HTTPCookieProcessor.http_response)r�r�r�r�rPr�rTr�rIrIrIrJrs
c@seZdZdd�ZdS)r.cCs|j}td|��dS)Nzunknown url type: %s)r�r)r~r�r�rIrIrJr�*s	zUnknownHandler.unknown_openN)r�r�r�r�rIrIrIrJr.)scCsmi}x`|D]X}|jdd�\}}|ddkr[|ddkr[|dd�}|||<q
W|S)N�=rOrr=rPrP)r?)�lZparsedZeltr�r�rIrIrJr^.s
 r^cCs�g}d}d}}x�|D]�}|r<||7}d}q|ry|dkrZd}qn|dkrld}||7}q|dkr�|j|�d}q|dkr�d}||7}qW|r�|j|�dd�|D�S)	NrlF�\Tr=�,cSsg|]}|j��qSrI)rO)r��partrIrIrJr%as	z#parse_http_list.<locals>.<listcomp>)r[)rl�resr��escaper	ZcurrIrIrJr`8s4	





r`c@s:eZdZdd�ZdZdd�Zdd�ZdS)r*cCs�|j}|dd�dkrq|dd�dkrq|jrq|jdkrq|j|j�kr~td��n
|j|�SdS)Nr�z//r:r��	localhostz-file:// scheme is supported only on localhost)r�rt�	get_namesr�open_local_file)r~r�rErIrIrJ�	file_openes	5zFileHandler.file_openNcCsztjdkrsy7ttjd�dtjtj��d�t_Wn*tjk
rrtjd�ft_YnXtjS)Nr�r�)r*�namesr&r��gethostbyname_ex�gethostname�gaierror�
gethostbyname)r~rIrIrJr�ps$zFileHandler.get_namescCs^ddl}ddl}|j}|j}t|�}y�tj|�}|j}|jj	|j
dd�}	|j|�d}
|jd|
p�d||	f�}|r�t
|�\}}|s�|rt|�|j�kr|r�d||}
n
d|}
tt|d�||
�SWn1tk
rM}zt|��WYdd}~XnXtd��dS)	Nr�usegmtTz6Content-type: %s
Content-length: %d
Last-modified: %s
z
text/plainzfile://�rbzfile not on local host)�email.utils�	mimetypesrtr�r4rT�stat�st_size�utils�
formatdate�st_mtime�
guess_type�message_from_stringr
�_safe_gethostbynamer�rrDrkr)r~r��emailr�rtr`Z	localfile�statsrg�modified�mtypercr0Zorigurl�exprIrIrJr�{s0			
zFileHandler.open_local_file)r�r�r�r�r�r�r�rIrIrIrJr*cs
cCs1ytj|�SWntjk
r,dSYnXdS)N)r�r�r�)rtrIrIrJr��sr�c@s(eZdZdd�Zdd�ZdS)r+cCs�ddl}ddl}|j}|s3td��t|�\}}|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�}y|j||||||j�}
|r�dp�d}xJ|
D]B}t|�\}}|j�dkr�|dkr�|j�}q�W|
j||�\}}d}|j|j�d}|rG|d
|7}|dk	rm|dkrm|d|7}tj|�}t|||j�SWnQ|jk
r�}z.td|�}|jtj �d��WYdd}~XnXdS)Nrzftp error: no host givenrlr�rOrS�Dr��a�Ar�r{zContent-type: %s
zContent-length: %d
z
ftp error: %rr�rPrP)r�r�r�rSr{r�)!�ftplibr�rtrr
�FTP_PORTr\rrr
r�r�rkrr�r?r��map�connect_ftprGrrr�upper�retrfiler�rnr�r�r�
all_errors�with_traceback�sys�exc_info)r~r�r�r�rtr0rr!r�rU�attrs�dirsrKZfwr��attrr�rb�retrlenrcr�r��excrIrIrJ�ftp_open�s\	!
zFTPHandler.ftp_openc	Cst||||||dd�S)N�
persistentF)�
ftpwrapper)r~rr!rtr0r�rGrIrIrJr��szFTPHandler.connect_ftpN)r�r�r�r�r�rIrIrIrJr+�s5c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)r,cCs1i|_i|_d|_d|_d|_dS)Nr�<rd)�cacherG�soonest�delay�	max_conns)r~rIrIrJr��s
				zCacheFTPHandler.__init__cCs
||_dS)N)r�)r~�trIrIrJ�
setTimeout�szCacheFTPHandler.setTimeoutcCs
||_dS)N)r�)r~r�rIrIrJ�setMaxConns�szCacheFTPHandler.setMaxConnscCs�|||dj|�|f}||jkrJtj�|j|j|<n<t||||||�|j|<tj�|j|j|<|j�|j|S)Nr�)�joinr�rer�rGr��check_cache)r~rr!rtr0r�rGrrIrIrJr��s
zCacheFTPHandler.connect_ftpcCstj�}|j|krrxTt|jj��D]=\}}||kr1|j|j�|j|=|j|=q1Wtt|jj���|_t	|j�|j
krxGt|jj��D]0\}}||jkr�|j|=|j|=Pq�Wtt|jj���|_dS)N)rer�r�rGryr�r��min�valuesr^r�)r~r�r�r�rIrIrJr��s"
"

zCacheFTPHandler.check_cachecCsBx!|jj�D]}|j�qW|jj�|jj�dS)N)r�r�r��clearrG)r~�connrIrIrJ�clear_caches
zCacheFTPHandler.clear_cacheN)	r�r�r�r�r�r�r�r�r�rIrIrIrJr,�sc@seZdZdd�ZdS)r-cCs�|j}|jdd�\}}|jdd�\}}t|�}|jd�rstj|�}|dd�}|sd}tjd|t|�f�}t	t
j|�||�S)	N�:rOr�z;base64�ztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
i����)rnr?r�endswithr�decodebytesr�r�r^r�io�BytesIO)r~r�rErrFZ	mediatypercrIrIrJ�	data_opens
		zDataHandler.data_openN)r�r�r�r�rIrIrIrJr-sr��nt)r4r3cCs
t|�S)N)r
)�pathnamerIrIrJr42scCs
t|�S)N)r	)r�rIrIrJr37sc@s+eZdZdZdeZddd�Zdd�Zdd�Zd	d
�Z	dd�Z
dd
d�Zddd�Zddd�Z
ddddd�Zdd�Zddd�Zddd�Zdd�Zer�dd�Zddd �Zd!d"�Zd#d$�Zd%d&�Zdd'd(�ZdS))r8NzPython-urllib/%scKs�dd|jji}tj|tdd�|dkrAt�}||_|jd�|_|jd�|_	d|j
fd
g|_g|_t
j|_d|_t|_dS)NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methods�class�
stacklevelr:�key_file�	cert_filez
User-Agent�Accept�*/*)r�r�)r9r�rCrD�DeprecationWarningr5rr�r�r��versionr��_URLopener__tempfilesrTrj�_URLopener__unlink�	tempcache�ftpcache)r~rZx509r�rIrIrJr�Ms				zURLopener.__init__cCs|j�dS)N)r�)r~rIrIrJ�__del__gszURLopener.__del__cCs|j�dS)N)�cleanup)r~rIrIrJr�jszURLopener.closecCsm|jrSx7|jD],}y|j|�Wqtk
r>YqXqW|jdd�=|jri|jj�dS)N)r�r�rkr�r�)r~rKrIrIrJr�ms	
		zURLopener.cleanupcGs|jj|�dS)N)r�r[)r~r�rIrIrJ�	addheader{szURLopener.addheadercCs�tt|��}t|dd�}|jrn||jkrn|j|\}}t|d�}t|||�St|�\}}|s�d}||jkr�|j|}t|�\}}	t|	�\}
}|
|f}nd}d|}||_	|j
dd�}t||�s"|dkrK|r;|j|||�S|j
||�Sy9|dkrmt||�|�St||�||�SWnattfk
r��YnGtk
r�}
z'td	|
�jtj�d
��WYdd}
~
XnXdS)Nrz%/:=&?~#+!$,;'@()*[]|r�rKZopen_�-r�r�zsocket errorr�)rrr	r�rDrrrrr�r�r��open_unknown_proxy�open_unknownr�rrrkr�r�r�)r~r�rFr`rcrb�urltyperEr�	proxyhostrtr�rYr�rIrIrJrD�s<

	zURLopener.opencCs(t|�\}}tdd|��dS)Nz	url errorzunknown url type)rrk)r~r�rFr�rErIrIrJr��szURLopener.open_unknowncCs,t|�\}}tdd||��dS)Nz	url errorzinvalid proxy for %s)rrk)r~rr�rFr�rErIrIrJr��szURLopener.open_unknown_proxyc Cs�tt|��}|jr5||jkr5|j|St|�\}}|dkr�|sf|dkr�yC|j|�}|j�}|j�tt|�d�|fSWn%t	k
r�}	zWYdd}	~	XnX|j
||�}z�|j�}
|r
t
|d�}n�ddl}t|�\}
}t|p4d�\}
}t|pLd�\}}
t
|pdd�\}}
tjj|�d}|j|�\}}|jj|�tj|d�}z�||
f}|jdk	r�||j|<d}d}d}d}d|
krt|
d	�}|r2||||�xW|j|�}|sKP|t|�7}|j|�|d7}|r5||||�q5WWd|j�XWd|j�X|dkr�||kr�td
||f|��|S)
NrKrOrLrrlirNzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rP)rrr�rr�rSr�r4rrkrDrWrrrTrU�splitext�mkstempr�r[�fdopenr\r]r^r_r)r~rEr`rarFr�Zurl1rbr�r�rcrdrWZgarbagerU�suffix�fdrerfrgr]rhrirIrIrJ�retrieve�sl



zURLopener.retrievecCs�d}d}t|t�rZt|�\}}|rQt|�\}}t|�}|}n�|\}}t|�\}}t|�\}	}
|
}d}|	j�dkr�d}nUt|
�\}}
|r�t|�\}}|r�d|	||
f}t|�r|}|stdd��|rNt|�}t	j
|j��jd�}nd}|r�t|�}t	j
|j��jd�}nd}||�}
i}|r�d||d<|r�d||d<|r�||d	<d
|d<x!|j
D]\}}|||<q�W|dk	r4d|d
<|
jd|||�n|
jd|d|�y|
j�}Wn$tjjk
r�td��YnXd|jko�dknr�t||jd||j�S|j||j|j|j|j|�SdS)Nr�z	%s://%s%sz
http errorz
no host givenrzBasic %szProxy-AuthorizationrNrmr�r�z!application/x-www-form-urlencodedzContent-Typer�r�rcz$http protocol error: bad status liner�i,zhttp:)r�r�rrr
rrrrrkrrrr r�rsr�r�r�Z
BadStatusLiner�statusrr��
http_errorrbr�)r~Zconnection_factoryrErFZuser_passwdZproxy_passwdrtr�Zrealhostr�r�Z
proxy_authrMZ	http_connrc�headerr�r�rIrIrJ�_open_generic_http�sr
		!!



	zURLopener._open_generic_httpcCs|jtjj||�S)N)r�r�r�r�)r~rErFrIrIrJ�	open_httpNszURLopener.open_httpc
Cs�d|}t||�rtt||�}|dkrO||||||�}	n|||||||�}	|	rt|	S|j|||||�S)Nz
http_error_%d)r�r�r�)
r~rErb�errcode�errmsgrcrFrYr}rerIrIrJr�Rs
zURLopener.http_errorcCs&|j�t||||d��dS)N)r�r)r~rErbrrrcrIrIrJr�bs
zURLopener.http_error_defaultcCs"tjj|d|jd|j�S)Nr�r�)r�r�r�r�r�)r~rtrIrIrJ�_https_connectionhs	zURLopener._https_connectioncCs|j|j||�S)N)r�r)r~rErFrIrIrJ�
open_httpsmszURLopener.open_httpscCs�t|t�std��|dd�dkrr|dd�dkrr|dd�j�dkrrtd��n
|j|�SdS)	NzEfile error: proxy support for file protocol currently not implementedr�z//r:r��z
localhost/z-file:// scheme is supported only on localhost)r�r�rrrr?r�)r~rErIrIrJ�	open_fileqs
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|}
tt|d	�||
�St|�\}}|r�tj|�t�ft�kr�|}
|dd�dkr�d|}
n&|dd
�dkr�td|��tt|d	�||
�Std
��dS)Nrr�Tz6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainrOr�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r4rTr�rkr�strerrorr`r�r�r�r�r�r�rrDr
r�r�r��thishostr?)r~rEr�r�rtrKZ	localnamer��ergr�r�rcZurlfiler0rIrIrJr�zs:(	
"
zURLopener.open_local_filecCsst|t�std��ddl}t|�\}}|sKtd��t|�\}}t|�\}}|r�t|�\}}nd}t|�}t|p�d�}t|p�d�}t	j
|�}|s�ddl}|j}nt
|�}t|�\}}	t|�}|jd�}
|
dd�|
d}
}|
rb|
drb|
dd�}
|
r}|
dr}d|
d<|||dj|
�f}t|j�tkr�xDt|j�D]3}
|
|kr�|j|
}|j|
=|j�q�Wy-||jkr%t|||||
�|j|<|s4d}nd}xJ|	D]B}t|�\}}|j�d	krA|dkrA|j�}qAW|j|j||�\}}|jd|�d}d}|r�|d|7}|dk	r�|dkr�|d|7}tj|�}t||d|�SWnKt�k
rn}z(td|�j t!j"�d��WYdd}~XnXdS)NzCftp error: proxy support for ftp protocol currently not implementedrzftp error: no host givenrlr�rOr�rSr�r�r�r�r{zftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr�rPrP)r�r�r�rSr{r�)#r�r�rr�rr
rrr
r�r�r�r�r\rr?r�r^r��MAXFTPCACHEr�r�r�rrrr�r�r�r�r�r�	ftperrorsr�r�r�)r~rEr�rtrUr0rr!r�r�r�rKrr�r�r�r�r�rbr�r�rcr�rIrIrJ�open_ftp�sp


	
zURLopener.open_ftpc	Cs�t|t�std��y|jdd�\}}Wn!tk
rZtdd��YnX|sgd}|jd�}|dkr�d	||d�kr�||dd�}|d|�}nd
}g}|jdtj	dtj
tj����|jd
|�|dkr8tj|j
d��jd�}nt|�}|jdt|��|jd
�|j|�dj|�}tj|�}tj|�}t|||�S)NzEdata error: proxy support for data protocol currently not implementedr�rOz
data errorzbad data URLztext/plain;charset=US-ASCII�;rr�rlzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %d�
)r�r�rr?r?rk�rfindr[re�strftime�gmtimerr�rr r
r^r�r�r�r��StringIOr)	r~rErFr�Zsemirr�rc�frIrIrJ�	open_data�s6
"$

zURLopener.open_data)r�r�r�r�r�r�r�r�r�r�r�rDr�r�r�r�rr�r�r@rrrr�rrrIrIrIrJr8@s,
$B\	 :c@s�eZdZdd�Zdd�Zddd�Zdd	�Zdd
d�Zddd
�Zddd�Z	dddd�Z
dddd�Zddd�Zddd�Z
ddd�Zddd�Zddd�Zd d!�ZdS)"r9cOs2tj|||�i|_d|_d|_dS)Nrr�)r8r��
auth_cache�tries�maxtries)r~r�r8rIrIrJr�s		zFancyURLopener.__init__cCst||d||�S)Nzhttp:)r)r~rErbrrrcrIrIrJr�sz!FancyURLopener.http_error_defaultNc	
Cs�|jd7_z{|jrg|j|jkrgt|d�rH|j}n	|j}|||dd|�S|j||||||�}|SWdd|_XdS)NrO�http_error_500i�z)Internal Server Error: Redirect Recursionr)rrr�rr��redirect_internal)	r~rErbrrrcrFr�rerIrIrJrs	zFancyURLopener.http_error_302c	Cs�d|kr|d}nd|kr2|d}ndS|j�t|jd||�}t|�}|jd	kr�t|||d|||��|j|�S)
Nr�r�r�r�r�r�rlz( Redirection to url '%s' is not allowed.)r�r�r�rl)r�rr�rrrrD)	r~rErbrrrcrFr�r	rIrIrJrs


	z FancyURLopener.redirect_internalcCs|j||||||�S)N)r)r~rErbrrrcrFrIrIrJr9szFancyURLopener.http_error_301cCs|j||||||�S)N)r)r~rErbrrrcrFrIrIrJr
=szFancyURLopener.http_error_303cCsE|dkr(|j||||||�S|j|||||�SdS)N)rr�)r~rErbrrrcrFrIrIrJrAszFancyURLopener.http_error_307Fc
Csd|kr(tj||||||�|d}tjd|�}	|	sftj||||||�|	j�\}
}|
j�dkr�tj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)Nzwww-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r<Zretry_�_basic_auth)r8r�rQ�matchrBrrr�r�)
r~rErbrrrcrFr|�stuffrrr'rYrIrIrJrUHs&



zFancyURLopener.http_error_401c
Csd|kr(tj||||||�|d}tjd|�}	|	sftj||||||�|	j�\}
}|
j�dkr�tj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)Nzproxy-authenticatez![ 	]*([^ 	]+)[ 	]+realm="([^"]*)"r<Zretry_proxy_r)r8r�rQrrBrrr�r�)
r~rErbrrrcrFr|rrrr'rYrIrIrJrVas&



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��@rOz%s:%s@%srrl)rrrr��get_user_passwdr	rD)r~rEr'rFrtr�r�rr�r��
proxyselectorr�rr!rIrIrJ�retry_proxy_http_basic_authzs 

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�rrOz%s:%s@%srrl)rrrr�rr	rD)r~rEr'rFrtr�r�rr�r�rr�rr!rIrIrJ�retry_proxy_https_basic_auth�s 

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)NrrOz%s:%s@%srrlzhttp://)rr�rr	rD)
r~rEr'rFrtr�r�rr!r�rIrIrJrF�s
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)NrrOz%s:%s@%srrlzhttps://)rr�rr	rD)
r~rEr'rFrtr�r�rr!r�rIrIrJ�retry_https_basic_auth�s
z%FancyURLopener.retry_https_basic_authrcCs�|d|j�}||jkrA|r6|j|=n|j|S|j||�\}}|se|rx||f|j|<||fS)Nr)rrr�prompt_user_passwd)r~rtr'r�rrr!rIrIrJr�s
zFancyURLopener.get_user_passwdcCspddl}y@td||f�}|jd|||f�}||fSWntk
rkt�dSYnXdS)NrzEnter username for %s at %s: z#Enter password for %s in %s at %s: )NN)�getpass�input�KeyboardInterrupt�print)r~rtr'r$rr!rIrIrJr#�s	
z!FancyURLopener.prompt_user_passwd)r�r�r�r�r�rrrr
rrUrVr r!rFr"rr#rIrIrIrJr9�s"cCstdkrtjd�atS)Nr�)�
_localhostr�r�rIrIrIrJr��sr�cCsdtdkr`y#ttjtj��d�aWn.tjk
r_ttjd�d�aYnXtS)Nr�r�)�	_thishostr&r�r�r�r�rIrIrIrJr�s#rcCs%tdkr!ddl}|jatS)Nr)�
_ftperrorsr�r�)r�rIrIrJr�s	rcCstdkrtjd�atS)Nrl)�
_noheadersr�r�rIrIrIrJ�	noheaders�sr,c@sjeZdZdddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dS)r�NTc	Csr||_||_||_||_||_||_d|_||_y|j�Wn|j	��YnXdS)Nr)
rr!rtr0r�rG�refcount�	keepalive�initr�)r~rr!rtr0r�rGr�rIrIrJr��s								
zftpwrapper.__init__cCs�ddl}d|_|j�|_|jj|j|j|j�|jj|j	|j
�dj|j�}|jj
|�dS)Nrr�)r��busyZFTPr��connectrtr0rGZloginrr!r�r��cwd)r~r��_targetrIrIrJr/	s	zftpwrapper.initc-Cs1ddl}|j�|dkr1d}d}nd|}d}y|jj|�Wn/|jk
r�|j�|jj|�YnXd}|r*|r*y&d|}|jj|�\}}Wng|jk
r)}zDt|�dd�d	krt	d
|�j
tj�d��WYdd}~XnX|s�|jjd�|r�|jj
�}	zVy|jj|�Wn;|jk
r�}zt	d
|�|�WYdd}~XnXWd|jj|	�Xd|}nd
}|jj|�\}}d|_t|jd�|j�}
|jd7_|j�|
|fS)Nrr{r�zTYPE ArOzTYPE zRETR r:Z550z
ftp error: %rr�zLIST �LISTr�)r{r�)r��endtransferr�Zvoidcmdr�r/ZntransfercmdZ
error_permr�rr�r�r��pwdr2r0r�makefile�
file_closer-r�)r~rKr�r��cmd�isdirr�r�r�r6ZftpobjrIrIrJr�	sN
	



&*
	
zftpwrapper.retrfilecCs
d|_dS)Nr)r0)r~rIrIrJr5D	szftpwrapper.endtransfercCs&d|_|jdkr"|j�dS)NFr)r.r-�
real_close)r~rIrIrJr�G	s	zftpwrapper.closecCs@|j�|jd8_|jdkr<|jr<|j�dS)NrOr)r5r-r.r;)r~rIrIrJr8L	s
zftpwrapper.file_closecCs7|j�y|jj�Wnt�k
r2YnXdS)N)r5r�r�r)r~rIrIrJr;R	s

zftpwrapper.real_close)
r�r�r�r�r/r�r5r�r8r;rIrIrIrJr��s	-r�cCs�i}xYtjj�D]H\}}|j�}|r|dd�dkr|||dd�<qWdtjkr�|jdd�xvtjj�D]e\}}|dd�dkr�|j�}|r�|||dd�<q�|j|dd	�d�q�W|S)
N��_proxyZREQUEST_METHODr�i����i����i����i����i����)rT�environryrrr�)rrYr�rIrIrJ�getproxies_environmentZ	s	r?c
Cs�|dkrt�}y|d}Wntk
r;dSYnX|dkrLdSt|�\}}dd�|jd�D�}xp|D]h}|r�|jd�}tj|�}d	|}tj||tj�s�tj||tj�r�dSq�WdS)
N�nor�*rOcSsg|]}|j��qSrI)rO)r�rrIrIrJr%�	s	z,proxy_bypass_environment.<locals>.<listcomp>r��.z
(.+\.)?%s$)	r?rtr
r?�lstriprQr�rrS)rtrZno_proxy�hostonlyr0Z
no_proxy_listrY�patternrIrIrJ�proxy_bypass_environmenty	s&	
	

rFc
Cszddlm}t|�\}}dd�}d|krH|drHdSd}x%|jdf�D]}|spqatjd	|�}|dk	r_|dkr�ytj|�}||�}Wntk
r�waYnX||jd
��}	|jd�}
|
dkr$d|jd
�j	d�d
}
nt
|
d
d��}
d
|
}
||
?|	|
?krrdSqa|||�radSqaWdS)Nr)�fnmatchcSs�|jd�}ttt|��}t|�dkrV|ddddgdd�}|dd>|dd>B|dd>B|d	BS)
NrBr�r�rOrdr�rNr:)r?r�r�r\r^)ZipAddrr/rIrIrJ�ip2num�	s
 z,_proxy_bypass_macosx_sysconf.<locals>.ip2numrBZexclude_simpleT�
exceptionsz(\d+(?:\.\d+)*)(/\d+)?rOr�rN� F)rGr
r�rQrr�r�rk�group�countr\)rt�proxy_settingsrGrDr0rIZhostIPr�r�r3�maskrIrIrJ�_proxy_bypass_macosx_sysconf�	s:

#
rP�darwin)�_get_proxy_settings�_get_proxiescCst�}t||�S)N)rRrP)rtrNrIrIrJ�proxy_bypass_macosx_sysconf�	s	rTcCst�S)N)rSrIrIrIrJ�getproxies_macosx_sysconf�	srUcCs*t�}|rt||�St|�SdS)N)r?rFrT)rtrrIrIrJr�	s	
rcCst�pt�S)N)r?rUrIrIrIrJr5�	scCsfi}yddl}Wntk
r.|SYnXy|j|jd�}|j|d�d}|r9t|j|d�d�}d|kr�x�|jd�D]J}|jdd�\}}tjd|�s�d	||f}|||<q�WnM|dd
�dkr||d<n*d
||d<d||d<d||d<|j	�Wnt
ttfk
raYnX|S)Nrz;Software\Microsoft\Windows\CurrentVersion\Internet Settings�ProxyEnableZProxyServerr�r
rOz^([^/:]+)://z%s://%sr[zhttp:r�z	http://%sz
https://%sr�zftp://%sr�)
�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr�r?rQrZCloserkr?r�)rrW�internetSettings�proxyEnableZproxyServer�pr��addressrIrIrJ�getproxies_registry�	s8
			

r`cCst�pt�S)N)r?r`rIrIrIrJr5(
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
|�Wntk
r�YnXy,tj|�}||kr&|j
|�Wntk
r;YnX|jd�}x�|D]�}	|	dkrtd|krtdS|	j
dd	�}	|	j
d
d�}	|	j
dd�}	x*|D]"}
tj|	|
tj�r�dSq�WqRWdS)
Nrz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsrVZ
ProxyOverrider
z<local>rBrOz\.rAz.*�?)rWrXrYrZr[r�rkr
r�r�r[�getfqdnr?r�rQrrS)rtrWr\r]Z
proxyOverrideZrawHostr0�addrZfqdnr4r�rIrIrJ�proxy_bypass_registry1
sR
			

		



rdcCs*t�}|rt||�St|�SdS)N)r?rFrd)rtrrIrIrJrc
s	
)~rr�r�rhZhttp.clientr�r�rTr1rQr�rr�rer�rWrQrCZurllib.errorrrrZurllib.parserrrrr	r
rrr
rrrrrrrrrZurllib.responserrrArXr@�__all__r�r�rCr�r0r1rZr6r7rR�ASCIIrprurrr2rr/rrrrr r!r"r#r$r%�urandomrgr&r'r(r}r)r�r�rBr[rr.r^r`r*r�r+r,r-r
rYZ
nturl2pathr4r3r�r8r9r(r�r)rr*rr+r,r�r?rFrP�platformZ_scproxyrRrSrTrUrr5r`rdrIrIrIrJ�<module>Ts�v
					!?n�$q*@
P	�r


+3:5!���

_#<

-	2

?>