def getRetriever(scheme): """ Get the right retriever function depending on the scheme. If scheme is 'http' return urllib.urlretrieve, else if the scheme is https create a URLOpener with certificates taken from the X509_USER_PROXY variable. If certificates are not available return urllib.urlretrieve as for the http case. """ if 'X509_USER_PROXY' in os.environ and os.path.isfile( os.environ['X509_USER_PROXY']): certfile = os.environ['X509_USER_PROXY'] else: if scheme == 'https': print( "User proxy not found. Trying to retrieve the file without using certificates" ) certfile = None if scheme == 'http' or not certfile: retriever = urllib.urlretrieve else: print("Using %s as X509 certificate" % certfile) op = URLopener(None, key_file=certfile, cert_file=certfile) op.addheader('Accept', 'application/octet-stream') retriever = op.retrieve return retriever
def command(self): args = list(self.args) method, url = args[0:2] if not url.startswith('http'): url = 'http://%s:%s%s' % (self.session.config.sys.http_host, self.session.config.sys.http_port, ('/' + url).replace('//', '/')) # FIXME: The python URLopener doesn't seem to support other verbs, # which is really quite lame. method = method.upper() assert (method in ('GET', 'POST')) qv, pv = [], [] if method == 'POST': which = pv else: which = qv for arg in args[2:]: if '=' in arg: which.append(tuple(arg.split('=', 1))) elif arg.upper()[0] == 'P': which = pv elif arg.upper()[0] == 'Q': which = qv if qv: qv = urlencode(qv) url += ('?' in url and '&' or '?') + qv # Log us in automagically! httpd = self.session.config.http_worker.httpd global HACKS_SESSION_ID if HACKS_SESSION_ID is None: HACKS_SESSION_ID = httpd.make_session_id(None) mailpile.auth.SetLoggedIn(None, user='******', session_id=HACKS_SESSION_ID) cookie = httpd.session_cookie try: uo = URLopener() uo.addheader('Cookie', '%s=%s' % (cookie, HACKS_SESSION_ID)) if method == 'POST': (fn, hdrs) = uo.retrieve(url, data=urlencode(pv)) else: (fn, hdrs) = uo.retrieve(url) hdrs = unicode(hdrs) data = open(fn, 'rb').read().strip() if data.startswith('{') and 'application/json' in hdrs: data = json.loads(data) return self._success('%s %s' % (method, url), result={ 'headers': hdrs.splitlines(), 'data': data }) except: self._ignore_exception() return self._error('%s %s' % (method, url))
def command(self): args = list(self.args) method, url = args[0:2] if not url.startswith('http'): url = 'http://%s:%s%s' % (self.session.config.sys.http_host, self.session.config.sys.http_port, ('/' + url).replace('//', '/')) # FIXME: The python URLopener doesn't seem to support other verbs, # which is really quite lame. method = method.upper() assert(method in ('GET', 'POST')) qv, pv = [], [] if method == 'POST': which = pv else: which = qv for arg in args[2:]: if '=' in arg: which.append(tuple(arg.split('=', 1))) elif arg.upper()[0] == 'P': which = pv elif arg.upper()[0] == 'Q': which = qv if qv: qv = urlencode(qv) url += ('?' in url and '&' or '?') + qv # Log us in automagically! httpd = self.session.config.http_worker.httpd global HACKS_SESSION_ID if HACKS_SESSION_ID is None: HACKS_SESSION_ID = httpd.make_session_id(None) mailpile.auth.SetLoggedIn(None, user='******', session_id=HACKS_SESSION_ID) cookie = httpd.session_cookie try: uo = URLopener() uo.addheader('Cookie', '%s=%s' % (cookie, HACKS_SESSION_ID)) with TcpConnBroker().context(need=[TcpConnBroker.OUTGOING_HTTP]): if method == 'POST': (fn, hdrs) = uo.retrieve(url, data=urlencode(pv)) else: (fn, hdrs) = uo.retrieve(url) hdrs = unicode(hdrs) data = open(fn, 'rb').read().strip() if data.startswith('{') and 'application/json' in hdrs: data = json.loads(data) return self._success('%s %s' % (method, url), result={ 'headers': hdrs.splitlines(), 'data': data }) except: self._ignore_exception() return self._error('%s %s' % (method, url))
def command(self): args = list(self.args) method, url = args[0:2] if not url.startswith("http"): url = "http://%s:%s%s" % ( self.session.config.sys.http_host, self.session.config.sys.http_port, ("/" + url).replace("//", "/"), ) # FIXME: The python URLopener doesn't seem to support other verbs, # which is really quite lame. method = method.upper() assert method in ("GET", "POST") qv, pv = [], [] if method == "POST": which = pv else: which = qv for arg in args[2:]: if "=" in arg: which.append(tuple(arg.split("=", 1))) elif arg.upper()[0] == "P": which = pv elif arg.upper()[0] == "Q": which = qv if qv: qv = urlencode(qv) url += ("?" in url and "&" or "?") + qv # Log us in automagically! httpd = self.session.config.http_worker.httpd global HACKS_SESSION_ID if HACKS_SESSION_ID is None: HACKS_SESSION_ID = httpd.make_session_id(None) mailpile.auth.SetLoggedIn(None, user="******", session_id=HACKS_SESSION_ID) cookie = httpd.session_cookie try: uo = URLopener() uo.addheader("Cookie", "%s=%s" % (cookie, HACKS_SESSION_ID)) with TcpConnBroker().context(need=[TcpConnBroker.OUTGOING_HTTP], oneshot=True): if method == "POST": (fn, hdrs) = uo.retrieve(url, data=urlencode(pv)) else: (fn, hdrs) = uo.retrieve(url) hdrs = unicode(hdrs) data = open(fn, "rb").read().strip() if data.startswith("{") and "application/json" in hdrs: data = json.loads(data) return self._success("%s %s" % (method, url), result={"headers": hdrs.splitlines(), "data": data}) except: self._ignore_exception() return self._error("%s %s" % (method, url))
def getRetriever(scheme): """ Get the right retriever function depending on the scheme. If scheme is 'http' return urllib.urlretrieve, else if the scheme is https create a URLOpener with certificates taken from the X509_USER_PROXY variable. If certificates are not available return urllib.urlretrieve as for the http case. """ if os.environ.has_key('X509_USER_PROXY') and os.path.isfile(os.environ['X509_USER_PROXY']): certfile = os.environ['X509_USER_PROXY'] else: if scheme == 'https': print "User proxy not found. Trying to retrieve the file without using certificates" certfile = None if scheme == 'http' or not certfile: retriever = urllib.urlretrieve else: print "Using %s as X509 certificate" % certfile op = URLopener(None, key_file=certfile, cert_file=certfile) op.addheader( 'Accept', 'application/octet-stream' ) retriever = op.retrieve return retriever