本文整理汇总了Python中msgpack.unpackb函数的典型用法代码示例。如果您正苦于以下问题:Python unpackb函数的具体用法?Python unpackb怎么用?Python unpackb使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unpackb函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: recv
def recv(sock, **kwargs):
msg = sock.recv()
print "recv: ", msg, kwargs
try:
msgpack.unpackb(msg)
except Exception:
"Error: invalid msgpack"
示例2: process_request
def process_request():
frames = socket.recv_multipart()
# killed by gsd
i = frames.index('')
command = frames[i + 2]
if command == '\x02':
global interrupted
interrupted = True
return
i = frames.index('', 1)
sequence, timestamp, expiry = msgpack.unpackb(frames[i+1])
method = frames[i+2]
params = msgpack.unpackb(frames[i+3])
try:
global converter
ret = getattr(converter, method)(*params)
except:
ret = ''
frames = frames[:i+1]
now = int(round(time.time() * 1000))
frames.append(msgpack.packb([sequence, now, 200]))
frames.append(msgpack.packb(ret))
socket.send_multipart(frames)
示例3: show_raw_results
def show_raw_results(campaign_id):
campaign = CampaignController.get(campaign_id)
if 'lastUpdate' in request.args:
time = datetime.datetime.utcfromtimestamp(float(request.args['lastUpdate']))
results = ResultsController.get_all_since_time(campaign_id, time)
else:
results = ResultsController.get_all(campaign_id)
if len(results) > 0:
first_result = results[0]
timestamp = (first_result.date_added - datetime.datetime(1970, 1, 1)).total_seconds()
else:
timestamp = (datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds()
results_arr = []
for result in results:
result_obj = msgpack.unpackb(result.data)
dataset_obj = msgpack.unpackb(result.dataset.data)
results_arr.append({'dataset': dataset_obj, 'result': result_obj})
data = msgpack.packb({'timestamp' : timestamp, 'results' : results_arr})
res = current_app.make_response(data)
filename = safe_filename(campaign.title) + '_results_' + str(math.floor(timestamp)) + '.msgpack'
res.headers.add('Content-Disposition', 'attachment;filename=' + filename)
res.mimetype = 'application/octet-stream'
return res
示例4: run_once
def run_once(self):
self._feed_buffer()
channel_idx, buf, fin = self._unpack_buffer()
if channel_idx is None:
return
elif channel_idx == -1:
raise FluxUSBError("USB protocol broken")
elif channel_idx < 0x80:
channel = self.channels.get(channel_idx)
if channel is None:
raise FluxUSBError("Recv bad channel idx 0x%02x" % channel_idx)
if fin == 0xfe:
channel.on_object(msgpack.unpackb(buf))
elif fin == 0xff:
self._send_binary_ack(channel_idx)
channel.on_binary(buf)
elif fin == 0x80:
channel.on_binary_ack()
else:
raise FluxUSBError("Recv bad fin 0x%02x" % fin)
elif channel_idx == 0xf0:
if fin != 0xfe:
raise FluxUSBError("Recv bad fin 0x%02x" % fin)
self._on_channel_ctrl_response(msgpack.unpackb(buf))
else:
raise FluxUSBError("Recv bad control channel 0x%02x" % channel_idx)
示例5: query
def query(client, i):
t = time.time()
v = {'dd_%s'%(i):'abc12333:'+ str(t)}
print v
client.send( packb(v))
while True:
y = client.recv(10240)
if not y:
break
print unpackb(y)
print time.time() - t
#print dir(client)
#client.send('0')
#y = client.recv(10240)
#print "[%s]"%(y)
gevent.sleep(0)
示例6: recv
def recv(self, s):
m = s.recv_multipart()
if len(m) == 6:
id, client_id, null, token, mtype, data = m
mtype = msgpack.unpackb(mtype)
mtype = MAP[mtype]
return id, client_id, token.decode('utf-8'), mtype, data.decode('utf-8')
elif len(m) == 5:
id, null, token, mtype, data = m
mtype = msgpack.unpackb(mtype)
mtype = MAP[mtype]
return id, token.decode('utf-8'), mtype, data.decode('utf-8')
elif len(m) == 4:
id, token, mtype, data = m
mtype = msgpack.unpackb(mtype)
mtype = MAP[mtype]
return id, token.decode('utf-8'), mtype, data.decode('utf-8')
elif len(m) == 3:
id, mtype, data = m
try:
mtype = msgpack.unpackb(mtype)
mtype = MAP[mtype]
except msgpack.exceptions.ExtraData:
pass
return id, mtype, data.decode('utf-8')
else:
mtype, data = m
return mtype, data.decode("utf-8")
示例7: unserialize
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(msgpack.unpackb(data, encoding = 'utf-8'))
## advance until everything consumed
i = i+4+l
if i != N:
raise Exception("batch format error [3]")
return msgs
else:
return [msgpack.unpackb(payload, encoding = 'utf-8')]
示例8: run_once
def run_once(self):
self._feed_buffer()
channel_idx, buf, fin = self._unpack_buffer()
if channel_idx is None:
return
elif channel_idx == -1:
raise FluxUSBError("USB protocol broken, got zero data length.")
elif channel_idx < 0x80:
channel = self.channels.get(channel_idx)
if channel is None:
raise FluxUSBError("Recv bad channel idx 0x%02x" % channel_idx)
if fin == 0xf0:
channel.on_object(msgpack.unpackb(buf, encoding="utf8",
unicode_errors="ignore"))
elif fin == 0xff:
self._send_binary_ack(channel_idx)
channel.on_binary(buf)
elif fin == 0xc0:
channel.on_binary_ack()
else:
raise FluxUSBError("Recv bad fin 0x%02x" % fin)
elif channel_idx == 0xa0 and fin == 0xff:
logger.debug("Recv padding")
elif channel_idx == 0xf1:
if fin != 0xf0:
raise FluxUSBError("Recv bad fin 0x%02x" % fin)
self._on_channel_ctrl_response(msgpack.unpackb(buf))
elif channel_idx == 0xfb:
self._on_pong(buf)
else:
self.stop()
self.close()
raise FluxUSBError("Recv bad control channel 0x%02x" % channel_idx)
示例9: decode
def decode(obj):
if isinstance(obj, ExtType):
if obj.code == TYPE_PSET:
unpacked_data = unpackb(obj.data,
use_list=False,
encoding='utf-8')
return pset(decode(item) for item in unpacked_data)
if obj.code == TYPE_PLIST:
unpacked_data = unpackb(obj.data,
use_list=False,
encoding='utf-8')
return plist(decode(item) for item in unpacked_data)
if obj.code == TYPE_PBAG:
unpacked_data = unpackb(obj.data,
use_list=False,
encoding='utf-8')
return pbag(decode(item) for item in unpacked_data)
module_name, class_name, *data = unpackb(obj.data,
use_list=False,
encoding='utf-8')
cls = getattr(sys.modules[module_name],
class_name)
return cls(*(decode(item) for item in data))
if isinstance(obj, tuple):
return pvector(decode(item) for item in obj)
if isinstance(obj, dict):
new_dict = dict()
for key in obj.keys():
new_dict[decode(key)] = decode(obj[key])
return pmap(new_dict)
return obj
示例10: test_call_with_multi_args
def test_call_with_multi_args(self):
addr = get_random_ipc_socket()
with utils.TestingServer(methods=self.get_methods(), addresses=addr):
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(addr)
socket.send(msgpack.packb({
'm': 'method3',
'k': {'name': 'WORLD'},
}, encoding='utf-8'))
response = msgpack.unpackb(socket.recv(), encoding='utf-8')
assert response == {'r': 'Hello, WORLD!'}
socket.send(msgpack.packb({
'm': 'method3',
'k': {'greeting': 'HOWDY'},
}, encoding='utf-8'))
response = msgpack.unpackb(socket.recv(), encoding='utf-8')
assert response == {'r': 'HOWDY, world!'}
socket.send(msgpack.packb({
'm': 'method3',
'k': {'greeting': 'HOWDY', 'name': 'MAN'},
}, encoding='utf-8'))
response = msgpack.unpackb(socket.recv(), encoding='utf-8')
assert response == {'r': 'HOWDY, MAN!'}
socket.send(msgpack.packb({
'm': 'method3',
'a': ('hey', 'man'),
}, encoding='utf-8'))
response = msgpack.unpackb(socket.recv(), encoding='utf-8')
assert response == {'r': 'hey, man!'}
示例11: test_odict
def test_odict():
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
od = OrderedDict(seq)
assert unpackb(packb(od), use_list=1) == dict(seq)
def pair_hook(seq):
return list(seq)
assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
示例12: _unserializer
def _unserializer(code, data):
if code == 0:
return uuid.UUID(hex=six.text_type(data, encoding='ascii'))
if code == 1:
return _deserialize_datetime(data)
if code == 2:
value = msgpack.unpackb(data)
if not _PY26:
return itertools.count(value[0], value[1])
else:
return itertools.count(value[0])
if netaddr and code == 3:
value = msgpack.unpackb(data)
return netaddr.IPAddress(value)
if code in (4, 5):
value = loads(data)
if code == 4:
return set(value)
else:
return frozenset(value)
if code == 6:
dt = _deserialize_datetime(data)
return xmlrpclib.DateTime(dt.timetuple())
if code == 7:
return _deserialize_date(data)
return msgpack.ExtType(code, data)
示例13: flash_binary
def flash_binary(fdesc, binary, base_address, device_class, destinations,
page_size=2048):
"""
Writes a full binary to the flash using the given file descriptor.
It also takes the binary image, the base address and the device class as
parameters.
"""
print("Erasing pages...")
pbar = progressbar.ProgressBar(maxval=len(binary)).start()
# First erase all pages
for offset in range(0, len(binary), page_size):
erase_command = commands.encode_erase_flash_page(base_address + offset,
device_class)
res = utils.write_command_retry(fdesc, erase_command, destinations)
failed_boards = [str(id) for id, success in res.items()
if not msgpack.unpackb(success)]
if failed_boards:
msg = ", ".join(failed_boards)
msg = "Boards {} failed during page erase, aborting...".format(msg)
logging.critical(msg)
sys.exit(2)
pbar.update(offset)
pbar.finish()
print("Writing pages...")
pbar = progressbar.ProgressBar(maxval=len(binary)).start()
# Then write all pages in chunks
for offset, chunk in enumerate(page.slice_into_pages(binary, page_size)):
offset *= page_size
command = commands.encode_write_flash(chunk,
base_address + offset,
device_class)
res = utils.write_command_retry(fdesc, command, destinations)
failed_boards = [str(id) for id, success in res.items()
if not msgpack.unpackb(success)]
if failed_boards:
msg = ", ".join(failed_boards)
msg = "Boards {} failed during page write, aborting...".format(msg)
logging.critical(msg)
sys.exit(2)
pbar.update(offset)
pbar.finish()
# Finally update application CRC and size in config
config = dict()
config['application_size'] = len(binary)
config['application_crc'] = crc32(binary)
utils.config_update_and_save(fdesc, config, destinations)
示例14: send_request_message
def send_request_message(self, request):
# for deep copy of Request object
obj = msgpack.unpackb(msgpack.packb(request.packed_object()))
rcv_request = Request.create_from_packed(obj)
response = self.dispatcher.dispatch_request(rcv_request)
# for deep copy of Response object
obj = msgpack.unpackb(msgpack.packb(response.packed_object()))
return Response.create_from_packed(obj)
示例15: decode
def decode(obj, silent=False):
"""Decode msgpack binary data to python object.
"""
if obj is None:
return
if silent:
return msgpack.unpackb(obj, object_hook=msgpack_decode_silent, encoding='utf8')
return msgpack.unpackb(obj, object_hook=msgpack_decode, encoding='utf8')