[Twisted-Python] Building a TCP server, having issues

Phil Mayers p.mayers at imperial.ac.uk
Mon Feb 18 03:55:40 EST 2013

On 02/17/2013 01:09 AM, Adam Heller wrote:
> Glyph,
> I will definately look into that.  As far as the server spec, it's over
> TOR (This part of the 'competition' has since closed, but feel free to
> catch up: http://uncovering-cicada.wikia.com/wiki/Uncovering_Cicada_Wiki
> and
> http://mentalfloss.com/article/31932/chasing-cicada-exploring-darkest-corridors-internet
> (from last year's 3301).  I'm simply trying to do the python thing and
> move classes around instead of trying to reinvent the wheel [dh() is a
> rudimentary Diffie Helman key exchange] : "
> In the programming language of your choice build a TCP server
> that implements the protocol below. The server code must be written by
> you and you alone, although you are free to use any modules or libraries
> publicly available for the selected programming language. Once you have
> done this, make it accessible as a Tor hidden service. Then provide us
> with the onion address and port via a GPG-encrypted email to this
> address. You have until 0:00 UTC on 3 Feb, 2013. Any emails received
> after that time will be ignored. Good luck. 3301
> ==================================================================== 1.
> INTRODUCTION The TCP server MUST listen on an arbitrary port, and send
> and receive plain text with lines separated by <CRLF>

Glyph has already mentioned the buffering and state machine options, but 
since it's a line-based protocol, you chould also look at 


...which does the buffering for you and calls a method with full 
"lines". You may want to look at the source for that class' dataReceived 
method, for an example of how it does the buffering Glyph talks about.

There are other examples of doing state machine dispatch in the Twisted 
sources, but it's a pretty simple technique - set a "state" variable, 
and a big if/then clause (or, for performance, a dict of state->handler)

More information about the Twisted-Python mailing list