Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cStringIO import StringIO
- import struct
- HEADER_FMT = '!L'
- HEADER_SIZE = 4
- class NSBuffer(object):
- def __init__(self):
- self.buf = StringIO()
- def write(self, s):
- self.buf.write(s)
- def extract(self):
- v = self.buf.getvalue()
- res = []
- while len(v) >= HEADER_SIZE:
- l, = struct.unpack(HEADER_FMT, v[:HEADER_SIZE])
- truncated = v[HEADER_SIZE:]
- if len(truncated) >= l:
- res.append(truncated[:l])
- v = truncated[l:]
- else:
- break
- self.buf = StringIO()
- self.buf.write(v)
- return res
- def ns_encode(s):
- return struct.pack(HEADER_FMT, len(s)) + s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement