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 create_node(): logger.debug("received client createNode") try: # data_lock.acquire() if 'gw' in request.form and 'public_key' in request.form and 'node_name' in request.form: # and 'address' in request.form \ # gw = request.form['gw'] # address = request.form['address'] public_key = request.form['public_key'] node_name = request.form['node_name'] accepted = server.create_node(public_key, node_name) # if accepted: # sync_data, sync_logs = server.sync() # else: # sync_data, sync_logs = None, None # logger.debug("response: %s", {'accepted': accepted, 'sync_data':sync_data, 'sync_logs':sync_logs}) resp_body = json.dumps({ 'accepted': accepted }) #, 'sync_data': sync_data, 'sync_logs': sync_logs}) resp = Response(resp_body, status=200, mimetype='application/json') return resp else: logger.warning("bad parameters!") return error_400() except Exception as e: logger.error("error in update request: %s", str(e), exc_info=True) return error_400()
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 update_request(server, outgoing_mail): try: resp = requests.post(server + '/namlat/update', data={'outgoing_mail': json.dumps(outgoing_mail)}) return resp.status_code == 200 except Exception as e: logger.error("Exception while sending pull request to server:%s", server, exc_info=True) return None
def ping(): # with data_lock: logger.debug("received ping request") if 'node_name' in request.form: resp_body = json.dumps( {"ping": server.ping(request.form['node_name'])}) resp = Response(resp_body, status=200, mimetype='application/json') return resp else: logger.warning("'node_name' param is not in the request data") return error_400()
def pull(): logger.debug("received pull request") try: # data_lock.acquire() if 'node_name' in request.form: # last_commit_id = request.form['last_commit_id'] node_name = request.form['node_name'] mail_bag = server.pull(node_name) resp_body = json.dumps({"mail_bag": mail_bag}) resp = Response(resp_body, status=200, mimetype='application/json') return resp else: logger.warning("'last_commit_id' param is not in the request data") return error_400() except Exception as e: logger.error("error in pull request: %s", str(e), exc_info=True) return error_400()
def send(self, obj): # logger.debug("sending obj: %s", obj) obj_ser = json.dumps(obj) size = len(obj_ser) tries = 0 if size > MAX_OBJECT_SIZE: raise ValueError("Can't send all this data!") try: if size < BUFFER_SIZE - 2: self._send('1' + obj_ser + '\\') else: self._send_fragmented(obj_ser, size) except Exception: # if tries > self.max_tries or not self.reconnect(): if tries > self.max_tries: #or not self.reconnect(): raise else: tries += 1
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()