Minimal example of using twisted.manhole (Since it took me so long to get it working...)

Okay, so you want to take a Twisted server and provide a way to execute arbitrary Python code within it while it is running. A few moments of googling gives you the information you need, namely that you want to use the twisted.manhole package. A few minutes more and you come across what seems to be a complete bit of sample code. Only problem is, it leaves out one piece of critical information, namely what type of object the mysterious "service" is supposed to be. Take heart, gentle coder, now you have an example that is, in fact, complete:
from twisted.internet import reactor
from twisted.manhole import telnet
def createShellServer( ):
print 'Creating shell server instance'
factory = telnet.ShellFactory()
port = reactor.listenTCP( 2000, factory)
factory.namespace['x'] = 'hello world'
factory.username = 'mike'
factory.password = 'which1ta'
print 'Listening on port 2000'
return port

if __name__ == "__main__":
reactor.callWhenRunning( createShellServer )
reactor.run()

Turns out "service" is entirely superfluous, it's just assigned to the variable "service" in the namespace that's created. It's likely good practice to provide a pointer to something service-like (in the general Twisted sense of the word), but it's not actually a functional requirement.

The script above creates a little telnet server on port 2000 with a Python prompt available to you once you log on. You can add whatever you want to the factory.namespace dictionary to make it available to the user. You'd obviously want to use a different username and password, of course ;) .

So far I've been a little underwhelmed with the provided interpreter client, but then I've had to use my sister's machine to do the connecting, and it's telnet client seems a little... confused. Seems there's no per-character return echo? Doesn't seem like that could really be the case, hoping it's a Windows-telnet problem that will go away when I get a chance to install telnet on my workstation.

Comments

  1. gjkjg

    gjkjg on 09/26/2009 12:47 a.m. #

    Really? I see nothing here. "Take heart, gentle coder, now you have an example that is, in fact, complete:" is the last line before the TRACKBACKS section that I see.

  2. Mike Fletcher

    Mike Fletcher on 09/26/2009 11:38 a.m. #

    Yeah, lost in the blog migration (all "extended content" sections were lost). The original source is still available in the way-back machine:

    http://web.archive.org/web/20050206102203/blog.vrplumber.com/349#more

    HTH,
    Mike

  3. Victor

    Victor on 07/23/2012 10:48 a.m. #

    Nice example. The only thing is that
    if _name_ == "_main_":
    should probably be
    if __name__ == "__main__":

Comments are closed.

Pingbacks

Pingbacks are closed.