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

Glyph glyph at twistedmatrix.com
Sat Feb 16 17:53:41 EST 2013

On Feb 16, 2013, at 1:09 PM, Adam Heller <sk82712 at gmail.com> wrote:

> Guys,
> I'll try to be as exact as I can be with my question, but it may end up being pretty broad. (I'm also relatively new to python, please go easy on my code although suggestions are of course welcome.)

The main suggestion is "you can't parse TCP like this" :).  You'll have this problem: <http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#Whyisprotocol.dataReceivedcalledwithonlypartofthedataIcalledtransport.writewith>

> The issue I'm running into:
> When a user connects to the TCP server, they are displayed a welcome menu of things they can do. I'm simply catching the 'data' in an if statement to determine which menu item was picked.  The problem is that some of the menu items require further input.  I've had to use a series of bools to determine if a certain menu option was previously picked that will then directly send the data into the function / class.  Is there an easier way to go about this? My current method seems a bit backwards and over complicated.  I'm thinking I missed something in the documentation.

> Anyway, here's the code: https://infotomb.com/q4293 // Pass: twisted

You should really attach the code itself to the email, so that future readers of the archive have some idea what you're talking about.  Pastebins in general are not great, since the code will expire before your archived message does, and pastebins with passwords (!?) are even worse.

> (it was part of a challenge, so to speak, don't take too much out of context)
> You can see I had trouble with next() and dh().  I'm now trying to move this TCP 'framework' over to another project (the above was already submitted), and I'm still running into the same issues.

You should try separating out your concerns a bit.  The protocol should just parse the bytes into something meaningful, then call methods on some other, higher-level object that isn't just manipulating strings.

The "other, higher-level object" in your case might be a state machine of some kind, or you might just have an object representing the mode that you're currently in and switch that out for different modes.

This isn't an issue with Twisted though; Twisted just calls some methods on your objects in order to tell you what happened; what happens next is up to you.  So it's a general program architecture issue.

This is very general advice, obviously, because I'm not sure what you are specifically asking, nor what your program is really intended to do.  What kind of client is talking to this system?  Did you invent the protocol yourself, or did it come from somewhere else?  What is 'dh()' even trying to accomplish?  Etc, etc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20130216/ff09b772/attachment.htm 

More information about the Twisted-Python mailing list