How to use the msgpack.unpackb function in msgpack

To help you get started, we’ve selected a few msgpack examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oleiade / Elevator / tests / core / api_tests.py View on Github external
def test_slice_with_limit(self):
        message = self.request_message('SLICE', ['1', 3])
        header, content = self.handler.command(message)

        plain_header = msgpack.unpackb(header)
        plain_content = msgpack.unpackb(content)

        self.assertEqual(plain_header['status'], SUCCESS_STATUS)
        self.assertIsInstance(plain_content['datas'], tuple)
        self.assertEqual(len(plain_content['datas']), 3)
        self.assertEqual(plain_content['datas'][0], ('1', '11'))
        self.assertEqual(plain_content['datas'][1], ('2', '12'))
        self.assertEqual(plain_content['datas'][2], ('3', '13'))
github msgpack / msgpack-python / test / test_timestamp.py View on Github external
def test_pack_datetime():
    t = Timestamp(42, 14000)
    dt = t.to_datetime()
    assert dt == datetime.datetime(1970, 1, 1, 0, 0, 42, 14, tzinfo=_utc)

    packed = msgpack.packb(dt, datetime=True)
    packed2 = msgpack.packb(t)
    assert packed == packed2

    unpacked = msgpack.unpackb(packed)
    print(packed, unpacked)
    assert unpacked == t

    unpacked = msgpack.unpackb(packed, timestamp=3)
    assert unpacked == dt

    x = []
    packed = msgpack.packb(dt, datetime=False, default=x.append)
    assert x
    assert x[0] == dt
    assert msgpack.unpackb(packed) is None
github DataDog / dd-trace-py / tests / test_integration.py View on Github external
def _decode(self, payload):
        """
        Helper function that decodes data based on the given Encoder.
        """
        if isinstance(self.api._encoder, JSONEncoder):
            return json.loads(payload)
        elif isinstance(self.api._encoder, MsgpackEncoder):
            return msgpack.unpackb(payload, encoding='utf-8')
github oleiade / Elevator / tests / core / api_tests.py View on Github external
def test_put_of_valid_key(self):
        message = self.request_message('PUT', ['a', '1'])
        header, content = self.handler.command(message)

        plain_header = msgpack.unpackb(header)
        plain_content = msgpack.unpackb(content)

        self.assertEqual(plain_header['status'], SUCCESS_STATUS)
        self.assertEqual(plain_content['datas'], None)
github cocaine / cocaine-core / scripts / cocaine-check-app.py View on Github external
request.setsockopt(zmq.LINGER, 0)
        request.connect('tcp://%s:5000' % host)

        # Statistics
        request.send_multipart([
            msgpack.packb(SLOT_INFO),
            msgpack.packb([])
        ])

        poller = zmq.Poller()
        poller.register(request, zmq.POLLIN)

        sockets = dict(poller.poll(timeout = timeout))

        if request in sockets and sockets[request] == zmq.POLLIN:
            response = msgpack.unpackb(request.recv())

            if app not in response["apps"]:
                print "The '%s' app was not found on '%s'." % (app, host)
                exit(1)

            state = response["apps"][app]["state"]
            print "The '%s' app is %s on '%s'." % (app, state, host)
            complain |= (state != "running")
        else:
            print "ERROR: Host '%s' is not responding." % host
            complain |= True

    exit(complain)
github borgbackup / borg / src / borg / archive.py View on Github external
manifest = Manifest(self.key, self.repository)
        archive_keys_serialized = [msgpack.packb(name.encode()) for name in ARCHIVE_KEYS]
        for chunk_id, _ in self.chunks.iteritems():
            cdata = self.repository.get(chunk_id)
            try:
                _, data = self.key.decrypt(chunk_id, cdata)
            except IntegrityError as exc:
                logger.error('Skipping corrupted chunk: %s', exc)
                self.error_found = True
                continue
            if not valid_msgpacked_dict(data, archive_keys_serialized):
                continue
            if b'cmdline' not in data or b'\xa7version\x01' not in data:
                continue
            try:
                archive = msgpack.unpackb(data)
            # Ignore exceptions that might be raised when feeding
            # msgpack with invalid data
            except (TypeError, ValueError, StopIteration):
                continue
            if valid_archive(archive):
                archive = ArchiveItem(internal_dict=archive)
                name = archive.name
                logger.info('Found archive %s', name)
                if name in manifest.archives:
                    i = 1
                    while True:
                        new_name = '%s.%d' % (name, i)
                        if new_name not in manifest.archives:
                            break
                        i += 1
                    logger.warning('Duplicate archive name %s, storing as %s', name, new_name)
github django / asgi_ipc / asgi_ipc.py View on Github external
except posix_ipc.BusyError:
            self._reset()
            self.semaphore.acquire(0)
        try:
            # Seek to start of memory segment
            self.mmap.seek(0)
            # The first four bytes should be "ASGD", followed by four bytes
            # of version (we're looking for 0001)
            signature = self.mmap.read(8)
            if signature != self.signature:
                # Start fresh
                return self.datatype()
            else:
                # There should then be four bytes of length
                size = struct.unpack(b'!I', self.mmap.read(4))[0]
                return msgpack.unpackb(self.mmap.read(size), encoding="utf8")
        finally:
            self.semaphore.release()
github fake-name / ReadableWebProxy / WebMirror / OutputFilters / AmqpInterface.py View on Github external
'chunk-count' : total_chunks,
				'chunks'      : {}
			}

		# Check our chunk count is sane.
		assert self.chunks[merge_key]['chunk-count'] == total_chunks
		self.chunks[merge_key]['chunks'][chunk_num] = data


		# TODO: clean out partial messages based on their age (see 'first-seen')

		if len(self.chunks[merge_key]['chunks']) == total_chunks:
			components = list(self.chunks[merge_key]['chunks'].items())
			components.sort()
			packed_message = b''.join([part[1] for part in components])
			ret = msgpack.unpackb(packed_message, encoding='utf-8', use_list=False)

			del self.chunks[merge_key]

			self.log.info("Received all chunks for key %s! Decoded size: %0.3fk from %s chunks. Active partial chunked messages: %s.",
				merge_key, len(packed_message) / 1024, len(components), len(self.chunks))

			return ret

		else:
			return None
github cyberegoorg / cetech / playground / src / playground / modules / logwidget.py View on Github external
def run(self):
        self.socket.set_string_option(SUB, SUB_SUBSCRIBE, b'')
        self.socket.connect(self.url)
        while True:
            try:
                msg = self.socket.recv()
                msg_pack = msgpack.unpackb(msg, encoding='utf-8')

                for h in self.handlers:
                    h(**msg_pack)

            except nanomsg.NanoMsgAPIError as e:
                raise
github openstack / dragonflow / dragonflow / db / pub_sub_api.py View on Github external
def unpack_message(message):
    entry = None
    try:
        entry = msgpack.unpackb(message, encoding='utf-8')
    except Exception:
        LOG.exception("Error in unpack_message: ")
    return entry