def do_method(self, method): method_conf = self.config[method] matchlen = 0 match = None for path in method_conf: if self.is_path_prefix(path) and len(path) > matchlen: matchlen = len(path) match = path if matchlen > 0: self.send_error(method_conf[match]) elif "forward_to" in self.config: url = urljoin(self.config['forward_to'], self.path) self.log_request() self.log_message("Forwarding to {}".format(url)) o = URLopener().open(url) self.wfile.write(o.read()) o.close() elif "*" in method_conf: self.send_error(method_conf['*']) else: print(method.upper(), self.path, self.config['port']) self.log_message( "No match for %s %s on port %d and no default configured" % (method.upper(), self.path, self.config['port'])) self.send_error(404)
def utGrabFromUrl(p_url): """ Takes a file from a remote server """ from urllib import URLopener try: l_opener = URLopener() l_file = l_opener.open(p_url) ctype = l_file.headers['Content-Type'] l_opener.close() return (l_file.read(), ctype) except: return (None, 'text/x-unknown-content-type')
def try_download(_path, _file, _url, _stale,): now = time() url = URLopener() file_exists = isfile(_path+_file) == True if file_exists: file_old = (getmtime(_path+_file) + _stale) < now if not file_exists or (file_exists and file_old): try: url.retrieve(_url, _path+_file) result = 'ID ALIAS MAPPER: \'{}\' successfully downloaded'.format(_file) except IOError: result = 'ID ALIAS MAPPER: \'{}\' could not be downloaded'.format(_file) else: result = 'ID ALIAS MAPPER: \'{}\' is current, not downloaded'.format(_file) url.close() return result
def __init__(self): global dbaselocal global datapath fname = datapath + 'TRMM_classmap.dat' print 'Loading class map ',fname if dbaselocal: landclassmap.data = np.loadtxt(fname, dtype='int')[:,1] else: f = URLopener().open(fname) tmp = [] for line in f: columns = line.split() tmp.append(int(columns[1])) f.close() landclassmap.data = np.array(tmp) landclassmap.data = np.reshape(landclassmap.data, (-1, 360)) print 'Class map loaded'
def install_firmware(self, new_version): logging.info('Update firmware request') logging.info('Current firmware version: {}'.format( self.firmware_version)) logging.info('Firmware version to install: {}'.format(new_version)) fw_fname_prefix = 'sensa-%s' % new_version fw_check_url = '%sstatic/firmware/%s.chk' % ( self.api_url, fw_fname_prefix) fw_filename = fw_fname_prefix + '.zip' fw_url = '%sstatic/firmware/%s' % (self.api_url, fw_filename) # Firmware install shell script deploy_script = 'deploy.sh' # Download firmware fw_file = URLopener() try: fw_file.retrieve(fw_url, fw_filename) except IOError: logging.error('Error during firmware download') return 1 fw_file.close() # Check downloaded firmware integrity try: fw_checksum_req = requests.get(fw_check_url) except requests.exceptions.RequestException: logging.error('Error during firmware download') return 1 expected_check = fw_checksum_req.text.split() fw_checksum = md5(open(fw_filename, 'rb').read()).hexdigest() if(fw_checksum != expected_check[0] and fw_filename != expected_check[1]): logging.error('Error checking firmware integrity') return logging.info('Files checked. Updating') # Unzip try: fw_file = ZipFile(fw_filename, 'r') except IOError: logging.error('Error reading local firmware file') return fw_file.extractall() fw_file.close() # Run firmware script call(['sh', deploy_script]) # Remove firmware file call(['rm', fw_filename]) # Remove firmware script call(['rm', deploy_script]) config = SafeConfigParser() config.read(self.config_file) # Update firmware version on config file config.set('device', 'firmware_version', new_version) try: conf_file = open(self.config, 'wb') try: parser.write(conf_file) finally: conf_file.close() except IOError: logging.error('Error updating version on config file') '''
f = URLopener().open(fname) # print 'Loading data from ',fname for line in f: columns = line.split() ic = int(columns[0]) ikey = tuple([int(c) for c in columns[1:3+idim]]) rr = float(columns[-17]) fracs = [float(x) for x in columns[-16:]] dictentry = {'NCOUNT': ic, 'RR' : rr, 'FR' : fracs} dbase_dict[cls][dbd].update({ikey : dictentry}) f.close() print 'Done loading databases.' print class landclassmap: def __init__(self): global dbaselocal global datapath fname = datapath + 'TRMM_classmap.dat' print 'Loading class map ',fname if dbaselocal: landclassmap.data = np.loadtxt(fname, dtype='int')[:,1]
def get_facilities(file): o = URLopener().open(file) t = o.readlines() o.close() return [i.split(' ')[0].lower() for i in t]