• File: utils.pyc
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyc
  • Date Modified: 10/31/2014 10:04 AM
  • File size: 11.98 KB
  • MIME-type: text/x-bytecode.python
  • Charset: 8 bit

P:}Pc@sdZdZdZdZdZdZdZddlZddlZddl	Z	dd	l
mZddlZddl
Z
dd
l
mZmZmZmZddlZddlmZddlmZdd
lmZe	jeZdefdYZdefdYZdddZ dddZ!dddZ"dddZ#ddZ$dZ%de&fdYZ'dZ(edkre	j)e(ndS(s`Utilities using NDG HTTPS Client, including a main module that can be used to
fetch from a URL.
s
R B Wilkinsons09/12/11s2(C) 2011 Science and Technology Facilities Councils-BSD - see LICENSE file in top-level directorysPhilip.Kershaw@stfc.ac.uks$Id$iN(tOptionParser(tHTTPHandlertHTTPCookieProcessortHTTPBasicAuthHandlertHTTPPasswordMgrWithDefaultRealm(tbuild_opener(tHTTPSContextHandler(tssl_context_utiltAccumulatingHTTPCookieProcessorcBseZdZdZeZRS(stCookie processor that adds new cookies (instead of replacing the existing
    ones as HTTPCookieProcessor does)
    cCsd}tj|j|ji|j|j}|jj||j|}|r|j	|r|j|}dj
||g}|j||q|j||n|S(sProcesses cookies for a HTTP request.
        @param request: request to process
        @type request: urllib2.Request
        @return: request
        @rtype: urllib2.Request
        tCookies; (turllib2tRequesttget_full_urltdatatorigin_req_hosttunverifiablet	cookiejartadd_cookie_headert
get_headert
has_headertjointadd_unredirected_header(tselftrequesttCOOKIE_HEADER_NAMEttmp_requesttnew_cookiestold_cookiestmerged_cookies((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pythttp_request s	
(t__name__t
__module__t__doc__Rt
https_request(((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRs	t
URLFetchErrorcBseZdZRS(sError fetching content from URL(RRR (((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyR">scCsct||d|d|\}}}|rS|tjkrS|j}|j|St|dS(sReturns data retrieved from a URL.
    @param url: URL to attempt to open
    @type url: basestring
    @param config: SSL context configuration
    @type config: Configuration
    @return data retrieved from URL or None
    R
thandlersN(topen_urlthttplibtOKtreadtcloseR"(turltconfigR
R#treturn_codetreturn_messagetresponsetreturn_data((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytfetch_from_urlBs
c
Cst||d|d|\}}}|tjkrr|j}|jt|d}	|	j||	jn|||tjkfS(sWrites data retrieved from a URL to a file.
    @param url: URL to attempt to open
    @type url: basestring
    @param config: SSL context configuration
    @type config: Configuration
    @param output_file: output file
    @type output_file: basestring
    @return: tuple (
        returned HTTP status code or 0 if an error occurred
        returned message
        boolean indicating whether access was successful)
    R
R#tw(R$R%R&R'R(topentwrite(
R)R*toutput_fileR
R#R+R,R-R.toutfile((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytfetch_from_url_to_fileSs



cCsMt||d|d|\}}}|r=|tjkr=|St|dS(sReturns data retrieved from a URL.
    @param url: URL to attempt to open
    @type url: basestring
    @param config: SSL context configuration
    @type config: Configuration
    @return: data retrieved from URL or None
    @rtype: file derived type
    R
R#N(R$R%R&R"(R)R*R
R#R+R,R-((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytfetch_stream_from_urljs
	c
Cs|jrdnd}|jr*|j}ntj}t|}|sQg}n|j||jrtd|}t|jd|}|j	||gn|j
rtt}	|	j
ddd|d|jdd|jd|j|	nt||js1|jtjitjdn9|jrj|jtj|jtjd	|jntd
|j|}
|j}|dkri}ntj|||}d}
d}d}yj|
j|}|j}|j}
tjtjr-x0t|D]\}}tjd||qWnWntj k
r}|j}
d
|j}tjtjrtjd|j|jqnWt!k
r}d
|j"}tjtjrddl#}tj|j$qnX|
||fS(sAttempts to open a connection to a specified URL.
    @param url: URL to attempt to open
    @param config: SSL context configuration
    @type config: Configuration
    @param data: HTTP POST data
    @type data: str
    @param handlers: list of custom urllib2 handlers to add to the request
    @type handlers: iterable
    @return: tuple (
        returned HTTP status code or 0 if an error occurred
        returned message or error description
        response object)
    iit
debugleveltrealmturitusertpasswdsNot using proxysConfiguring proxies: %stssl_contextts	%s  :  %ss	Error: %ss%s %siN(%tdebugtcookiet	cookielibt	CookieJarRtappendRRR<textendthttp_basicauthRRtadd_passwordtNonethttpautht_should_use_proxytno_proxyR
tProxyHandlertlogtproxiesRtheadersRR1tmsgtcodetisEnabledFortloggingtDEBUGt	enumeratet	HTTPErrort	Exceptiont__str__t	tracebackt
format_exc(R)R*R
R#R7tcjtcookie_handlerthttp_handlert
https_handlertauth_handlertopenerRMRR+R,R-tindexR?texcRW((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyR${sf		
			
						
cCs|dkr$tjjdd}n|}tjt|}xCg|jdD]}|j^qRD]}|j|krht	SqhWt
S(sDetermines whether a proxy should be used to open a connection to the 
    specified URL, based on the value of the no_proxy environment variable.
    @param url: URL
    @type url: basestring or urllib2.Request
    RIR=t,N(RFtostenvirontgetturlparset_url_as_stringtsplittstripthostnametFalsetTrue(R)RItno_proxy_effectiveturlObjthtnp((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRHs/cCsLt|tjr|jSt|tr/|StdttjfdS(sReturns the URL string from a URL value that is either a string or
    urllib2.Request..
    @param url: URL
    @type url: basestring or urllib2.Request
    @return: URL string
    @rtype: basestring
    sExpected type %r or %rN(t
isinstanceR
RRt
basestringt	TypeError(R)((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRfs
t
ConfigurationcBs)eZdZeddddddZRS(sConnection configuration.
    cCsC||_||_||_||_||_||_||_dS(s
        @param ssl_context: SSL context to use with this configuration
        @type ssl_context: OpenSSL.SSL.Context
        @param debug: if True, output debugging information
        @type debug: bool
        @param proxies: proxies to use for 
        @type proxies: dict with basestring keys and values
        @param no_proxy: hosts for which a proxy should not be used
        @type no_proxy: basestring
        @param cookie: cookies to set for request
        @type cookie: cookielib.CookieJar
        @param http_basicauth: http authentication, or None
        @type http_basicauth: tuple of (username,password)
        @param headers: http headers
        @type headers: dict
        N(R<R>RLRIR?RDRM(RR<R>RLRIR?RDRM((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyt__init__s						N(RRR RjRFRt(((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyRss	cCstdd}|jddddddd	tjjd
dd|jd
dddddd	d8dd|jddddddd	d8dd|jddddddd	tdd|jddddddd	d8dd|jd d!dd"ddd	d8dd#|jd$d%dddd&d	tdd'|jd(d)dd*dd+d	d8dd,|jd-dd.dd/dd0dd1|j\}}t|d2kr|j	d3n|d4}|j
rtjj
tjn|jr
tjj|jr
|j}nd8}|jr:tjj|jr:|j}nd8}|jrjtjj|jrj|j}nd8}|j}|jrtjj|jrt|j}|j}	|jnd8}	|jr|jjd5d2}
nd8}
i}|jrAxB|jD]4}|jd5d2\}
}|j||
j<qWntj||d8|||}t ||j
d6|
d/|}|j!rt"|||j!|	d7 \}}t#||nt$||}	|	GHd8S(9sHUtility to fetch data using HTTP or HTTPS GET from a specified URL.
    tusages%prog [options] urls-cs
