def receive_loop(self):
"""
This is the receive loop for Banyan messages.
This method may be overwritten to meet the needs
of the application before handling received messages.
"""
while True:
try:
data = self.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]))
# if no messages are available, zmq throws this exception
except zmq.error.Again:
try:
time.sleep(self.loop_time)
except KeyboardInterrupt:
self.clean_up()
raise KeyboardInterrupt
python类unpackb()的实例源码
def msgpack_appendable_unpack(path):
# if not list?
# return msgpack.unpackb(f.read())
with open(path, 'rb') as f:
packer = msgpack.Packer()
unpacker = msgpack.Unpacker(f, encoding='utf-8')
length = unpacker.read_array_header()
header_lenght = len(packer.pack_array_header(length))
unpacker.read_bytes(MAX_MSGPACK_ARRAY_HEADER_LEN - header_lenght)
f.seek(MAX_MSGPACK_ARRAY_HEADER_LEN)
return [unpacker.unpack() for _ in range(length)]
def datagram_received(self, data, sender):
message = msgpack.unpackb(data, encoding='utf-8')
self.orchestrator.data_received_peer(sender, message)
def data_received(self, data):
message = msgpack.unpackb(data, encoding='utf-8')
self.orchestrator.data_received_client(self, message)
def msgpack_decode_hashable(x):
return messagepack.unpackb(x, use_list=False)
# encode should support: arbitrary python object => bytes
# decode should support: python Buffer => object
# default for all encode/decode is 'str', except None maps to '' for encode
# for node/edge types/values as well as property keys, you should strive to make sure the encoder is deterministic
# if you plan to use complex values - if dicts are involved, msgpack is not so much.
def msgpack(hashable=False):
return Serializer(encode=msgpack_encode_hashable, decode=msgpack_decode_hashable) if hashable else Serializer(encode=messagepack.packb, decode=messagepack.unpackb)
def __msgpackUnpack(self, data):
"""??msgpack??"""
return unpackb(data)
#----------------------------------------------------------------------
def decode(self, data):
return msgpack.unpackb(data)
def unpack(x):
"""Decode ``x`` from msgpack into Python object."""
return msgpack.unpackb(bytes(x['data']), encoding='utf8')
def unpack(stream):
"""Pack python data to a binary stream.
:param stream: bytes.
:rtype: The unpacked python object.
"""
return msgpack.unpackb(stream, list_hook=decode, encoding='utf-8')
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')
def blob_to_dict(self, blob):
try:
return msgpack.unpackb(blob, encoding='utf-8', ext_hook=self.ext_hook)
except (TypeError, msgpack.UnpackValueError, msgpack.ExtraData) as e:
raise InvalidMessage(*e.args)
def process_results(self):
print "[ +D+ ] - Begin processing minimization results"
bs = beanstalkc.Connection(host='127.0.0.1', port=11300)
bs.use('min-results')
if self.get_job():
seed = msgpack.unpackb(zlib.decompress(self.job.body))
name = seed['seed_name']
data = seed['data']
filename = os.path.join(self.min_dir, name)
with open(filename, 'wb') as f:
f.write(data)
def unpack(job):
data = msgpack.unpackb(zlib.decompress(job))
return data
def loads(content):
return msgpack.unpackb(content, object_hook=all.decode, encoding='utf-8')
def __msgpack_unpack(self, data):
return msgpack.unpackb(data)
def decode(self, data):
return msgpack.unpackb(data)
def msf_rpc_get_temp_auth_token():
"""Get a temporary authentication token from the Metasploit RPC Server"""
global msfRpcToken
payload = msgpack.packb(["auth.login", msfRpcUser, msfRpcPass])
response = msf_rpc_request(payload)
if response is not None:
if DEBUG:
print debug + "MSF RPC auth.login response:\n\tHTTP Status Code: %s" % response.status_code
if response.headers['Content-Type'] == "binary/message-pack":
msf_rpc_message = msgpack.unpackb(response.content, use_list=False)
print "\t" + debug + "MSF RPC Server Response: %s" % msf_rpc_message
if 'error' in msf_rpc_message.keys():
print debug + "MSF RPC Error: %s" % msf_rpc_message['error_message']
else:
print "\t" + debug + "HTTP Server Response: %s" % response.content
if response.status_code == 200:
result = msgpack.unpackb(response.content, use_list=False)
if 'error' in result.keys():
print warn + "MSF RPC Error: %s" % result['error_message']
print warn + "Quitting"
sys.exit()
elif 'token' in result.keys():
msfRpcToken = result['token']
def msf_rpc_get_session_list():
"""Get a list of Meterpreter sessions"""
payload = msgpack.packb(["session.list", msfRpcToken])
response = msf_rpc_request(payload)
if response is not None:
result = msgpack.unpackb(response.content, use_list=False)
if response.status_code == 200:
return result
else:
return None
else:
return None
def __missing__(self, key):
row = self.table.row(key)
if not row:
super(DomainCache, self).__missing__(key)
raise KeyError
value = {}
for k, v in row.iteritems():
cf, _, col = k.partition(':')
value[col] = unpackb(v)
self.__setitem__(key, value)
return value