def bajarDatos(username, libro, tamfragmento, fragmento, centinela): if (not username in activas): activas[username] = [] activas[username].append(libro) print("Enviando libro...") ruta = "Repositorio/" + libro + ".pdf" file = open(ruta, "rb") file.read(tamfragmento * (fragmento - 1)) if (centinela): aux = file.read() else: aux = file.read(tamfragmento) file.close() return Binary(aux)
def r1getd(self, key): # Default return value db = shelve.open(f) self.r1data = db['r1data'] # If the key is in the data structure, return properly formated results key = key.data print(key) if key in self.r1data: print("True") else: print("false") db.close() return Binary(pickle.dumps(self.r1data[key]))
def writeFile(self, path, filename, data, confirm=True): """ Write data in provided filepath """ filepath = os.path.join(path, filename) serialized_data = Binary(str(data)) try: self.conn.putfo(StringIO(str(serialized_data)), filepath, confirm=confirm) except error, msg: raise SFTPError( str(msg) + ' while writing file %s on %s' % (filepath, path, self.url))
def repair(self, path): #Used to correct the data of a particular path. path = path.data for i in other_servers: #print other_servers try: a = xmlrpclib.ServerProxy("http://localhost:" + i) r = pickle.loads(a.get(Binary(path))['value'].data) # old_value = self.data[path][0] old_ttl = self.data[path][1] self.data[path] = (r, old_ttl) print "Corrected Data." break except Exception, E: print E
def get(self, key): # Default return value s = shelve.open(f) self.data = s['data'] rv = list # If the key is in the data structure, return properly formated results key = key.data print(key) if key in self.data: print("True") else: print("false") s.close() return Binary(pickle.dumps(self.data[key]))
def _dbeToXmlrpc(self, dbe): ret = {} for n in dbe.getNames(): tmpvalue = dbe.getValue(n) if isinstance(tmpvalue, str) or isinstance(tmpvalue, unicode): try: tmpvalue.decode('ascii') except Exception: tmpvalue = Binary(tmpvalue) if isinstance(tmpvalue, datetime.datetime): ret[n] = "%s" % tmpvalue else: ret[n] = tmpvalue return [dbe.getTypeName(), ret]
def r2put(self, key, value): # Remove expired entries s = shelve.open(f) value = pickle.loads(value.data) self.r2data[key.data] = value self.r2data1[key.data] = value s['r2data'] = self.r2data s['r2data1'] = self.r2data1 s.close() print( "......===============================================.............................................................", self.r2data1) return Binary(pickle.dumps(True))
def put(self,argv): datagram = pickle.loads(argv.data) port = datagram['port'] path = datagram['path'] ID = datagram['blks'] version = datagram['version'] value = datagram['data'] length = str(len(value)) if path not in self.data[port]: # initialize a dict for a new file self.data[port][path] = {} if ID not in self.data[port][path]: self.data[port][path][ID] = {} # initialize a dict for a new block self.data[port][path][ID][version] = value + self.checksum(value) return Binary(length)
def get(self,argv): rv = False datagram = pickle.loads(argv.data) port = datagram['port'] path = datagram['path'] ID = datagram['blks'] version = datagram['version'] #check whether there is the specified block in the specified path on the specified server. Also, #need to make sure it has the version. if port in self.data and path in self.data[port] and \ ID in self.data[port][path] and \ version in self.data[port][path][ID]: rv = Binary(self.data[port][path][ID][version]) return rv
def test_newMediaObject(self): # Create a new Image using the RPC method. path = os.path.join(os.path.dirname(__file__), 'static', 'xmlrpc_wp', 'TODO.gif') bits = Binary(open(path).read()) overrides = { 'data': { 'name': path, 'type': 'image/gif', 'bits': bits, } } result = self.get_result(MetaWeblog.newMediaObject, **overrides) self.assertTrue(result)
def test_round_trip(): """ Test `ipalib.rpc.xml_wrap` and `ipalib.rpc.xml_unwrap`. This tests the two functions together with ``xmlrpclib.dumps()`` and ``xmlrpclib.loads()`` in a full wrap/dumps/loads/unwrap round trip. """ # We first test that our assumptions about xmlrpclib module in the Python # standard library are correct: assert_equal(dump_n_load(utf8_bytes), unicode_str) assert_equal(dump_n_load(unicode_str), unicode_str) assert_equal(dump_n_load(Binary(binary_bytes)).data, binary_bytes) assert isinstance(dump_n_load(Binary(binary_bytes)), Binary) assert type(dump_n_load('hello')) is str assert type(dump_n_load(u'hello')) is str assert_equal(dump_n_load(''), '') assert_equal(dump_n_load(u''), '') assert dump_n_load(None) is None # Now we test our wrap and unwrap methods in combination with dumps, loads: # All str should come back str (because they get wrapped in # xmlrpclib.Binary(). All unicode should come back unicode because str # explicity get decoded by rpc.xml_unwrap() if they weren't already # decoded by xmlrpclib.loads(). assert_equal(round_trip(utf8_bytes), utf8_bytes) assert_equal(round_trip(unicode_str), unicode_str) assert_equal(round_trip(binary_bytes), binary_bytes) assert type(round_trip('hello')) is str assert type(round_trip(u'hello')) is unicode assert_equal(round_trip(''), '') assert_equal(round_trip(u''), u'') assert round_trip(None) is None compound = [ utf8_bytes, None, binary_bytes, (None, unicode_str), dict(utf8=utf8_bytes, chars=unicode_str, data=binary_bytes) ] assert round_trip(compound) == tuple(compound)
def __init__(self, portlist): self.data = {} self.next_check = datetime.now() + timedelta(minutes=5) print "WELCOME" portlist = portlist[1:] url = "http://127.0.0.1:" server_list = [] for each in portlist: server_list.append(xmlrpclib.Server(url + each)) keylist = [] print "server_list", server_list available_servers = [] for server in server_list: try: res = server.get(Binary("keys")) keylist = res available_servers.append(server) except: print "iterate to next server" print "LIST OF KEYS", keylist for key in keylist: available_values = [] print "key is: ", key for server in available_servers: res = server.get(Binary(key)) value = pickle.loads(res["value"].data) available_values.append(value) for each in available_values: count = available_values.count(each) if count >= 2: value = each break print self.put(Binary(key), Binary(pickle.dumps(value)), 6000)
def build_data(self, serv_id): #print('Flag is 1') x, y = self.find_adj_serv(serv_id) #print('here server x=',x) #print('and server y=',y) new_store = {} ret_obj = self.d_server[x].request_data(1, 0) stored = pickle.loads(ret_obj.data) new_store.update(stored) ret_obj = self.d_server[y].request_data(0, 1) stored = pickle.loads(ret_obj.data) new_store.update(stored) self.d_server[serv_id].load_serv(Binary(pickle.dumps(new_store)))
def restore_meta(self): dict_t = {} print 'restore func!!!' # node to be deleted node = (self.path.rsplit('/', 1)[1]) # parent node print self.path parent_node = ('/' + self.path.rsplit('/', 1)[0]) + '&&' + 'list_nodes' print parent_node, node # remove node from parents list rpc = xmlrpclib.Server(url + ':' + meta_port) res = rpc.get(Binary(parent_node)) print res if "value" in res: list_nodes = pickle.loads(res["value"].data) print 'before!!', list_nodes else: print 'None in list_nodes' return None del list_nodes[node] print list_nodes rpc.put(Binary(parent_node), Binary(pickle.dumps(list_nodes)), 6000) return
def GetUpdateApp(self, version): global _apk_size, _apk_time, _apk_version try: apk_state = lstat(APK_FILENAME) if apk_state.st_ctime <> _apk_time or apk_state.st_size <> _apk_size: _apk_time = apk_state.st_ctime _apk_size = apk_state.st_size _apk_version = GetApkVersion() if DEBUG_LEVEL > 0: Log("GetUpdateApp:\t Client version=%d, Current version: %d" % (version, _apk_version)) if version < _apk_version: if DEBUG_LEVEL > 1: Log("GetUpdateApp:\tSend ubdate APK v: %d" % _apk_version) with open(APK_FILENAME, "rb") as handle: return Binary(handle.read()) else: if DEBUG_LEVEL > 1: Log("GetUpdateApp:\tUpdate does not require") return Binary('0') except Exception as e: Log(e, True) raise
def corrupt(self, key): shelve_datastore = shelve.open(datastore) if key in shelve_datastore.keys(): self.data[key.data] = Binary( pickle.dumps("abc12345\nabcdefghijklmnop1234567891234567")) print("corrupted path :") print(key[2:]) print("Corrupted block no: ") print(key[1]) shelve_datastore.update(self.data) shelve_datastore.close() return True else: print("Path not found on this dataserver") return False
def copy_file_to_machine(self, local_path, remote_path=None, netns=None): remote_path = self._rpc_call_x(netns, "start_copy_to", remote_path) f = open(local_path, "rb") while True: data = f.read(1024 * 1024) # 1MB buffer if len(data) == 0: break self._rpc_call_x(netns, "copy_part_to", remote_path, Binary(data)) self._rpc_call_x(netns, "finish_copy_to", remote_path) return remote_path
def get_data_server(self,path): serv_data = {} i = 0 majority = 0 #To check for the most common data. if self.Qr % 2 == 0: majority = (self.Qr/2)+1 else: majority = (self.Qr+1)/2 # while len(serv_data)<(self.Qr): for i in range(self.Qw): try: serv_data[i] = (pickle.loads(self.data_server[i].get(Binary(path))["value"].data)) except Exception, E: print E print ('Data Server %i is not reachable.' %(i+1))
def get(self, key): # Remove expired entries self.check() # Default return value rv = {} # If the key is in the data structure, return properly formated results key = key.data if key in self.data: ent = self.data[key] now = datetime.now() if ent[1] > now: ttl = (ent[1] - now).seconds rv = {"value": Binary(ent[0]), "ttl": ttl} else: del self.data[key] return rv
def corrupt(self,key): key = Binary(key) key = key.data if key in self.data: ent = self.data[key] corruptValue = str(random.randint(100, 1000000)) tmplist = list(self.data[key]) tmplist[0] = corruptValue self.data[key] = tuple(tmplist) #since it is tuple element, we can't change it in common method returnValue = [] returnValue.append(ent[0]) returnValue.append(corruptValue) print self.data[key] print '------------data that is corruptted -> replace value----------' print returnValue return returnValue
def test_new_media_object(self): file_ = TemporaryFile() file_.write('My test content') file_.seek(0) media = {'name': 'zinnia_test_file.txt', 'type': 'text/plain', 'bits': Binary(file_.read())} file_.close() self.assertRaises(Fault, self.server.metaWeblog.newMediaObject, 1, 'contributor', 'password', media) new_media = self.server.metaWeblog.newMediaObject( 1, 'webmaster', 'password', media) self.assertTrue('/zinnia_test_file' in new_media['url']) default_storage.delete('/'.join([ UPLOAD_TO, new_media['url'].split('/')[-1]]))
def generate_image(event): png = event.replace('.xml', '.png') thumb = event.replace('.xml', '.thumb.png') png2 = 'latest_event.png' server = ServerProxy('http://localhost:%d' % port) #server = ServerProxy('https://atlas-live.cern.ch/event_files/Main/') #if not server.atlantis.event.xmlrpc.AServerXMLRPCEventSource.isReady(): # print 'Server is not ready, try again later' #else: # print "Requesting image and thumbnail for '%s'" % event images = server.atlantis.event.xmlrpc.AServerXMLRPCEventSource.generateDefaultImages( Binary(data), event, 1024, 0.1) open(png2, 'w').write(images[0].data) open(png, 'w').write(images[0].data) open(thumb, 'w').write(images[1].data)
def runAnalysis(): try: result = {} modelName = optionMenuWidget.cget("text") print "value is",modelName predictDate = cal.selection.date() print ("Date selected:"+str(predictDate.year)) inputValues={'model':modelName,'year':predictDate.year,'month':predictDate.month,'day':predictDate.day,} prediction = diskServer.getPrediction(Binary(pickle.dumps(inputValues))) print "prediction:::",pickle.loads(prediction.data) resultWindow(pickle.loads(prediction.data)['predicted'],modelName) #diskUIRoot.quit() except xmlrpclib.Fault, errcode: errorMessage = str(errcode) if errorMessage.find("IOError"): errorWindow("Invalid Input!! Please enter a valid Disk or Date") else: errorWindow("Remote Server Fault.. Try again later")
def list(range=()): '''Lists the song queue's contents. If a range is specified, only the items that fall within that range are listed. Arguments: Either none, or an array of integers that represents a range. * If no range is given, the whole list is returned. * If the range contains a single integer, it will represent all members of the queue whose index is greater than or equal to the value of the integer. * If the range contains two integers, it will represent all members of the queue whose index is greater than or equal to the value of the first integer and less than the value of the second integer. * If the range contains more than two integers, an error will occur. Return value: An array of (base64-encoded) strings, representing the selected range from the song queue's contents. ''' start, end = split_range(range) return [Binary(i) for i in data.song_queue[start:end]]
def get_abac_creds(root): ''' Reas a directory of ABAC certs and return them ready for an XMLRPC call. Technically this reads all the files in root into a list of xmlrpc Binary objects and return them. ''' creds = [] for r, d, f in os.walk(os.path.expanduser(root)): _ = d # appease eclipse for fn in f: try: ff = open(os.path.join(r, fn), 'r') c = ff.read() ff.close() creds.append(Binary(c)) except EnvironmentError, e: # XXX logger?? print sys.stderr, "Error on %s: %s" % (e.filename, e.strerror)
def read(self, path, size, offset, fh): if self.lost == 1: self.lost = 0 offset_pos = offset % blk_size start_blk = offset // blk_size last_blk = (offset + size) // blk_size data_dict = {} for key in self.data: key_path = key[0] key_blk = key[1] if key_path == path: if (key_blk >= start_blk) and (key_blk <= last_blk): if not key in data_dict: data_dict[key] = self.data[key] #print("this is the dictionary", data_dict) #print(self.data) return Binary(pickle.dumps(data_dict))
def request_data(self, prev_serv, next_serv): new_dict = {} if (prev_serv == 1 and next_serv == 0): for key in self.data: dat = self.data[key][0] checksum = self.data[key][1] copyn = self.data[key][2] if copyn < 2: new_dict[key] = [dat, checksum, copyn + 1] elif (prev_serv == 0 and next_serv == 1): for key in self.data: dat = self.data[key][0] checksum = self.data[key][1] copyn = self.data[key][2] if copyn > 0: new_dict[key] = [dat, checksum, copyn - 1] return Binary(pickle.dumps(new_dict))
def upload_image(ploneServer, folderPath, imageFile, imageDescrip=''): ''' Helper function that upload the image file. ''' assert os.path.isfile(imageFile), 'Oops! The image file does not exist.' imageTitle = os.path.splitext(os.path.split(imageFile)[1])[0] imagePost = { folderPath + '/' + imageTitle.lower(): [{ 'title': imageTitle, 'description': imageDescrip, 'image': Binary(open(imageFile, 'rb').read()) }, 'Image'] } imagePath = ploneServer.post_object(imagePost) print('Create a link to this image in Markdown with:\n' + \ '[![]({0}/@@images/image/preview)]({0})'.format(imagePath[0]))
def _run_bzr(argv, workdir, func): """Actually executes the command and build the response.""" try: os.chdir(workdir) exitval = func(argv) sys.stderr.flush() sys.stdout.flush() if isinstance(exitval, Fault): return_val = exitval else: # use a Binary object to wrap the output to avoid NULL and other # non xmlrpc (or client xml parsers) friendly chars out = Binary(data=sys.stdout.getvalue()) return_val = (exitval, out, sys.stderr.getvalue()) os.chdir(run_dir) return return_val except: import traceback traceback.print_exc(file=sys.__stderr__) raise
def truncate(self, path, length, fh=None): print("truncate") hash_val = pickle.loads(self.ms_helper.gethashVal(Binary(path))) delete_blocks = pickle.loads( self.ms_helper.truncate(Binary(path), Binary(str(length)))) offset = length % MaxBLOCKSIZE for b in delete_blocks: block_num = int(b[len(hash_val):]) server_id = (int(hash_val) + block_num) % numDServers if (offset != 0): self.ds_helpers[server_id].truncate(Binary(b), Binary(offset)) offset = 0 else: self.ds_helpers[server_id].delete(Binary(b))