def test_binary_string(self): #TODO: python2 support test = b'aaaa\x00\xf0\xab\xffddddmfdlksdmflkd' test_dump = jsons.dumps({'test': test}) print(test_dump) test_load = jsons.loads(test_dump)['test'] self.assertIsInstance(test_load, bytes) self.assertEqual(test_load, test)
def test_serilizable_class_n(self): test = TestSerializable2('testa', 'testb') json_dump = jsons.dumps({'test': test}, meta_prefix="__") print(json_dump) self.assertTrue(True) json_load = jsons.loads(json_dump, meta_prefix="__") self.assertIsInstance(json_load['test'], TestSerializable2) self.assertEqual(test.a, json_load['test'].a) self.assertNotIn('_temp', json_load['test'].__dict__)
def test_set_n_tuple(self): t = (1, 2, 3) s = set(t) test = {'s': s, 't': t} test_dump = jsons.dumps(test) print(test_dump) test_load = jsons.loads(test_dump) # self.assertIsInstance(test_load['t'], tuple) self.assertIsInstance(test_load['s'], set)
def ping_request(server, node_name): try: resp = requests.post(server + '/namlat/ping', data={'node_name': node_name}) return json.loads(resp.text)['ping'] except: logger.error("Exception while sending ping request to server:%s", server, exc_info=True) return False
def pull_request(server, node_name): try: # resp = requests.post(server+'/namlat/pull', data={'last_commit_id': last_commit_id, "node_name": node_name}) resp = requests.post(server + '/namlat/pull', data={"node_name": node_name}) data = json.loads(resp.text) # return data['updates_log'], data['mail_bag'] return data['mail_bag'] except Exception as e: logger.error("Exception while sending pull request to server:%s", server, exc_info=True) return None
def create_node_request(server, public_key, node_name): try: resp = requests.post( server + '/namlat/createNode', data={ 'gw': server, # 'address':address, 'public_key': public_key, 'node_name': node_name }) # logger.info("received %dB sync data", len(resp.text)) # logger.info("received data: %s", resp.text) resp_data = json.loads(resp.text) # return resp_data['accepted'], resp_data['sync_data'], resp_data['sync_logs'] return resp_data['accepted'] except Exception as e: logger.error("Exception while sending pull request to server:%s", server, exc_info=True) return None
def update(): logger.debug("received client update") try: # data_lock.acquire() if 'outgoing_mail' in request.form: logger.debug("update request: %s", request.form) # old_commit_id = request.form['old_commit_id'] outgoing_mail = json.loads(request.form['outgoing_mail']) # update_request_dict = json.loads(request.form['update']) # update = get_update_from_request_dict(update_request_dict) commit_id = server.updati(outgoing_mail) logger.debug("returning commit_id=%s", commit_id) resp_body = json.dumps({"OK": True}) resp = Response(resp_body, status=200, mimetype='application/json') return resp else: logger.warning( "'outgoing_mail' params are not in the request data") return error_400() except Exception as e: logger.error("error in update request: %s", str(e), exc_info=True) return error_400()
def receive(self): state = 0 data = '' size = 0 obj_ser = '' obj = None state2_tries = 0 state8_tries = 0 state12_tries = 0 while True: if state == 0: # Print('receiving... ', end='') data = self.sock.recv(BUFFER_SIZE).decode() # Print('received : ' + data) if data.endswith('\\'): state = 1 else: state = 2 elif state == 1: if data.startswith('1'): state = 3 elif data.startswith('s'): state = 4 else: state = 2 elif state == 2: if state2_tries < self.max_tries: state2_tries += 1 # Print('sending... ', end='') self.sock.sendall(b'r') # Print('sent k') state = 0 else: raise IOError("Data malformated!") elif state == 3: obj_ser = data[1:-1] try: obj = json.loads(obj_ser) state = 5 except: state = 2 elif state == 4: size = int(data[1:-1]) obj_ser = '' # print 'received: s%d//' % (size) # Print('sending... ', end='') self.sock.sendall(b'k') # Print('sent k') state = 6 elif state == 5: # Print('sending... ', end='') self.sock.sendall(b'k') # Print('sent k') # logger.debug('received obj: %s', obj) return obj elif state == 6: # Print('receiving... ', end='') data = self.sock.recv(BUFFER_SIZE).decode() # Print('received : ' + data) if data.endswith('\\'): state = 7 else: state = 8 elif state == 7: if data.startswith('c'): # print 'received: c + %d'%(len(data)-2) state = 9 elif data.startswith('F'): # print 'received: F' state = 10 else: state = 8 elif state == 8: if state8_tries < self.max_tries: state8_tries += 1 # Print('sending... ', end='') self.sock.sendall(b'r') # Print('sent r') state = 6 else: raise IOError("Data malformated!") elif state == 9: obj_ser += data[1:-1] # Print('sending... ', end='') self.sock.sendall(b'k') # Print('sent k') state = 6 elif state == 10: if size == len(obj_ser): try: obj = json.loads(obj_ser) state = 11 except: state = 12 else: state = 12 elif state == 11: # Print('sending... ', end='') self.sock.sendall(b'K') # Print('sent K') # logger.debug('received obj: %s', obj) return obj elif state == 12: if state12_tries < self.max_tries: state12_tries += 1 # Print('sending... ', end='') self.sock.sendall(b'R') # Print('sent R') state = 0 else: raise IOError("Data malformated!")