Skip to content

Instantly share code, notes, and snippets.

@lukasjuhrich
Created November 18, 2017 23:23
Show Gist options
  • Save lukasjuhrich/fa450a733aeb7d108f97462ebc2f6763 to your computer and use it in GitHub Desktop.
Save lukasjuhrich/fa450a733aeb7d108f97462ebc2f6763 to your computer and use it in GitHub Desktop.
def get_next_field(buf):
"""Parse the buffer to return the next field
Each field is of the format `<field length>\x00<field>`
"""
if buf[:2] == b'\0\0':
# Field of length zero
return b'', buf[2:]
raw_length, rest = buf.split(b'\0', maxsplit=1)
length = int.from_bytes(raw_length, byteorder='little')
return rest[:length], rest[length:]
def iter_fields(buf):
rest = buf
while rest:
field, rest = get_next_field(rest)
yield field
def parse_uwsgi_request(request):
# The first 4 bytes are the uwsgi header
parsed = list(iter_fields(request[4:]))
return dict(zip(parsed[::2], parsed[1::2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment