views:

219

answers:

2

I have a project which is essentially a game server where users connect and send text commands via telnet.

The code is in C and really old and unmodular and has several bugs and missing features. The main function alone is half the code.

I came to the conclusion that rewriting it in Python, with Twisted, could actually result in faster completement, besides other benefits.

So, here is the questions: What packages and modules I should use? I see a "telnet" module inside "protocols" package. I also see "cronch" package with "ssh" and another "telnet" module.

I'm a complete novice regarding Python.

A: 

It sounds like you've got two separate tasks here:

  • Port the code from C to Python.
  • Rewrite the whole program to use Twisted.

Since you're new to Python, I would be inclined to do the first one first, before trying to make the program structure work in Twisted. If the program is old, there isn't likely to be any performance problems running it on modern hardware.

Converting the C code to Python first will give you the familiarity with Python you need to start on the port to Twisted.

Greg Hewgill
thnx for the answer.
ypercube
+2  A: 

Greg's suggestion to try to become familiar with Python before trying to take on Twisted is perhaps a reasonable one. Limiting the possible sources of your confusion may help you avoid some very frustrating cases.

On the other hand, I know a lot of people who take on a Twisted-based project as a first Python learning experience, and succeed. So it's possible. And you'll have to do something first. There's no guarantee that what you pick instead of Twisted will be easier. :)

As far as the specifics of telnet go, you want to use twisted.conch.telnet, not twisted.protocols.telnet. The former is newer, better tested, more featureful, and has several examples (although unfortunately not a lot of documentation beyond that).

The main telnet example shows you the most useful event handlers you can define if you use twisted.conch.telnet. Unfortunately it doesn't really explain what they do, nor does it give a demonstration of how you might negotiation options. If you're already familiar with the telnet protocol itself (primarily the option negotiation part of it), this should make sense to you. If you're thinking of "telnet" as just a way to pass human readable/writeable bytes between two computers, then you probably haven't run across option negotiation before and it might not make much sense. You can either ignore it, or check out the telnet RFC to learn more (it's a little dense, though).

For performing option negotiations yourself, you can at least take a look at the API documentation for the self.transport object you see in the above linked example (this isn't a regular TCP transport as you'll find used throughout much of Twisted, but a special telnet-derived transport which sits on top of TCP, so it has a few extra features).

If you just want to pass bytes around, though, then you can focus on the dataReceived method and on self.transport.write. The former will be called when you receive bytes from your peer; the latter you can call to send bytes to your peer.

Jean-Paul Calderone
thnx. I think I'll try this road.
ypercube