Quick hacks: Python netstring reader

Published · 2min

I’m writing a proof-of-concept server for a less jokey version of the SRGP. The protocol is more or less SRGP, but it doesn’t use MIME-style headers each request is made up of a series of netstrings. The header and body sections are terminated with empty netstrings, i.e., 0:,.

The proof-of-concept server is being written in Python. Here’s the netstring reader generator function, in case it might be of use to somebody else out there:

class MalformedNetstringError:

def netstring_reader(f):
    while True:
        n = ""
        while True:
            c =
            if c == '':
            if c == ':':
            if len(n) > 10:
                raise MalformedNetstringError
            if c == '0' and n == '':
                # We can't allow leading zeros.
                if != ':':
                    raise MalformedNetstringError
                n = c
            n += c
        n = int(n, 10)
        payload =
        if len(payload) < n:
        if != ',':
            raise MalformedNetstringError
        yield payload

netstring_reader() takes a file object and returns a generator that returns each netstring extracted from the file object. If the stream is malformed, a MalformedNetstringError is raised.

Currently the diagnostics are far from as good as they ought to be. It should state whether it’s a malformed length or a missing terminating comma, and the offset into the stream where the error occurred. There’s a few places where it can be made more robust, but expect that in later releases.

Here’s an example of it in use.

f = open("sample.dat", "r")
    for i in netstring_reader(f):
        print i