def test_unpack_exceptions(self): for (name, data, exception) in unpack_exception_test_vectors: print("\tTesting %s" % name) try: umsgpack.unpackb(data) except Exception as e: self.assertTrue(isinstance(e, exception))
def unserialize(self, payload): """ Implements :func:`autobahn.wamp.interfaces.IObjectSerializer.unserialize` """ if self._batched: msgs = [] N = len(payload) i = 0 while i < N: # read message length prefix if i + 4 > N: raise Exception("batch format error [1]") l = struct.unpack("!L", payload[i:i + 4])[0] # read message data if i + 4 + l > N: raise Exception("batch format error [2]") data = payload[i + 4:i + 4 + l] # append parsed raw message msgs.append(umsgpack.unpackb(data)) # advance until everything consumed i = i + 4 + l if i != N: raise Exception("batch format error [3]") return msgs else: unpacked = umsgpack.unpackb(payload) return [unpacked]
def dequeue(self): self.cur.execute("SELECT * FROM data") results = self.cur.fetchone() id = results[0] data = umsgpack.unpackb(results[1]) unit = umsgpack.unpackb(results[2]) return id, data, unit
def main(): # Switch to API Mode print ">> Configuring Xbee" time.sleep(1) ser_xbee.write("+++") # enter command mode time.sleep(1) if (ser_xbee.read(3) == "OK\r"): print "AT \tOK" ser_xbee.write("ATAP 1\r") # switch to API mode print "ATAP\t%s" %ser_xbee.read(3) # wait for reply ser_xbee.write("ATID %s\r" %XBEE_MESH_ID) # mesh id print "ATID\t%s" %ser_xbee.read(3) # wait for reply ser_xbee.write("ATCH %s\r" %XBEE_MESH_CH) # mesh ch print "ATCH\t%s" %ser_xbee.read(3) # wait for reply ser_xbee.write("ATWR\r") # apply settings print "ATWR\t%s" %ser_xbee.read(3) # wait for reply ser_xbee.write("ATAG %s\r" %XBEE_MESH_DL) # apply settings print "ATAG\t%s" %ser_xbee.read(3) # wait for reply ser_xbee.write("ATCN\r") # exit command mode else: print "AT \tFAIL" # Configure Xbee #xbee.at(command="CE", parameter="0") # router mode #xbee.at(command="ID", parameter=XBEE_MESH_ID) # mesh id #xbee.at(command="CH", parameter=XBEE_MESH_CH) # mesh channel #xbee.at(command="WR") # apply #xbee.at(command="AG", parameter=XBEE_MESH_DL) # broadcast as master # Receive packets frames = {} # buffer for storing unprocessed frames packet = "" # buffer for placing string after processing packets = [] # all packets received while True: frame = xbee.wait_read_frame() # wait for frames print frame["source_addr"] frame = unpackb(frame["data"]) # decode json pid = frame["id"] # get id pnum = frame["pn"] # get packet number pdata = frame["dt"] # get data if not pid in frames: # if new packet stream frames[pid] = {} # initialize slot frames[pid][pnum] = pdata # store data from frame #print frame if 0 in frames[pid]: # if we already have the header if frames[pid][0] == len(frames[pid]): # and we have all the frames for i in range(1, frames[pid][0]): # join data from frames packet += frames[pid][i] frames[pid] = {} packet = unpackb(packet) timestamp = time.strftime("%H:%M:%S", time.localtime()) print "Got packet from: %s at %s" %(packet["ant_mac"], timestamp) packets += (timestamp, packet) packet = ""
def aes_decrypt(word, key=config.aes_key, iv=None): if iv is None: word, iv = umsgpack.unpackb(word) else: raise Exception('no iv error') aes = AES.new(key, AES.MODE_CBC, iv) word = aes.decrypt(word) return umsgpack.unpackb(word)
def test_unpack_tuple(self): # Use tuple test vector (_, obj, data, obj_tuple) = tuple_test_vectors[0] # Unpack with default options (list) self.assertEqual(umsgpack.unpackb(data), obj) # Unpack with use_tuple=False (list) self.assertEqual(umsgpack.unpackb(data, use_tuple=False), obj) # Unpack with use_tuple=True (tuple) self.assertEqual(umsgpack.unpackb(data, use_tuple=True), obj_tuple)
def aes_decrypt(word, key=config.aes_key, iv=None): if iv is None: word, iv = umsgpack.unpackb(word) else: raise Exception('no iv error') aes = AES.new(key, AES.MODE_CBC, iv) word = aes.decrypt(word) while word: try: return umsgpack.unpackb(word) except umsgpack.ExtraData: word = word[:-1]
def receive_loop(self): """ This is the receive loop for zmq messages It is assumed that this method may be overwritten to meet the needs of the application It returns payload via user provided callback method :return: """ while True: try: data = self.subscriber.recv_multipart(zmq.NOBLOCK) self.incoming_message_processing(data[0].decode(), umsgpack.unpackb(data[1])) time.sleep(.001) except zmq.error.Again: try: time.sleep(.001) self.root.update() except KeyboardInterrupt: self.root.destroy() self.publisher.close() self.subscriber.close() self.context.term() sys.exit(0) except KeyboardInterrupt: self.root.destroy() self.publisher.close() self.subscriber.close() self.context.term() sys.exit(0)
def get_message(self): """ This method is called from the tkevent loop "after" call. It will poll for new zeromq messages :return: """ try: data = self.subscriber.recv_multipart(zmq.NOBLOCK) self.incoming_message_processing(data[0].decode(), umsgpack.unpackb(data[1])) self.root.after(1, self.get_message) except zmq.error.Again: try: time.sleep(.0001) self.root.after(1, self.get_message) except KeyboardInterrupt: self.root.destroy() self.publisher.close() self.subscriber.close() self.context.term() sys.exit(0) except KeyboardInterrupt: self.root.destroy() self.publisher.close() self.subscriber.close() self.context.term() sys.exit(0)
def get_current_user(self): ret = self.get_secure_cookie('user', max_age_days=config.cookie_days) if not ret: return ret user = umsgpack.unpackb(ret, encoding="utf8") user['isadmin'] = 'admin' in user['role'] if user['role'] else False return user
def __init__( self, version=VERSION, close_date=None, previous_block_signature=None, merkle_root=None, signer=None, total=None, b_transactions=None, b_engagements=None, signature=None, ): self.version = version self.close_date = datetime.date.fromisoformat( close_date) if close_date else None self.previous_block_signature = previous_block_signature self.merkle_root = merkle_root self.signer = signer self.total = total self.transactions = ( [Transaction(*umsgpack.unpackb(b_tx)) for b_tx in b_transactions] if b_transactions else []) self.engagements = [] self.signature = signature self.packer = BytePacker()
def _load_cache_settings(self): """Load settings from cache to self.cached_settings.""" successful = _ensure_file(self.cache_file) if not successful: LOG.debug("Unable to load cache.") return with open(self.cache_file, "rb") as stream: LOG.debug("Opening subscription cache to retrieve subscriptions.") data = stream.read() if data == b"": LOG.debug("Received empty string from cache.") return False for encoded_sub in umsgpack.unpackb(data): try: decoded_sub = Subscription.Subscription.decode_subscription(encoded_sub) except Error.MalformedSubscriptionError as exception: LOG.debug("Encountered error in subscription decoding:") LOG.debug(exception) LOG.debug("Skipping this sub.") continue self.cache_map["by_name"][decoded_sub.name] = decoded_sub self.cache_map["by_url"][decoded_sub.original_url] = decoded_sub return True
def datagramReceived(self, datagram, address): time_keeper = TimeKeeper() if self.noisy: log.msg("received datagram from %s" % repr(address)) if len(datagram) < 22: log.msg("received datagram too small from %s, ignoring" % repr(address)) return msgID = datagram[1:21] time_keeper.start_clock() data = umsgpack.unpackb(datagram[21:]) time_keeper.stop_clock("time_unpack_msg") # self.log.debug("[BENCH] LOW RPC RECEIVE -> %s " % (time_keeper.get_summary(),)) if datagram[:1] == b'\x00': self._acceptRequest(msgID, data, address) elif datagram[:1] == b'\x01': self._acceptResponse(msgID, data, address) else: # otherwise, don't know the format, don't do anything log.msg("Received unknown message from %s, ignoring" % repr(address))
def Configure(self, request, context): log.info(f"Configure: {request}") prepared_config = umsgpack.unpackb(request.config.msgpack) self.__provider = self.__provider_class(prepared_config) return tfplugin5_0_pb2.Configure.Response()
def test_unpack_ordered_dict(self): # Use last composite test vector (a map) (_, obj, data) = composite_test_vectors[-1] # Unpack with default options (unordered dict) unpacked = umsgpack.unpackb(data) self.assertTrue(isinstance(unpacked, dict)) # Unpack with unordered dict unpacked = umsgpack.unpackb(data, use_ordered_dict=False) self.assertTrue(isinstance(unpacked, dict)) # Unpack with ordered dict unpacked = umsgpack.unpackb(data, use_ordered_dict=True) self.assertTrue(isinstance(unpacked, OrderedDict)) self.assertEqual(unpacked, obj)
def test_unpack_invalid_string(self): # Use last unpack exception test vector (an invalid string) (_, data, _) = unpack_exception_test_vectors[-1] obj = umsgpack.unpackb(data, allow_invalid_utf8=True) self.assertTrue(isinstance(obj, umsgpack.InvalidString)) self.assertEqual(obj, b"\x80")
def run_bb_i2c_bridge(self): """ Start up the bridge :return: """ # self.pi.set_mode(11, pigpio.INPUT) # cb1 = self.pi.callback(11, pigpio.EITHER_EDGE, self.cbf) while True: # noinspection PyBroadException try: z = self.subscriber.recv_multipart(zmq.NOBLOCK) self.payload = umsgpack.unpackb(z[1]) # print("[%s] %s" % (z[0], self.payload)) # print(self.payload) command = self.payload['command'] if command == 'i2c_request': self.i2c_request() else: # print("can't execute unknown command'") pass time.sleep(.0001) except KeyboardInterrupt: sys.exit(0) except zmq.error.Again: time.sleep(.0001)
def process(self, message): datagram, host, port = umsgpack.unpackb(message[0]) reply = self.processAuth(datagram, host, port) logger.info("[Radiusd] :: Send radius response: %s" % repr(reply)) if self.config.system.debug: logger.debug(reply.format_str()) self.pusher.push(umsgpack.packb([reply.ReplyPacket(),host,port]))
def transform_manifest(mani): identities = dict() posts = list() manifest = { 'identities': identities, 'posts': posts, } for key, pk in enumerate(mani['identities']): ident = { 'public_key': pk, #b64encode(pk), 'verify_key': VerifyKey(b64decode(pk)), } identities[key] = ident for (smessage, ident_index) in mani['posts']: identity = identities[ident_index] vk = identity['verify_key'] mpacked = vk.verify(b64decode(smessage)) to, link = umsgpack.unpackb(mpacked) posts.append({ 'to': to, 'link': link, 'signature': smessage, 'identity': identity, }) return manifest
async def _decrypt_metadata(self, encrypted_metadata, user_vault_key): import zipfile from io import BytesIO from syncrypt.pipes import SnappyDecompress import umsgpack # decrypt package export_pipe = Once(user_vault_key) \ >> DecryptRSA_PKCS1_OAEP(self.identity.private_key) package_info = await export_pipe.readall() zipf = zipfile.ZipFile(BytesIO(package_info), 'r') vault_public_key = zipf.read('.vault/id_rsa.pub') vault_key = zipf.read('.vault/id_rsa') vault_identity = Identity.from_key(vault_public_key, self.config, private_key=vault_key) sink = Once(encrypted_metadata) \ >> DecryptRSA_PKCS1_OAEP(vault_identity.private_key) \ >> SnappyDecompress() serialized_metadata = await sink.readall() return umsgpack.unpackb(serialized_metadata)
def get_current_user(self): ret = self.get_secure_cookie('user', max_age_days=config.cookie_days) if not ret: return ret user = umsgpack.unpackb(ret) user['isadmin'] = 'admin' in user['role'] if user['role'] else False return user
async def get_next_message(self): """ This method uses an async future to retrieve the next message from the network. :return: If not message is available, None is returned, else the message is returned as a list containing the topic and payload. """ # create an asyncio Future future = asyncio.Future() try: # get the next available message data = self.subscriber.recv_multipart(zmq.NOBLOCK) # get the topic and unpack the payload topic = data[0].decode() payload = umsgpack.unpackb(data[1]) # place them into a list message = [topic, payload] # place the message in the future result future.set_result(message) # wait until the future reports that it is complete and then return the topic, payload list while not future.done(): await asyncio.sleep(.01) return future.result() except zmq.error.Again: # if no message is available, zmq throws the Again exception, so just return None return None
def test_40_with_rpc(self): data = dict(self.sample_task_http) data["url"] = "data:,hello" result = umsgpack.unpackb(self.rpc.fetch(data).data) self.assertEqual(result["status_code"], 200) self.assertIn("content", result) self.assertEqual(result["content"], "hello")
def read_config(): """ 读取配置文件,记得先判断配置文件是否存在,不存在的话先调用init_config()。具体步骤: ①读取配置文件中的所有内容 ②将内容反序列化,设置异常捕获 ③反序列化后应该得到一个list,为计算得到的SHA512和配置dict( [sha512, dict] ) ④计算配置dict的SHA512与读取的SHA512进行对比校验 不符合则抛出异常,符合则返回配置内容 """ with open("mrdb.conf", "rb") as config: data = config.read() try: config = umsgpack.unpackb(data) except umsgpack.ExtraData: # 反序列化出错,表示文件一定遭过损毁,抛出异常 raise Error.ConfigFileBroken except ValueError: # 反序列化出错,表示文件一定遭过损毁,抛出异常 raise Error.ConfigFileBroken except UnicodeDecodeError: # 反序列化出错,表示文件一定遭过损毁,抛出异常 raise Error.ConfigFileBroken # 读取保存的SHA512和配置内容 config_sha512 = config[0] config_config = config[1] # 计算配置内容的SHA512和保存的SHA512是否一致,不一致则抛出异常,一致则返回配置内容 if not generate_sha512(umsgpack.packb(config_config)) == config_sha512: raise Error.ConfigFileBroken else: return config_config
async def keep_alive(self): """ This method is used to keep the server up and running when not connected to Scratch :return: """ while True: # check for reporter messages try: [address, contents] = self.subscriber.recv_multipart(zmq.NOBLOCK) payload = umsgpack.unpackb(contents) # print("[%s] %s" % (address, payload)) board_num = address.decode() board_num = board_num[1] command = payload['command'] # we will ignore any i2c_replies if command == 'i2c_reply' or command == 'i2c_request': continue elif command == 'problem': data_string = command + '/' + board_num + ' ' + payload['problem'] else: # noinspection PyPep8 if not 'pin' in payload: continue else: pin = payload['pin'] value = payload['value'] data_string = command + '/' + board_num + '/' + pin + ' ' + value + '\n' # print(data_string) self.poll_reply += data_string except zmq.error.Again: await asyncio.sleep(.001)
def receive_loop(self): """ This is the receive loop for zmq messages. This method may be overwritten to meet the needs of the application before handling received messages. :return: """ for element in itertools.cycle(self.backplane_table): if element['subscriber']: try: data = element['subscriber'].recv_multipart(zmq.NOBLOCK) if self.numpy: payload = msgpack.unpackb(data[1], object_hook=m.decode) self.incoming_message_processing(data[0].decode(), payload) else: self.incoming_message_processing(data[0].decode(), umsgpack.unpackb(data[1])) except zmq.error.Again: try: time.sleep(self.loop_time) except KeyboardInterrupt: self.clean_up() sys.exit(0) except AttributeError: raise
def consume_single_chapter(self, task): logging.info("consume_single_chapter get a task") task = umsgpack.unpackb(task) rsp = self.request.get(task["href"]) rsp.encoding = "gbk" root = fromstring(rsp.text) content_div = root.xpath('//div[@id="content"]')[0] content = tostring(content_div, method="html", pretty_print=True, encoding="utf-8") content = content.decode("utf-8") content = content.replace("<br>", "") content = content.replace("\xa0", "") content = content.replace('<div id="content">', '') content = content.replace('</div>', "") # import pdb; pdb.set_trace() sqlSession = sqlalchemyConn.DBSession() chapter = Chapter(chapter_id=task["id"], title=task["name"], content=content, book_id=book_id, site=self.HOST) sqlSession.add(chapter) sqlSession.commit()
def from_bytes(self, b): """Load data from given bytes into the instance :returns: None """ hashed_blocks = umsgpack.unpackb(b) self._from_hashed_blocks(hashed_blocks)
def run_bb_bridge(self): """ Start up the bridge :return: """ # self.pi.set_mode(11, pigpio.INPUT) # cb1 = self.pi.callback(11, pigpio.EITHER_EDGE, self.cbf) while True: if self.last_problem: self.report_problem() # noinspection PyBroadException try: z = self.subscriber.recv_multipart(zmq.NOBLOCK) self.payload = umsgpack.unpackb(z[1]) # print("[%s] %s" % (z[0], self.payload)) command = self.payload['command'] if command == 'i2c_request': time.sleep(.001) continue elif command in self.command_dict: self.command_dict[command]() else: print("can't execute unknown command", str(command)) # time.sleep(.001) except KeyboardInterrupt: self.cleanup() sys.exit(0) except zmq.error.Again: time.sleep(.001)
def add(self, email, password, ip): if self.get(email=email, fields='1') is not None: raise self.DeplicateUser('duplicate username') now = time.time() if isinstance(ip, str): ip = utils.ip2int(ip) userkey = umsgpack.unpackb(crypto.password_hash(password))[0] hash = MD5.new() hash.update(password.encode('utf-8')) password_md5 = hash.hexdigest() insert = dict( email=email, email_verified=0, password=crypto.aes_encrypt(crypto.password_hash(password), userkey), userkey=crypto.aes_encrypt(userkey), nickname=None, role=None, ctime=now, mtime=now, atime=now, cip=ip, mip=ip, aip=ip, password_md5=password_md5, ) return self._insert(**insert)
def webui(ctx, host, port, cdn, scheduler_rpc, fetcher_rpc, max_rate, max_burst, username, password, need_auth, webui_instance): """ Run WebUI """ app = load_cls(None, None, webui_instance) g = ctx.obj app.config['taskdb'] = g.taskdb app.config['projectdb'] = g.projectdb app.config['resultdb'] = g.resultdb app.config['cdn'] = cdn if max_rate: app.config['max_rate'] = max_rate if max_burst: app.config['max_burst'] = max_burst if username: app.config['webui_username'] = username if password: app.config['webui_password'] = password app.config['need_auth'] = need_auth # fetcher rpc if isinstance(fetcher_rpc, six.string_types): import umsgpack fetcher_rpc = connect_rpc(ctx, None, fetcher_rpc) app.config['fetch'] = lambda x: umsgpack.unpackb(fetcher_rpc.fetch(x).data) else: # get fetcher instance for webui fetcher_config = g.config.get('fetcher', {}) scheduler2fetcher = g.scheduler2fetcher fetcher2processor = g.fetcher2processor testing_mode = g.get('testing_mode', False) g['scheduler2fetcher'] = None g['fetcher2processor'] = None g['testing_mode'] = True webui_fetcher = ctx.invoke(fetcher, async=False, **fetcher_config) g['scheduler2fetcher'] = scheduler2fetcher g['fetcher2processor'] = fetcher2processor g['testing_mode'] = testing_mode app.config['fetch'] = lambda x: webui_fetcher.fetch(x)[1] if isinstance(scheduler_rpc, six.string_types): scheduler_rpc = connect_rpc(ctx, None, scheduler_rpc) if scheduler_rpc is None and os.environ.get('SCHEDULER_NAME'): app.config['scheduler_rpc'] = connect_rpc(ctx, None, 'http://%s/' % ( os.environ['SCHEDULER_PORT_23333_TCP'][len('tcp://'):])) elif scheduler_rpc is None: app.config['scheduler_rpc'] = connect_rpc(ctx, None, 'http://localhost:23333/') else: app.config['scheduler_rpc'] = scheduler_rpc app.debug = g.debug g.instances.append(app) if g.get('testing_mode'): return app app.run(host=host, port=port)
def add(self, email, password, ip): if self.get(email=email, fields='1') is not None: raise self.DeplicateUser('duplicate username') now = time.time() if isinstance(ip, basestring): ip = utils.ip2int(ip) userkey = umsgpack.unpackb(crypto.password_hash(password))[0] insert = dict( email = email, email_verified = 0, password = crypto.aes_encrypt( crypto.password_hash(password), userkey), userkey = crypto.aes_encrypt(userkey), nickname = None, role = None, ctime = now, mtime = now, atime = now, cip = ip, mip = ip, aip = ip, ) return self._insert(**insert)
def unserialize(binary): ''' Unpack the original OpenConfig object, serialized using MessagePack. This is to be used when disable_security is set. ''' return umsgpack.unpackb(binary)
async def keep_alive(self): """ This method is used to keep the server up and running when not connected to Scratch :return: """ while True: # check for reporter messages try: [address, contents] = self.router_socket.recv_multipart(zmq.NOBLOCK) payload = umsgpack.unpackb(contents) # print("[%s] %s" % (address, payload)) board_num = address.decode() board_num = board_num[1] command = payload['command'] if command == 'problem': data_string = command + '/' + board_num + ' ' + payload['problem'] else: pin = payload['pin'] value = payload['value'] data_string = command + '/' + board_num + '/' + pin + ' ' + value + '\n' # print(data_string) self.poll_reply += data_string except zmq.error.Again: pass await asyncio.sleep(.001)
def test_invalid_info_len(self): created = base.create(self.btctxstore, self.wif, "info", []) # repack to eliminate namedtuples and simulate io repacked = umsgpack.unpackb(umsgpack.packb(created)) self.assertIsNone(info.read(self.btctxstore, repacked))
def start(self): ''' Start the worker process. ''' self._setup_ipc() # Start suicide polling thread thread = threading.Thread(target=self._suicide_when_without_parent, args=(os.getppid(), )) thread.start() self.__up = True while self.__up: bin_obj = self.sub.recv() msg_dict, address = umsgpack.unpackb(bin_obj, use_list=False) kwargs = self._parse(msg_dict) if not kwargs: continue oc_obj = self._emit(**kwargs) error = kwargs.get('error') model_name = kwargs.get('oc_model') host = msg_dict.get('host') timestamp = self._format_time(msg_dict.get('time', ''), msg_dict.get('date', '')) to_publish = { 'error': error, 'host': host, 'ip': address, 'timestamp': timestamp, 'open_config': oc_obj, 'message_details': msg_dict, 'model_name': model_name, 'os': self._name } self._publish(to_publish)
def test_invalid_name(self): created = signal.create(self.btctxstore, self.wif, "test") # repack to eliminate namedtuples and simulate io repacked = umsgpack.unpackb(umsgpack.packb(created)) self.assertIsNone(signal.read(self.btctxstore, repacked, "wrongname"))
def test_invalid_peer_type(self): created = base.create(self.btctxstore, self.wif, "peers", None) # repack to eliminate namedtuples and simulate io repacked = umsgpack.unpackb(umsgpack.packb(created)) self.assertIsNone(peers.read(self.btctxstore, repacked))
def test_40_with_rpc(self): data = copy.deepcopy(self.sample_task_http) data['url'] = 'data:,hello' result = umsgpack.unpackb(self.rpc.fetch(data).data) self.assertEqual(result['status_code'], 200) self.assertIn('content', result) self.assertEqual(result['content'], 'hello')
def client_parse(rfidInput, client, client_socket): clientReturn = None try: message = umsgpack.unpackb(rfidInput) print message except TypeError: message = [''] if message[0] in socket_options: clientReturn = socket_options[message[0]]['d'](message) else: clientReturn = {'response': response(rfidInput[0], 'InvalidOption')} if clientReturn == None: return if 'response' not in clientReturn: pass elif clientReturn['response'] is '': client_socket.send( umsgpack.packb(response(rfidInput[0], 'ServerError'))) else: print "Returning", clientReturn['response'], "to", client['addr'] client_socket.send(umsgpack.packb(clientReturn['response'])) print ":".join("{0:x}".format(ord(c)) for c in umsgpack.packb(clientReturn['response']))
def test_unpack_composite(self): for (name, obj, data) in composite_test_vectors: obj_repr = repr(obj) print("\tTesting %s: object %s" % (name, obj_repr if len(obj_repr) < 24 else obj_repr[0:24] + "...")) self.assertEqual(umsgpack.unpackb(data), obj)
def get_message(self): """ This method is called from the tkevent loop "after" call. It will poll for new zeromq messages :return: """ try: data = self.subscriber.recv_multipart(zmq.NOBLOCK) self.incoming_message_processing(data[0].decode(), umsgpack.unpackb(data[1])) time.sleep(.001) self.root.after(1, self.get_message) except zmq.error.Again: try: time.sleep(.001) self.root.after(1, self.get_message) except KeyboardInterrupt: self.root.destroy() self.publisher.close() self.subscriber.close() self.context.term() sys.exit(0) except KeyboardInterrupt: self.root.destroy() self.publisher.close() self.subscriber.close() self.context.term() sys.exit(0)
def test_ext_serializable_subclass(self): @umsgpack.ext_serializable(0x10) class Rectangle: def __init__(self, length, width): self.length = length self.width = width def __eq__(self, other): return self.length == other.length and self.width == other.width def packb(self): return umsgpack.packb([self.length, self.width]) @classmethod def unpackb(cls, data): return cls(*umsgpack.unpackb(data)) class Square(Rectangle): def __init__(self, width): Rectangle.__init__(self, width, width) # Test pack (packs base class) packed = umsgpack.packb(Square(5)) self.assertEqual(packed, b"\xc7\x03\x10\x92\x05\x05") # Test unpack (unpacks base class) unpacked = umsgpack.unpackb(packed) self.assertEqual(unpacked, Rectangle(5, 5)) # Unregister Ext serializable classes to prevent interference with # subsequent tests umsgpack._ext_classes_to_code = {} umsgpack._ext_code_to_classes = {}
async def keep_alive(self): """ This method is used to keep the server up and running when not connected to Scratch :return: """ while True: # check for reporter messages try: [address, contents] = self.router_socket.recv_multipart(zmq.NOBLOCK) payload = umsgpack.unpackb(contents) # print("[%s] %s" % (address, payload)) board_num = address.decode() board_num = board_num[1] command = payload['command'] if command == 'problem': data_string = command + '/' + board_num + ' ' + payload[ 'problem'] else: pin = payload['pin'] value = payload['value'] data_string = command + '/' + board_num + '/' + pin + ' ' + value + '\n' # print(data_string) self.poll_reply += data_string except zmq.error.Again: pass await asyncio.sleep(.001)
def test_invalid_version_value(self): _info = ["invalidversion", None, None, None] created = base.create(self.btctxstore, self.wif, "info", _info) # repack to eliminate namedtuples and simulate io repacked = umsgpack.unpackb(umsgpack.packb(created)) self.assertIsNone(info.read(self.btctxstore, repacked))
def get_nowait(self, ack=False): with self.lock: message = self.channel.basic_get(self.name, not ack) if message is None: raise BaseQueue.Empty if ack: self.channel.basic_ack(message.delivery_tag) return umsgpack.unpackb(message.body)
def test_invalid_storage_value_types(self): _info = ["0.0.0", [None, 0, 0], None, None] created = base.create(self.btctxstore, self.wif, "info", _info) # repack to eliminate namedtuples and simulate io repacked = umsgpack.unpackb(umsgpack.packb(created)) self.assertIsNone(info.read(self.btctxstore, repacked))
def test_40_with_rpc(self): request = copy.deepcopy(self.sample_task_http) request['url'] = 'data:,hello' result = umsgpack.unpackb(self.rpc.fetch(request).data) response = rebuild_response(result) self.assertEqual(response.status_code, 200) self.assertEqual(response.text, 'hello')