def httpsMultipartUpload(url, filename, _cert_file, _key_file): # HTTPS + MULTIPART example # This is customized for mod_gridsite uploads... gridsite_cgi = "gridsite-admin.cgi" log.debug( 'webutils.httpsMultipartUpload(): \n URL: %s\n filename: %s' % (url, filename)) from mariachi import MultipartPostHandler log.debug( 'webutils.httpsMultipartUpload(): Setting x509. cert=%s, key=%s' % (_cert_file, _key_file)) urllib2.HTTPSHandler.certfile = _cert_file urllib2.HTTPSHandler.keyfile = _key_file urllib2.HTTPSHandler.strict = None params = {'filename': filename, 'uploadfile': open(filename, 'rb')} log.debug('webutils.httpsMultipartUpload(): params: %s' % params) opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) urllib2.install_opener(opener) url = url + gridsite_cgi log.debug('webutils.httpsMultipartUpload(): CGI upload URL: %s' % url) response = opener.open(url, params) #req = urllib2.Request(url, params) #response = urllib2.urlopen(req).read().strip() return response
def urllib2x509httpsGet(url, _cert_file, _key_file): urllib2.HTTPSHandler.certfile = _cert_file urllib2.HTTPSHandler.keyfile = _key_file urllib2.HTTPSHandler.strict = None opener = urllib2.build_opener() urllib2.install_opener(opener) f = urllib2.urlopen(url) return f.read()
def _uploadFile(self, filename): """ Uploads a single file from sourcedir to destdir as specified for this uploader. """ self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): Setting x509...') urllib2.HTTPSHandler.certfile = self.certfile urllib2.HTTPSHandler.keyfile = self.keyfile urllib2.HTTPSHandler.strict = self.strict # build localfilename to open fullpath = "%s/%s" % (self.local_base, filename) self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): Uploading %s' % fullpath) # filename e.g. "/subdir/newsubdir" self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): Uploading %s' % filename) (parents, newfile) = os.path.split(filename) self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): parents = %s tail=%s' % (parents, newfile)) # build CGI URL url = "https://%s%s" % (self.upload_host, self.base_path) self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): URL step 1: %s' % url) if parents and len(parents) > 1: url = "%s%s/" % (url, parents) else: url = "%s/" % url self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): URL step 2: %s' % url) url = "%s%s" % (url, self.cgi_exec) self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): URL step 3: %s' % url) self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): CGI upload URL: %s' % url) opener = urllib2.build_opener(MultipartPostHandler) urllib2.install_opener(opener) params = {'filename': filename, 'uploadfile': open(fullpath, 'rb')} self.log.debug( 'vihuela.daq.gridsiteUploader._uploadFile(): params: %s' % params) response = opener.open(url, params) return response
def _mkDir(self, newname): """ Creates a new directory of newname. """ self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): Setting x509.') urllib2.HTTPSHandler.certfile = self.certfile urllib2.HTTPSHandler.keyfile = self.keyfile urllib2.HTTPSHandler.strict = self.strict # newname e.g. "/subdir/newsubdir" self.log.debug('vihuela.plugins.GridsitePlugin._mkDir(): Creating %s' % newname) (parents, newdir) = os.path.split(newname) self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): parents = %s tail=%s' % (parents, newdir)) # build CGI URL url = "https://%s%s" % (self.upload_host, self.base_path) self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): URL step 1: %s' % url) if parents and len(parents) > 1: url = "%s%s/" % (url, parents) else: url = "%s/" % url self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): URL step 2: %s' % url) url = "%s%s" % (url, self.cgi_exec) self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): URL step 3: %s' % url) # debug info self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): CGI upload URL: %s' % url) params = {'cmd': 'edit', 'button': 'Create', 'file': newdir} self.log.debug( 'vihuela.plugins.GridsitePlugin._mkDir(): params: %s' % params) #execute directory creation opener = urllib2.build_opener(MultipartPostHandler) urllib2.install_opener(opener) response = opener.open(url, params) return response
def httpProxyGet(url, _http_proxy): # HTTP + PROXY example log.debug('webutils.httpProxyGet(): Begin fetching URL: %s' % url) urllib2.HTTPSHandler.certfile = "/home/jhover/.globus/usercert.pem" urllib2.HTTPSHandler.keyfile = "/home/jhover/.globus/userkeynopw.pem" urllib2.HTTPSHandler.strict = None proxyurl = 'http://%s' % _http_proxy log.debug('webutils.httpProxyGet(): Adding proxy: %s' % proxyurl) proxy_support = urllib2.ProxyHandler({"http": proxyurl}) log.debug('webutils.httpProxyGet(): making opener') opener = urllib2.build_opener(proxy_support) log.debug('webutils.httpProxyGet(): installing opener...') urllib2.install_opener(opener) log.debug('webutils.httpProxyGet(): making Request...') req = urllib2.Request(url) response = urllib2.urlopen(req).read().strip() return response
def main(): import tempfile, sys validatorURL = "http://validator.w3.org/check" opener = urllib2.build_opener(MultipartPostHandler) def validateFile(url): temp = tempfile.mkstemp(suffix=".html") os.write(temp[0], opener.open(url).read()) params = { "ss": "0", # show source "doctype": "Inline", "uploaded_file": open(temp[1], "rb") } print opener.open(validatorURL, params).read() os.remove(temp[1]) if len(sys.argv[1:]) > 0: for arg in sys.argv[1:]: validateFile(arg) else: validateFile("http://www.google.com")
def httpsProxyGet(url, _cert_file, _key_file, _http_proxy): # HTTPS + PROXY example #import urllib2 log.debug('webutils.httpsProxyGet(): Begin fetching URL: %s' % url) from mariachi.ProxyHTTPConnection import ConnectHTTPSHandler, ConnectHTTPHandler urllib2.HTTPSHandler.certfile = _cert_file urllib2.HTTPSHandler.keyfile = _key_file urllib2.HTTPSHandler.strict = None proxyurl = 'http://%s' % _http_proxy log.debug('webutils.httpsProxyGet(): Adding proxy: %s' % proxyurl) proxy_support = urllib2.ProxyHandler({"http": proxyurl}) log.debug('webutils.httpsProxyGet(): making opener') opener = urllib2.build_opener(proxy_support, ConnectHTTPHandler, ConnectHTTPSHandler) log.debug('webutils.httpsProxyGet(): installing opener...') urllib2.install_opener(opener) log.debug('webutils.httpsProxyGet(): making Request...') req = urllib2.Request(url) log.debug('webutils.httpsProxyGet(): calling urlopen(Request)') response = urllib2.urlopen(req).read().strip() return response
def do_open(self, http_class, req): log.debug('ConnectHTTPSHandler.do_open():Called... ') return urllib2.HTTPSHandler.do_open(self, ProxyHTTPSConnection, req) if __name__ == '__main__': import sys, logging # Set up logging. logging.basicConfig() log.setLevel(logging.DEBUG) log.debug("Testing ConnectHTTPHandler") test_url = '<URL:https://www-mariachi.physics.sunysb.edu/gridsite/>' cert_file = '/home/jhover/.globus/usercert.pem' key_file = '/home/jhover/.globus/userkeynopw.pem' log.debug('Setting x509. cert=%s, key=%s' % (cert_file, key_file)) urllib2.HTTPSHandler.certfile = cert_file urllib2.HTTPSHandler.keyfile = key_file urllib2.HTTPSHandler.strict = None log.debug('Building Opener...') opener = urllib2.build_opener(ConnectHTTPHandler, ConnectHTTPSHandler) urllib2.install_opener(opener) req = urllib2.Request(url=test_url) req.set_proxy('192.168.1.130:3128', 'https') log.debug("Opening url %s" % test_url) f = urllib2.urlopen(req) print f.read()