????
Your IP : 18.221.158.39
�qc{�)@s�dZddlZddlZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlmZmZmZddlmZmZmZmZmZmZmZmZm Z m!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
ddd
ddddddddddddddddddd d!d"d#d$d%d&d'd(g!Z0e
j1dd)�Z2da3dej4d*dd+dd,dd-dd.d�Z5d/d �Z6gZ7dddd0d%�Z8d1d&�Z9e
j:d2e
j;�Z<d3d4�Z=Gd5d�d�Z>Gd6d �d �Z?d7d!�Z@Gd8d
�d
�ZAGd9d�deA�ZBGd:d�deA�ZCGd;d�deA�ZDd<d=�ZEGd>d�deA�ZFGd?d�d�ZGGd@d�deG�ZHGdAd�deH�ZIGdBd�d�ZJGdCd�deJeA�ZKGdDd�deJeA�ZLejMZNGdEd�d�ZOGdFd�deAeO�ZPGdGd�deAeO�ZQGdHdI�dIeA�ZRGdJd�deR�ZSeTejUdK�r�GdLdM�dMeR�ZVe0jWdM�GdNd
�d
eA�ZXGdOd�deA�ZYdPdQ�ZZdRdS�Z[GdTd�deA�Z\dUdV�Z]GdWd�deA�Z^GdXd�de^�Z_GdYd�deA�Z`dZZaejbd[kr`dd\lcmdZdmeZend]d#�Zdd^d"�ZeiZfGd_d'�d'�ZgGd`d(�d(eg�Zhdaidadb�Zjdakdcdd�Zldamdedf�Zndaodgdh�ZpGdidj�dj�Zqdkdl�Zrddmdn�Zsdodp�Zte
judqkr�ddrlvmwZwmxZxdsdt�Zydudv�Zzdwdx�Z{dyd$�Z|nNejbd[kr�dzd{�Z}d|d$�Z|d}d~�Z~ddx�Z{nerZ|esZ{dS)�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�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�3/opt/alt/python35/lib64/python3.5/urllib/request.pyr0�s$ cCs
|adS)N)rC)rHrIrIrJr1�scCs�t|�\}}tjt||���:}|j�}|dkrb|rbtjj|�|fS|rzt|d�}n(t j
dd�}|j}tj
|�|��||f} d
}
d}d}d}
d |kr�t|d
�}|r�||
|
|�xW|j|
�}|sP|t|�7}|j|�|
d7}
|r||
|
|�qWWdQRXWdQRX|dkr�||kr�td||f| ��| 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�closingr0�info�os�path�normpathrD�tempfileZNamedTemporaryFile�name�_url_tempfiles�append�int�read�len�writer)rE�filename�
reporthookrFZurl_typerT�fp�headers�tfp�result�bs�sizer[�blocknum�blockrIrIrJr6�sD
"cCsTx4tD],}ytj|�Wqtk
r2YqXqWtdd�=trPdadS)z0Clean up temporary files from urlretrieve calls.N)rXrS�unlink�OSErrorrC)Z temp_filerIrIrJr7�s
z:\d+$cCsV|j}t|�d}|dkr7|jdd�}tjd|d�}|j�S)z�Return 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�hostrIrIrJ�request_host�s rsc@s*eZdZdiddddd�Zedd��Zejdd��Zejdd��Zed d
��Zejdd
��Zejdd
��Zd
d�Z dd�Z
dd�Zdd�Zdd�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)rlra�unredirected_hdrs�_datarF�_tunnel_host�items�
add_headerrs�origin_req_host�unverifiable�method) �selfrErFraryrzr{�key�valuerIrIrJ�__init__
s zRequest.__init__cCs&|jrdj|j|j�S|jS)Nz{}#{})�fragment�format� _full_url)r|rIrIrJrls zRequest.full_urlcCs8t|�|_t|j�\|_|_|j�dS)N)rr�rr��_parse)r|rErIrIrJrl"scCsd|_d|_d|_dS)Nrj)r�r��selector)r|rIrIrJrl)s cCs|jS)N)ru)r|rIrIrJrF/szRequest.datacCs8||jkr4||_|jd�r4|jd�dS)NzContent-length)ru�
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?rlrrrr�r
)r|�restrIrIrJr�As zRequest._parsecCs+|jdk rdnd}t|d|�S)z3Return a string indicating the HTTP request method.N�POST�GETr{)rF�getattr)r|Zdefault_methodrIrIrJ�
get_methodIszRequest.get_methodcCs|jS)N)rl)r|rIrIrJ�get_full_urlNszRequest.get_full_urlcCsJ|jdkr(|jr(|j|_n||_|j|_||_dS)N�https)r�rvrrrlr�)r|rrr�rIrIrJ� set_proxyQs
zRequest.set_proxycCs|j|jkS)N)r�rl)r|rIrIrJ� has_proxyYszRequest.has_proxycCs||j|j�<dS)N)ra�
capitalize)r|r}�valrIrIrJrx\szRequest.add_headercCs||j|j�<dS)N)rtr�)r|r}r�rIrIrJ�add_unredirected_header`szRequest.add_unredirected_headercCs||jkp||jkS)N)rart)r|�header_namerIrIrJr�dszRequest.has_headercCs"|jj||jj||��S)N)ra�getrt)r|r��defaultrIrIrJrmhs zRequest.get_headercCs*|jj|d�|jj|d�dS)N)ra�poprt)r|r�rIrIrJr�mszRequest.remove_headercCs/|jj�}|j|j�t|j��S)N)rt�copy�updatera�listrw)r|�hdrsrIrIrJ�header_itemsqszRequest.header_items)�__name__�
__module__�__qualname__r�propertyrl�setter�deleterrFr�r�r�r�r�rxr�r�rmr�r�rIrIrIrJrs(
c@speZdZdd�Zdd�Zdd�Zdd�Zd ejd
d�Z d dd
�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_versionrIrIrJrws
zOpenerDirector.__init__cCs�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!|dkr8|}|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�_rN�errorrD�responserqT)r�r�r�)�hasattr� TypeErrorr��dir�find�
startswithrZr?r�r�r�r�r��
setdefault�bisectZinsortrYr�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��funcrcrIrIrJ�_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�Zdefault_openr��unknown�unknown_open)r�r�r�)r|r�rFrcr�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_%srNZ_errorrr��http_error_default)r�r�)r�r�)r|�protor��dictr�Zhttp_errZ orig_argsrcrIrIrJr��s
zOpenerDirector.error)r�r�r�rr�r�r��socket�_GLOBAL_DEFAULT_TIMEOUTrDr�r�rIrIrIrJrvs/c Gs7t�}ttttttttt g }t
tjd�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)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��clientrYrB�setr�r��
issubclass�add�remover�)r�rHZdefault_classes�skip�klassZcheck�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(sc@s.eZdZdZdZdd�ZeZdS)r/zProcess HTTP error responses.i�cCsa|j|j|j�}}}d|ko7dkns]|jjd|||||�}|S)N��i,r�)�code�msgrRr�r�)r|rqr�r�r�r�rIrIrJ�
http_response?s
z HTTPErrorProcessor.http_responseN)r�r�r��__doc__r�r��https_responserIrIrIrJr/;sc@seZdZdd�ZdS)rcCst|j||||��dS)N)rrl)r|r�r`r�r�r�rIrIrJr�Msz*HTTPDefaultErrorHandler.http_error_defaultN)r�r�r�r�rIrIrIrJrLsc@sHeZdZdZdZdd�Zdd�ZeZZZ dZ
dS) r��
c s�|j�}|dkr$|dkp9|dko9|dksWt|j||||��|jdd �}d�t�fdd
�|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)rp)�.0�k�v)�CONTENT_HEADERSrIrJ� <genexpr>ss z7HTTPRedirectHandler.redirect_request.<locals>.<genexpr>raryrzT)r�r�r�r�)r�r�)r�r�r�)r�r�) r�rrl�replacer�rarwrry) r|r�r`r�r�ra�newurl�mZ
newheadersrI)r�rJr�Xs
( z$HTTPRedirectHandler.redirect_requestc
Cs�d|kr|d}nd|kr2|d}ndSt|�}|jdkrst||d||f||��|jr�|jr�t|�}d|d <t|�}t|d
ddtj �}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��ftprjz+%s - Redirection to url '%s' is not allowed�/r��encodingz
iso-8859-1�safe�
redirect_dictrrNrG)r�r�r�rj)r�schemerrTZnetlocr�rr �stringZpunctuationrrlr�r�r�r��max_repeatsr\�max_redirections�inf_msgr[r�r�rDrG)
r|r�r`r�r�rar��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)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�rNrO)rr�r?r�rr) �proxyrZr_scheme� authority�endZuserinfo�hostport�user�passwordrIrIrJ�_parse_proxy�s
rc@s1eZdZdZddd�Zdd�ZdS)r�dNc Cs�|dkrt�}t|d�s0td��||_x@|j�D]2\}}t|d||||jdd��qFWdS)N�keyszproxies must be a mappingz%s_opencSs||||�S)NrI)�rrr�r�rIrIrJ�<lambda>�sz'ProxyHandler.__init__.<locals>.<lambda>)r5r��AssertionError�proxiesrw�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�rrr�proxy_bypassr
�base64� b64encode�encode�decoderxr�r�rDrG)r|r�rr�Z orig_typeZ
proxy_typerrrZ user_passZcredsrIrIrJr��s zProxyHandler.proxy_open)r�r�r�r�rr�rIrIrIrJr�s
c@sOeZdZdd�Zdd�Zdd�Zddd �Zd
d�ZdS)
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)rr�r rw� 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�dddd ij|�} | dk r�d
|| f}||fS)z@Accept authority or URI and extract only the authority and path.rNrr�r�Nr��Pr�i�z%s:%d)rr
r�)
r|r�r"�partsrrrTrr�portZdportrIrIrJr "s
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.
TrFrN)� posixpath�commonprefixr\)r|�base�test�commonrIrIrJr(9szHTTPPasswordMgr.is_suburiN)r�r�r�rr'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!HscsReZdZ�fdd�Zd�fdd�Zddd�Zdd �Z�S)
r"cs i|_t�j||�dS)N)�
authenticated�superr)r|r��kwargs)� __class__rIrJrTs z%HTTPPasswordMgrWithPriorAuth.__init__FcsR|j||�|dk r5t�jd|||�t�j||||�dS)N)�update_authenticatedr5r')r|r%r�rr�is_authenticated)r7rIrJr'Xsz)HTTPPasswordMgrWithPriorAuth.add_passwordcCs]t|t�r|g}x>dD]6}x-|D]%}|j||�}||j|<q,WqWdS)NTF)TF)r�r�r r4)r|r�r9r"r!r&rIrIrJr8_s
z1HTTPPasswordMgrWithPriorAuth.update_authenticatedcCsXxQdD]I}|j||�}x.|jD]#}|j||�r)|j|Sq)WqWdS)NTF)TF)r r4r()r|r)r"r*r�rIrIrJr9is
z-HTTPPasswordMgrWithPriorAuth.is_authenticated)r�r�r�rr'r8r9rIrI)r7rJr"Rs
c@speZdZejdej�Zddd�Zdd�Zdd�Z d d
�Z
dd�Ze
ZeZ
dS)
r#z1(?:.*,)*[ ]*([^ ]+)[ ]+realm=(["']?)([^"']*)\2NcCs1|dkrt�}||_|jj|_dS)N)r rr')r|Zpassword_mgrrIrIrJrs 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��splitrpr?r#�rx�search�groups�warnings�warn�UserWarning�retry_http_basic_auth) r|�authreqrrr�rarZmor 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)rr+rrrrrm�auth_headerr�r�rDrG)r|rrr�r%r�pw�raw�authrIrIrJrD�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)Nr9�
Authorizationz{0}:{1}zBasic {})
r�rr9rlr�r+r�rrZstandard_b64encoderr��strip)r|r�rrZcredentialsZauth_strrIrIrJ�http_request�s z%AbstractBasicAuthHandler.http_requestcCsdt|jd�r`d|jko,dknrJ|jj|jd�n|jj|jd�|S)Nr9r�i,TF)r�rr�r8rl)r|r�r�rIrIrJr��s
z&AbstractBasicAuthHandler.http_response)r�r�r��re�compile�Ir>rrFrDrMr��
https_requestr�rIrIrIrJr#qs
c@s"eZdZdZdd�ZdS)r$rKcCs%|j}|jd|||�}|S)Nzwww-authenticate)rlrF)r|r�r`r�r�rarEr�rIrIrJ�http_error_401�s z#HTTPBasicAuthHandler.http_error_401N)r�r�r�rGrRrIrIrIrJr$�sc@s"eZdZdZdd�ZdS)r%zProxy-authorizationcCs%|j}|jd|||�}|S)Nzproxy-authenticate)rrrF)r|r�r`r�r�rarr�rIrIrJ�http_error_407�s z$ProxyBasicAuthHandler.http_error_407N)r�r�r�rGrSrIrIrIrJr%�sc@sseZdZddd�Zdd�Zdd�Zdd �Zd
d�Zdd
�Zdd�Z dd�Z
dS)r&NcCsL|dkrt�}||_|jj|_d|_d|_d|_dS)Nr)r rr'�retried�nonce_count�
last_nonce)r|rrIrIrJr�s z"AbstractDigestAuthHandler.__init__cCs
d|_dS)Nr)rT)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 failedrNrZdigestr:zEAbstractDigestAuthHandler does not support the following scheme: '%s')r�rTrrlr=rp�retry_http_digest_authr?)r|rGrrr�rarErrIrIrJrF�sz/AbstractDigestAuthHandler.http_error_auth_reqedcCs�|jdd�\}}ttdt|���}|j||�}|r�d|}|jj|jd�|krwdS|j|j|�|j j
|d|j�}|SdS)Nr�rNz Digest %srG)r=�parse_keqv_list�filter�parse_http_list�get_authorizationrar�rGr�r�rDrG)r|r�rJ�tokenZ challenge�chalZauth_valZresprIrIrJrY
s
z0AbstractDigestAuthHandler.retry_http_digest_authcCsXd|j|tj�f}|jd�td�}tj|�j�}|dd�S)Nz %s:%s:%s:rrM�)rU�timeZctimer�_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%rf�qop� algorithm�MD5�opaquez%s:%s:%sz%s:%srJrNz%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+rlrF�get_entity_digestr�r�rVrUrjr)r|r�r_r%rfrkrlrn�H�KDrrHZentdigZA1ZA2ZncvalueZcnonceZnoncebitZrespdigr1rIrIrJr]!sV
(
z+AbstractDigestAuthHandler.get_authorizationcsb|dkrdd��n+|dkr6dd��ntd|���fdd�}�|fS)NrmcSstj|jd��j�S)Nr)rcZmd5rre)�xrIrIrJr`sz?AbstractDigestAuthHandler.get_algorithm_impls.<locals>.<lambda>ZSHAcSstj|jd��j�S)Nr)rcrdrre)rtrIrIrJrbsz.Unsupported digest authentication algorithm %rcs�d||f�S)Nz%s:%srI)rg�d)rrrIrJrgs)r?)r|rlrsrI)rrrJrp]s
z-AbstractDigestAuthHandler.get_algorithm_implscCsdS)NrI)r|rFr_rIrIrJrqjsz+AbstractDigestAuthHandler.get_entity_digest)r�r�r�rrWrFrYrjr]rprqrIrIrIrJr&�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.
rKi�cCs9t|j�d}|jd|||�}|j�|S)NrNzwww-authenticate)rrlrFrW)r|r�r`r�r�rarr�retryrIrIrJrRys
z$HTTPDigestAuthHandler.http_error_401N)r�r�r�r�rGr�rRrIrIrIrJr'osc@s(eZdZdZdZdd�ZdS)r(zProxy-Authorizationi�cCs/|j}|jd|||�}|j�|S)Nzproxy-authenticate)rrrFrW)r|r�r`r�r�rarrrvrIrIrJrS�s
z%ProxyDigestAuthHandler.http_error_407N)r�r�r�rGr�rSrIrIrIrJr(�sc@sCeZdZddd�Zdd�Zdd�Zdd �Zd
S)�AbstractHTTPHandlerrcCs
||_dS)N)�_debuglevel)r|�
debuglevelrIrIrJr�szAbstractHTTPHandler.__init__cCs
||_dS)N)rx)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.zContent-typez!application/x-www-form-urlencodedzContent-lengthzBContent-Length should be specified for iterable data of type %r %rz%drk)rrrrFr�r�r�r�r��
memoryview�collections�Iterabler?r�r\�itemsizer�rr�rr�r�r�)r|rqrrrFr�ZmvZsel_hostrZselZsel_pathrWr~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|��WYd
d
}~XnX|j�} Wn|j��YnX|jr�|jj�d
|_|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 givenrGc3s-|]#\}}|�kr||fVqdS)NrI)r�r�r�)rarIrJr��s z.AbstractHTTPHandler.do_open.<locals>.<genexpr>r��
Connectioncss'|]\}}|j�|fVqdS)N)�title)r�rWr�rIrIrJr��szProxy-AuthorizationraN)rrrrGZset_debuglevelrxr�rtr�rarwrvZ
set_tunnelrqr�r�rFri�getresponser�Zsockr�rE�reasonr�)
r|Z
http_classr�Zhttp_conn_argsrrr�Ztunnel_headersZproxy_auth_hdr�errrrI)rarJr��s> /
&
zAbstractHTTPHandler.do_openN)r�r�r�rr{r�r�rIrIrIrJrw�s(rwc@s%eZdZdd�ZejZdS)r)cCs|jtjj|�S)N)r�r�r��HTTPConnection)r|r�rIrIrJ� http_openszHTTPHandler.http_openN)r�r�r�r�rwr�rMrIrIrIrJr)�sr�c@s:eZdZddddd�Zdd�ZejZdS)rBrNcCs&tj||�||_||_dS)N)rwr�_context�_check_hostname)r|ryr>�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�rr�rwr�rQrIrIrIrJrBsrBc@sCeZdZddd�Zdd�Zdd�ZeZeZdS)rNcCs4ddl}|dkr'|jj�}||_dS)Nr)Zhttp.cookiejar� cookiejarZ CookieJar)r|r�r�rIrIrJrszHTTPCookieProcessor.__init__cCs|jj|�|S)N)r�Zadd_cookie_header)r|rqrIrIrJrMsz HTTPCookieProcessor.http_requestcCs|jj||�|S)N)r�Zextract_cookies)r|rqr�rIrIrJr�"sz!HTTPCookieProcessor.http_response)r�r�r�rrMr�rQr�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.)scCsmi}x`|D]X}|jdd�\}}|ddkr[|ddkr[|dd�}|||<q
W|S)z>Parse list of key=value strings where keys are not duplicated.�=rNrr;rOrO)r=)�lZparsedZeltr�r�rIrIrJrZ.s
rZcCs�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) 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.
rjF�\Tr;�,cSsg|]}|j��qSrI)rL)r��partrIrIrJr#as z#parse_http_list.<locals>.<listcomp>)rY)rg�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�rr� 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}
|jd|
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� mimetypesrrr�r4rS�stat�st_size�utils�
formatdate�st_mtime�
guess_type�message_from_stringr
�_safe_gethostbynamer�rrDrir)r|r��emailr�rrr^Z localfile�statsre�modified�mtyperar.Zorigurl�exprIrIrJr�{s0
zFileHandler.open_local_file)r�r�r�r�r�r�r�rIrIrIrJr*cs
cCs1ytj|�SWntjk
r,dSYnXdS)N)r�r�r�)rrrIrIrJr��sr�c@s(eZdZdd�Zdd�ZdS)r+cCs�ddl}ddl}|j}|s3td��t|�\}}|dkr]|j}nt|�}t|�\}}|r�t|�\}}nd}t |�}|p�d}|p�d}yt
j|�}Wn1tk
r}zt|��WYdd}~XnXt
|j�\} }
| jd�}ttt |��}|dd�|d}}|r||dr||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 givenrjr�rNrP�Dr��a�Ar�ruzContent-type: %s
zContent-length: %d
z
ftp error: %rr�rOrO)r�r�r�rPrur�)!�ftplibr�rrrr
�FTP_PORTrZrrr
r�r�rirr�r=r��map�connect_ftprGrrp�upper�retrfiler�rlr�r�r�
all_errors�with_traceback�sys�exc_info)r|r�r�r�rrr.rrr�rT�attrs�dirsrKZfwr��attrr~r`�retrlenrar�r��excrIrIrJ�ftp_open�s\ !
zFTPHandler.ftp_openc Cst||||||dd�S)N�
persistentF)�
ftpwrapper)r|rrrrr.r�rGrIrIrJr��szFTPHandler.connect_ftpN)r�r�r�r�r�rIrIrIrJr+�s5c@sXeZdZdd�Zdd�Zdd�Zdd�Zd d
�Zdd�Zd
S)r,cCs1i|_i|_d|_d|_d|_dS)Nr�<r`)�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�rar�rGr��check_cache)r|rrrrr.r�rGr}rIrIrJr��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)rar�r�rGrwr�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�rr�r�r�r�r�rIrIrIrJr,�sc@seZdZdd�ZdS)r-cCs�|j}|jdd�\}}|jdd�\}}t|�}|jd�rstj|�}|dd�}|sd}tjd|t|�f�}t t
j|�||�S) N�:rNr�z;base64�ztext/plain;charset=US-ASCIIz$Content-type: %s
Content-length: %d
i����)rlr=r�endswithr�decodebytesr�r�r\r�io�BytesIO)r|r�rErrFZ mediatyperarIrIrJ� data_opens
zDataHandler.data_openN)r�r�r�r�rIrIrIrJr-sr��nt)r4r3cCs
t|�S)zOS-specific conversion from a relative URL of the 'file' scheme
to a file system path; not recommended for general use.)r
)�pathnamerIrIrJr42scCs
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�rIrIrJr37sc@s1eZdZdZdZdeZddd�Zdd�Zdd �Z d
d�Z
dd
�Zddd�Zddd�Z
ddd�Zddddd�Zdd�Zddd�Zddd�Zdd�Zer�dd�Zdd d!�Zd"d#�Zd$d%�Zd&d'�Zdd(d)�ZdS)*r8a,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�dd|jji}tj|tdd�|dkrAt�}t|d�s\td��||_|j d�|_
|j d�|_d |jfdg|_
g|_tj|_d|_t|_dS)
NzW%(class)s style of invoking requests is deprecated. Use newer urlopen functions/methods�class�
stacklevelr:rzproxies must be a mapping�key_file� cert_filez
User-Agent�Accept�*/*)r�r�)r7r�rArB�DeprecationWarningr5r�rrr�r�r��versionr��_URLopener__tempfilesrSrh�_URLopener__unlink� tempcache�ftpcache)r|rZx509r�rIrIrJrMs zURLopener.__init__cCs|j�dS)N)r�)r|rIrIrJ�__del__gszURLopener.__del__cCs|j�dS)N)�cleanup)r|rIrIrJr�jszURLopener.closecCsm|jrSx7|jD],}y|j|�Wqtk
r>YqXqW|jdd�=|jri|jj�dS)N)r�r�rir�r�)r|rKrIrIrJr�ms
zURLopener.cleanupcGs|jj|�dS)zdAdd a header to be used by the HTTP interface only
e.g. u.addheader('Accept', 'sound/basic')N)r�rY)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"|d krK|r;|j|||�S|j
||�Sy9|dkrmt||�|�St||�||�SWnattfk
r��YnGtk
r�}
z'td
|
�jtj�d��WYdd}
~
XnXdS)z6Use URLopener().open(file) instead of open(file, 'r').r�z%/:=&?~#+!$,;'@()*[]|r�rKNZopen_�-r�r�zsocket errorr�)rrr r�rDrrrrr�r�r��open_unknown_proxy�open_unknownr�rrrir�r�r�)r|r�rFr^rar`�urltyperEr� proxyhostrrr�rWr�rIrIrJrD�s<
zURLopener.opencCs(t|�\}}tdd|��dS)z/Overridable interface to open unknown URL type.z url errorzunknown url typeN)rri)r|r�rFr�rErIrIrJr��szURLopener.open_unknowncCs,t|�\}}tdd||��dS)z/Overridable interface to open unknown URL type.z url errorzinvalid proxy for %sN)rri)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)ztretrieve(url) returns (filename, headers) for a local object
or (tempfilename, headers) for a remote object.NrKrNrLrrjirMzcontent-lengthzContent-Lengthz1retrieval incomplete: got only %i out of %i bytesi rO)rrr�rr�rRr�r4rrirDrVrrrSrT�splitextZmkstempr�rY�fdopenrZr[r\r]r)r|rEr^r_rFr�Zurl1r`r�r�rarbrVZgarbagerT�suffix�fdrcrdrer[rfrgrIrIrJ�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��jd�}nd}|r�t|�}t j
|j��jd�}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)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-AuthorizationrKrkr�r�z!application/x-www-form-urlencodedzContent-Typer�r�raz$http protocol error: bad status liner�i,zhttp:)r�r�rrr
rrprrirrrrr�rqr�r�r�Z
BadStatusLinerZstatusrr��
http_errorr`r�)r|Zconnection_factoryrErFZuser_passwdZproxy_passwdrrr�Zrealhostr�r�Z
proxy_authrJZ http_connra�headerr~r�rIrIrJ�_open_generic_http�sr
!!
zURLopener._open_generic_httpcCs|jtjj||�S)zUse HTTP protocol.)r�r�r�r�)r|rErFrIrIrJ� open_httpNszURLopener.open_httpc
Cs�d|}t||�rtt||�}|dkrO||||||�} n|||||||�} | rt| S|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|rEr`�errcode�errmsgrarFrWr{rcrIrIrJr�Rs
zURLopener.http_errorcCs&|j�t||||d��dS)z>Default error handler: close the connection and raise OSError.N)r�r)r|rEr`r�r�rarIrIrJr�bs
zURLopener.http_error_defaultcCs"tjj|d|jd|j�S)Nr�r�)r�r�r�r�r�)r|rrrIrIrJ�_https_connectionhs zURLopener._https_connectioncCs|j|j||�S)zUse HTTPS protocol.)r�r�)r|rErFrIrIrJ�
open_httpsmszURLopener.open_httpscCs�t|t�std��|dd�dkrr|dd�dkrr|dd�j�dkrrtd ��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//r:r��z
localhost/z-file:// scheme is supported only on localhost)r�r�rrpr?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)zUse local file.rNr�Tz6Content-Type: %s
Content-Length: %d
Last-modified: %s
z
text/plainrNr�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�rr4rSr�rir�strerrorr^r�r�r�r�r�r�rrDr
r�r�r��thishostr?)r|rEr�r�rrrKZ localnamer��erer�r�raZurlfiler.rIrIrJr�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}nt
|�}t|�\}} t|�}|jd�}
|
dd�|
d}
}|
rb|
drb|
dd�}
|
r}|
dr}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)zUse FTP protocol.zCftp error: proxy support for ftp protocol currently not implementedrNzftp error: no host givenrjr�rNr�rPr�r�r�r�ruzftp:zContent-Type: %s
zContent-Length: %d
zftp error %rr�rOrO)r�r�r�rPrur�)#r�r�rr�rr
rrr
r�r�r�r�rZrr=r�r\r��MAXFTPCACHEr�r�r�rrpr�r�r�r�r�r� ftperrorsr�r�r�)r|rEr�rrrTr.rrr�r�r�rKr}r�r�r�r�r~r`r�r�rar�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�}nt|�}|jdt|��|jd�|j|�dj|�}tj|�}tj|�}t|||�S)zUse "data" URL.zEdata error: proxy support for data protocol currently not implementedr�rNz
data errorzbad data URLztext/plain;charset=US-ASCII�;rr�NrjzDate: %sz%a, %d %b %Y %H:%M:%S GMTzContent-type: %srrzlatin-1zContent-Length: %d�
)r�r�rr=r?ri�rfindrYraZstrftimeZgmtimerr�rrr
r\r�r�r�r��StringIOr) r|rErFr�Zsemir�r�ra�frIrIrJ� open_data�s6
"$
zURLopener.open_data)r�r�r�r�r�r�r�rr�r�r�r�rDr�r�r�r�r�r�r�r@r�r�r�r�rr rIrIrIrJr8@s.
$B\ :c@s�eZdZdZdd�Zdd�Zddd�Zd d
�Zddd�Zdd
d�Z ddd�Z
dddd�Zdddd�Zddd�Z
ddd�Zddd�Zddd�Zddd �Zd!d"�ZdS)#r9z?Derived class with handlers for errors we can handle (perhaps).cOs2tj|||�i|_d|_d|_dS)Nrr�)r8r�
auth_cache�tries�maxtries)r|r�r6rIrIrJrs zFancyURLopener.__init__cCst||d||�S)z3Default error handling -- don't raise an exception.zhttp:)r)r|rEr`r�r�rarIrIrJr�sz!FancyURLopener.http_error_defaultNc
Cs�|jd7_z{|jrg|j|jkrgt|d�rH|j}n |j}|||dd|�S|j||||||�}|SWdd|_XdS)z%Error 302 -- relocated (temporarily).rN�http_error_500i�z)Internal Server Error: Redirect RecursionNr)rrr�r
r��redirect_internal) r|rEr`r�r�rarFr�rcrIrIrJrs 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�rjz( Redirection to url '%s' is not allowed.)r�r�r�rj)r�rr�rrrrD) r|rEr`r�r�rarFr�rrIrIrJrs
z FancyURLopener.redirect_internalcCs|j||||||�S)z*Error 301 -- also relocated (permanently).)r)r|rEr`r�r�rarFrIrIrJr9szFancyURLopener.http_error_301cCs|j||||||�S)z;Error 303 -- also relocated (essentially identical to 302).)r)r|rEr`r�r�rarFrIrIrJr =szFancyURLopener.http_error_303cCsE|dkr(|j||||||�S|j|||||�SdS)z1Error 307 -- relocated, but turn POST into error.N)rr�)r|rEr`r�r�rarFrIrIrJr
AszFancyURLopener.http_error_307Fc
Csd|kr(tj||||||�|d}tjd|�} | sftj||||||�| j�\}
}|
j�dkr�tj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)z_Error 401 -- authentication required.
This function supports Basic authentication only.zwww-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"r:Zretry_�_basic_authN)r8r�rN�matchr@rpr�r�)
r|rEr`r�r�rarFrv�stuffrrr%rWrIrIrJrRHs&
zFancyURLopener.http_error_401c
Csd|kr(tj||||||�|d}tjd|�} | sftj||||||�| j�\}
}|
j�dkr�tj||||||�|s�tj||||||�d|jd}|dkr�t||�||�St||�|||�SdS)zeError 407 -- proxy authentication required.
This function supports Basic authentication only.zproxy-authenticatez![ ]*([^ ]+)[ ]+realm="([^"]*)"r:Zretry_proxy_rN)r8r�rNrr@rpr�r�)
r|rEr`r�r�rarFrvrrrr%rWrIrIrJrSas&
zFancyURLopener.http_error_407cCst|�\}}d||}|jd}t|�\}} t| �\} }
| jd�d}| |d�} |j| ||�\}}
|p�|
s�dSdt|dd�t|
dd�| f} d| |
|jd<|dkr�|j|�S|j||�SdS)Nzhttp://r��@rNz%s:%s@%sr�rj)rrrr��get_user_passwdr rD)r|rEr%rFrrr�r�rr�r��
proxyselectorr�rrrIrIrJ�retry_proxy_http_basic_authzs
z*FancyURLopener.retry_proxy_http_basic_authcCst|�\}}d||}|jd}t|�\}} t| �\} }
| jd�d}| |d�} |j| ||�\}}
|p�|
s�dSdt|dd�t|
dd�| f} d| |
|jd<|dkr�|j|�S|j||�SdS)Nzhttps://r�rrNz%s:%s@%sr�rj)rrrr�rr rD)r|rEr%rFrrr�r�rr�r�rr�rrrIrIrJ�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)NrrNz%s:%s@%sr�rjzhttp://)rr�rr rD)
r|rEr%rFrrr�r�rrr�rIrIrJrD�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)NrrNz%s:%s@%sr�rjzhttps://)rr�rr rD)
r|rEr%rFrrr�r�rrr�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)rpr
�prompt_user_passwd)r|rrr%r�r}rrrIrIrJr�s
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|rrr%rrrrIrIrJr�s
z!FancyURLopener.prompt_user_passwd)r�r�r�r�rr�rrrr r
rRrSrrrDrrrrIrIrIrJr9�s$cCstdkrtjd�atS)z8Return the IP address of the magic hostname 'localhost'.Nr�)�
_localhostr�r�rIrIrIrJr��sr�cCsdtdkr`y#ttjtj��d�aWn.tjk
r_ttjd�d�aYnXtS)z,Return the IP addresses of the current host.Nr�r�)� _thishostr$r�r�r�r�rIrIrIrJr��s#r�cCs%tdkr!ddl}|jatS)z1Return the set of errors raised by the FTP class.Nr)�
_ftperrorsr�r�)r�rIrIrJr�s rcCstdkrtjd�atS)z%Return an empty email Message object.Nrj)�
_noheadersr�r�rIrIrIrJ� noheaders�sr!c@speZdZdZdddd�Zdd�Zdd �Zd
d�Zdd
�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)
rrrrr.r�rG�refcount� keepalive�initr�)r|rrrrr.r�rGr�rIrIrJr�s
zftpwrapper.__init__cCs�ddl}d|_|j�|_|jj|j|j|j�|jj|j |j
�dj|j�}|jj
|�dS)Nrr�)r��busyZFTPr�Zconnectrrr.rGZloginrrr�r��cwd)r|r�Z_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)Nrrur�zTYPE ArNzTYPE zRETR r:Z550z
ftp error: %rr�zLIST ZLISTr�)rur�)r��endtransferr�Zvoidcmdr�r$ZntransfercmdZ
error_permr�rr�r�r��pwdr&r%r�makefile�
file_closer"r�)r|rKr�r��cmd�isdirr�r�r�r(ZftpobjrIrIrJr� sN
&*
zftpwrapper.retrfilecCs
d|_dS)Nr)r%)r|rIrIrJr'D szftpwrapper.endtransfercCs&d|_|jdkr"|j�dS)NFr)r#r"�
real_close)r|rIrIrJr�G s zftpwrapper.closecCs@|j�|jd8_|jdkr<|jr<|j�dS)NrNr)r'r"r#r-)r|rIrIrJr*L s
zftpwrapper.file_closecCs7|j�y|jj�Wnt�k
r2YnXdS)N)r'r�r�r)r|rIrIrJr-R s
zftpwrapper.real_close)r�r�r�r�rr$r�r'r�r*r-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)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����i����i����i����)rS�environrwrpr�)rrWr~rIrIrJ�getproxies_environmentZ s r1c
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)z�Test if proxies should not be used for a particular host.
Checks the proxy dict for the value of no_proxy, which should
be a list of comma separated DNS suffixes, or '*' for all hosts.
N�nor�*rNcSsg|]}|j��qSrI)rL)r�rrIrIrJr#� s z,proxy_bypass_environment.<locals>.<listcomp>r��.z
(.+\.)?%s$) r1ror
r=�lstriprNr�rrP)rrrZno_proxy�hostonlyr.Z
no_proxy_listrW�patternrIrIrJ�proxy_bypass_environmenty s&
r8c
Cszddlm}t|�\}}dd�}d|krH|drHdSd}x%|jd f�D]}|spqatjd
|�}|dk r_|dkr�ytj|�}||�}Wntk
r�waYnX||jd��} |jd�}
|
dkr$d
|jd�j d�d}
nt
|
dd��}
d|
}
||
?| |
?krrdSqa|||�radSqaWdS)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|�dkrV|ddddgdd�}|dd>|dd>B|dd>B|d BS)
Nr4r�r�rNr`r�rMr:)r=r�r�rZr\)ZipAddrr-rIrIrJ�ip2num� s
z,_proxy_bypass_macosx_sysconf.<locals>.ip2numr4Zexclude_simpleTN�
exceptionsz(\d+(?:\.\d+)*)(/\d+)?rNr�rM� F)r9r
r�rNrr�r�ri�group�countrZ)rr�proxy_settingsr9r6r.r;ZhostIPr~r�r1�maskrIrIrJ�_proxy_bypass_macosx_sysconf� s:
#
rB�darwin)�_get_proxy_settings�_get_proxiescCst�}t||�S)N)rDrB)rrr@rIrIrJ�proxy_bypass_macosx_sysconf� s rFcCst�S)z�Return a dictionary of scheme -> proxy server URL mappings.
This function uses the MacOSX framework SystemConfiguration
to fetch the proxy information.
)rErIrIrIrJ�getproxies_macosx_sysconf� srGcCs*t�}|rt||�St|�SdS)z�Return True, if host should be bypassed.
Checks proxy settings gathered from the environment, if specified,
or from the MacOSX framework SystemConfiguration.
N)r1r8rF)rrrrIrIrJr� s
rcCst�pt�S)N)r1rGrIrIrIrJr5� 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
ttfk
raYnX|S)zxReturn a dictionary of scheme -> proxy server URL mappings.
Win32 uses the registry to store proxies.
rNz;Software\Microsoft\Windows\CurrentVersion\Internet Settings�ProxyEnableZProxyServerr�rrNz^([^/:]+)://z%s://%srXzhttp:r�z http://%sz
https://%sr�zftp://%sr�)
�winreg�ImportError�OpenKey�HKEY_CURRENT_USER�QueryValueExr�r=rNrZCloserir?r�)rrI�internetSettings�proxyEnableZproxyServer�pr�ZaddressrIrIrJ�getproxies_registry� s8
rQcCst�pt�S)z�Return a dictionary of scheme -> proxy server URL mappings.
Returns settings gathered from the environment, if specified,
or the registry.
)r1rQrIrIrIrJr5(
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|jd�}x�|D]�} | dkrtd|krtdS| j
dd �} | j
d
d�} | j
dd�} x*|D]"}
tj| |
tj�r�dSq�WqRWdS)
Nrz;Software\Microsoft\Windows\CurrentVersion\Internet SettingsrHZ
ProxyOverriderz<local>r4rNz\.r3z.*�?)rIrJrKrLrMr�rir
r�r�rYZgetfqdnr=r�rNrrP)rrrIrNrOZ
proxyOverrideZrawHostr.ZaddrZfqdnr2r�rIrIrJ�proxy_bypass_registry1
sR
rScCs*t�}|rt||�St|�SdS)z�Return True, if host should be bypassed.
Checks proxy settings gathered from the environment, if specified,
or the registry.
N)r1r8rS)rrrrIrIrJrc
s
)r�rr�r�rcZhttp.clientr�r�rSr/rNr�rr�rar}rVrPrAZurllib.errorrrrZurllib.parserrrrr r
rrr
rrrrrrrrrZurllib.responserrrArJr@�__all__r�r�rCr�r0r1rXr6r7rO�ASCIIrnrsrrr2rr/rrrrr r!r"r#r$r%�urandomrbr&r'r(rwr)r�r�rBrYrr.rZr\r*r�r+r,r-rrWZ
nturl2pathr4r3r�r8r9rr�rr�rrr r!r�r1r8rB�platformZ_scproxyrDrErFrGrr5rQrSrIrIrIrJ�<module>Ds�v
!?n�$q*@
P �r
+3:5!���
_#<
- 2