--certificatetdestt	cert_filetmetavartFILEtdefaults~/credentials.pemthelps4Certificate file - defaults to $HOME/credentials.pems-ks
--private-keytkey_files3Private key file - defaults to the certificate files-ts--ca-certificate-dirtca_dirtPATHs%Trusted CA certificate file directorys-ds--debugtactiont
store_trueR>sPrint debug information.s-ps--post-data-filet	data_filesPOST data files-fs--fetchR3sOutput files-ns--no-verify-peertno_verify_peers&Skip verification of peer certificate.s-as--basicauthtauthsUSER:PASSWDsHTTP authentication credentialss--headerRBRMs
HEADER: VALUEsAdd HTTP header to requestisIncorrect number of argumentsit:RDiN(%Rt
add_optionRbtpatht
expanduserRFRjt
parse_argstlenterrorR>RQt	getLoggertsetLevelRRR|texistsRwR}RRR1R'R(t	basicauthRRgRMtlstripRhRtmake_ssl_contextRsR3R5t
SystemExitR/(tparsertoptionstargsR)R|RwR}tverify_peerRR
RDRMRntkeytvalR<R*R+R,((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pytmains

	

							t__main__(*R t
__author__t__date__t
__copyright__t__license__t__contact__t__revision__R@R%RQtoptparseRRbR
RRRRRet$ndg.httpsclient.urllib2_build_openerRtndg.httpsclient.httpsRtndg.httpsclientRRRRKRRUR"RFR/R5R6R$RHRftobjectRsRtbasicConfig(((s9/usr/lib/python2.7/site-packages/ndg/httpsclient/utils.pyt<module>s>""Z		d