Skip to content

Instantly share code, notes, and snippets.

@HelloZeroNet
Created May 25, 2015 16:00
Show Gist options
  • Save HelloZeroNet/2e2186a48a5e172c07ea to your computer and use it in GitHub Desktop.
Save HelloZeroNet/2e2186a48a5e172c07ea to your computer and use it in GitHub Desktop.
import msgpack
import cStringIO as StringIO
print "Msgpack Version", msgpack.version
print "Testing simple data"
data = "A"*10
for i in range(1,20):
print "- %skb..." % len(data),
print "Packing",
unpacked = {"hello": data}
packed = StringIO.StringIO(msgpack.packb(unpacked))
print "Unpacking",
unpacker = msgpack.Unpacker()
while 1:
buff = packed.read(1024*16)
if not buff: break
print ".",
unpacker.feed(buff)
error = False
for found in unpacker:
if found == unpacked: print "OK"
else:
print found.keys(), unpacked.keys()
error = True
if error: break
data += data
print "Testing complex data"
data = "A"*10
for i in range(1,20):
print "- %skb..." % len(data),
print "Packing",
unpacked = {
"version": [1,2,3],
"protocol": "v2",
"peer_id": "RANDOMPEERiD",
"fileserver_port": 12321,
"port_opened": True,
"rev": 123,
"data": data
}
packed = StringIO.StringIO(msgpack.packb(unpacked))
print "Unpacking",
unpacker = msgpack.Unpacker()
while 1:
buff = packed.read(1024*16)
if not buff: break
print ".",
unpacker.feed(buff)
error = False
for found in unpacker:
if found == unpacked: print "OK"
else:
print found.keys(), unpacked.keys()
error = True
if error: break
data += data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment