Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. from sans_io import *
  2.  
  3. @protocol
  4. async def bytes_protocol():
  5. try:
  6. while True:
  7. sz: bytes = await read_exactly(4)
  8. length = int.from_bytes(sz, 'big')
  9.  
  10. hdr = await read_exactly(length*4)
  11. data = array.array.frombytes('L', hdr)
  12. if sys.byteorder != "big":
  13. data.byteswap()
  14.  
  15. text, *buffers = [(await read_exactly(l)) for l in data]
  16.  
  17. text = text.encode("utf-8")
  18. text = json.loads(text)
  19.  
  20. await emit(Message(text, buffers))
  21. except ConnectionResetError:
  22. pass
  23.  
  24. x = bytes_protocol()
  25. for event in x.feed(b"stuff..."):
  26. _do_stuff_with(event)
  27. for event in x.feed(b"stuff..."):
  28. _do_stuff_with(event)
  29. #...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement