[Twisted-Python] NetstringReceiver

Albert Brandl albert.brandl at weiermayer.com
Fri Mar 26 06:25:48 EDT 2010

On Thu, Mar 25, 2010 at 09:22:03AM +1100, Andrew Bennetts wrote:
> No; I think once loseConnection has been called dataReceived won't be
> called again, so there's no point checking for brokenPeer in
> dataReceived.

There is a unittest that assumes that dataReceives still works after
sending in garbage:

   def test_illegal(self):
        Assert that illegal strings cause the transport to be closed.
        for s in self.illegalStrings:
            r = self.getProtocol()
            for c in s:

self.illegalStrings is defined as

    illegalStrings = [
        '9999999999999999999999', 'abc', '4:abcde',

In this implementation, the dataReceived method has to be functional
even after the NetstringReceiver has detected that the received
data are garbage. And there is no direct information that the
received string was no valid netstring - you have to check for

That's why I thought it might be a good idea to raise an appropriate 
exception if the receiver is no longer functional. I use a generator- 
based approach to parsing the netstring (for details see 
and the generator raises a StopIteration exception.

But I'd prefer to raise an exception that explains what happened at a 
higher level - that's why I asked if there was some predefined exception 
that would match this kind of problem.

> There are some existing tests for NetstringReceiver in
> twisted/test/test_protocols.py.

Thanks, this was very helpful.

DI Albert Brandl

Weiermayer Solutions GmbH

4813 Altmünster | Abteistraße 12 | Austria
Fon: +43(0)720 70 30 14 | Fax: +43 (0) 7612 88081

More information about the Twisted-Python mailing list