[Twisted-Python] struggling with twisted telnet

Hi
I'm pretty new to python which probably isn't helping, but I'm kind of stuck with twisted telnet. I'm trying to build a couple of 'emulators' for some simple router/switches just so that you can log on and run some 'standard' commands and get a 'standard' output for use elsewhere. I've managed to get an SSH version working with the massively helpful section in the O'Reilly book but I cannot get the telnet version working. the script 'runs' with no errors, but it doesn't stay running and bind to port 23 giving me my telnet server. I enclose a zip with the ssh and the telnet versions, if anyone can give me any pointers it would be massively appreciated (I haven't even attempted to add in the 'login' which is in the SSH version... so any help on that would be great too :) )
Thanks
Andrew

On Wed, 8 Aug 2007 13:25:59 +0100, Andrew Roden andrew.roden@gmail.com wrote:
Hi
I'm pretty new to python which probably isn't helping, but I'm kind of stuck with twisted telnet. I'm trying to build a couple of 'emulators' for some simple router/switches just so that you can log on and run some 'standard' commands and get a 'standard' output for use elsewhere. I've managed to get an SSH version working with the massively helpful section in the O'Reilly book but I cannot get the telnet version working. the script 'runs' with no errors, but it doesn't stay running and bind to port 23 giving me my telnet server. I enclose a zip with the ssh and the telnet versions, if anyone can give me any pointers it would be massively appreciated (I haven't even attempted to add in the 'login' which is in the SSH version... so any help on that would be great too :) )
Are you using twistd to run it?
$ twistd -noy telnet_server.py Removing stale pidfile twistd.pid 2007-08-08 08:31:05-0400 [-] Log opened. 2007-08-08 08:31:05-0400 [-] twistd 2.5.0+r20987 (/usr/bin/python 2.4.3) starting up 2007-08-08 08:31:05-0400 [-] reactor class: <class 'twisted.internet.selectreactor.SelectReactor'> 2007-08-08 08:31:05-0400 [-] twisted.internet.protocol.ServerFactory starting on 23 2007-08-08 08:31:05-0400 [-] Starting factory <twisted.internet.protocol.ServerFactory instance at 0xb77f5fac> ...
After I connect to the port, it reports several tracebacks and drops the connection.
Jean-Paul

The answer is no I wasn't running it with twistd as I didn't need to with the ssh one... having now run it with twistd I can see the exceptions it throws but I have absolutely no clue how to go about rectifying them :(
From what I can make out the main issue is with no terminal size being set
which from the demo_insults.tac looks like it's set in my class that deals with connections associating it with insults.TerminalProtocol (eg. class DemoProtocol(insults.TerminalProtocol): ) however, I am dealing with my connections in recvline as I don't want it to react on a per character basis but rather a per line basis...
*confused of england*
Andrew
On 8/8/07, Jean-Paul Calderone < exarkun@divmod.com> wrote:
On Wed, 8 Aug 2007 13:25:59 +0100, Andrew Roden < andrew.roden@gmail.com> wrote:
Hi
I'm pretty new to python which probably isn't helping, but I'm kind of
stuck
with twisted telnet. I'm trying to build a couple of 'emulators' for some
simple router/switches just so that you can log on and run some
'standard'
commands and get a 'standard' output for use elsewhere. I've managed to
get
an SSH version working with the massively helpful section in the O'Reilly
book but I cannot get the telnet version working. the script 'runs' with
no
errors, but it doesn't stay running and bind to port 23 giving me my
telnet
server. I enclose a zip with the ssh and the telnet versions, if anyone
can
give me any pointers it would be massively appreciated (I haven't even attempted to add in the 'login' which is in the SSH version... so any
help
on that would be great too :) )
Are you using twistd to run it?
$ twistd -noy telnet_server.py Removing stale pidfile twistd.pid 2007-08-08 08:31:05-0400 [-] Log opened. 2007-08-08 08:31:05-0400 [-] twistd 2.5.0+r20987 (/usr/bin/python
2.4.3) starting up 2007-08-08 08:31:05-0400 [-] reactor class: <class ' twisted.internet.selectreactor.SelectReactor'> 2007-08-08 08:31:05-0400 [-] twisted.internet.protocol.ServerFactorystarting on 23 2007-08-08 08:31:05-0400 [-] Starting factory < twisted.internet.protocol.ServerFactory instance at 0xb77f5fac> ...
After I connect to the port, it reports several tracebacks and drops the connection.
Jean-Paul
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On Wed, 8 Aug 2007 14:27:36 +0100, Andrew Roden andrew.roden@gmail.com wrote:
The answer is no I wasn't running it with twistd as I didn't need to with the ssh one... having now run it with twistd I can see the exceptions it throws but I have absolutely no clue how to go about rectifying them :(
From what I can make out the main issue is with no terminal size being set
which from the demo_insults.tac looks like it's set in my class that deals with connections associating it with insults.TerminalProtocol (eg. class DemoProtocol(insults.TerminalProtocol): ) however, I am dealing with my connections in recvline as I don't want it to react on a per character basis but rather a per line basis...
*confused of england*
Andrew
The first traceback I see is this one:
2007-08-08 09:46:39-0400 [twisted.internet.protocol.ServerFactory] Unhandled Error Traceback (most recent call last): File "/home/exarkun/Projects/Twisted/trunk/twisted/python/log.py", line 36, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 59, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 37, in callWithContext return func(*args,**kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/selectreactor.py", line 139, in _doReadOrWrite why = getattr(selectable, method)() --- <exception caught here> --- File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/tcp.py", line 813, in doRead protocol.makeConnection(transport) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/protocol.py", line 333, in makeConnection self.connectionMade() File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 732, in connectionMade self.protocol.makeConnection(self) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/protocol.py", line 333, in makeConnection self.connectionMade() File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 795, in connectionMade self.protocol.makeConnection(self) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/protocol.py", line 333, in makeConnection self.connectionMade() File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/insults/insults.py", line 416, in connectionMade self.terminalProtocol = self.protocolFactory(*self.protocolArgs, **self.protocolKwArgs) exceptions.TypeError: __init__() takes exactly 2 arguments (1 given)
Python is really helpful here, omitting the class name which has the bad __init__, but I guess you can track that down with a little effort (and there are probably only a small number of candidates).
The next one I see is this:
2007-08-08 09:46:39-0400 [TelnetTransport,0,127.0.0.1] Unhandled Error Traceback (most recent call last): File "/home/exarkun/Projects/Twisted/trunk/twisted/python/log.py", line 51, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/log.py", line 36, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 59, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 37, in callWithContext return func(*args,**kw) --- <exception caught here> --- File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/selectreactor.py", line 139, in _doReadOrWrite why = getattr(selectable, method)() File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/tcp.py", line 362, in doRead return self.protocol.dataReceived(data) File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 483, in dataReceived self.negotiate(commands) File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 526, in negotiate cmdFunc(bytes) File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 833, in telnet_NAWS self.protocol.terminalProtocol.terminalSize(width, height) exceptions.AttributeError: 'NoneType' object has no attribute 'terminalSize'
This may just be a consequence of the first one, so I'd try resolving the first one before looking more at this.
Jean-Paul

ok... thanks for the help so far :)
I've managed to get it 'working' by hashing out the __init__ under the Telnet Protocol class
class TelnetProtocol(recvline.HistoricRecvLine): # def __init__(self, user): # self.user = user
That will now allow me to connect to the 'server' but it behaves in a different way to the SSH server in that if I type in one of my commands... to see the output and return to the prompt, I have to hit enter twice which then shows me 2 prompts :(
oh for a nice simple example like the one for SSH in the O'Reilly book :(
Andrew
On 8/8/07, Jean-Paul Calderone exarkun@divmod.com wrote:
On Wed, 8 Aug 2007 14:27:36 +0100, Andrew Roden andrew.roden@gmail.com wrote:
The answer is no I wasn't running it with twistd as I didn't need to with the ssh one... having now run it with twistd I can see the exceptions it throws but I have absolutely no clue how to go about rectifying them :(
From what I can make out the main issue is with no terminal size being
set
which from the demo_insults.tac looks like it's set in my class that
deals
with connections associating it with insults.TerminalProtocol (eg. class DemoProtocol(insults.TerminalProtocol): ) however, I am dealing with my connections in recvline as I don't want it to react on a per character
basis
but rather a per line basis...
*confused of england*
Andrew
The first traceback I see is this one:
2007-08-08 09:46:39-0400 [twisted.internet.protocol.ServerFactory] Unhandled Error Traceback (most recent call last): File "/home/exarkun/Projects/Twisted/trunk/twisted/python/log.py", line 36, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 59, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 37, in callWithContext return func(*args,**kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/selectreactor.py", line 139, in _doReadOrWrite why = getattr(selectable, method)() --- <exception caught here> --- File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/tcp.py", line 813, in doRead protocol.makeConnection(transport) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/protocol.py", line 333, in makeConnection self.connectionMade() File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 732, in connectionMade self.protocol.makeConnection(self) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/protocol.py", line 333, in makeConnection self.connectionMade() File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 795, in connectionMade self.protocol.makeConnection(self) File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/protocol.py", line 333, in makeConnection self.connectionMade() File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/insults/insults.py", line 416, in connectionMade self.terminalProtocol = self.protocolFactory (* self.protocolArgs, **self.protocolKwArgs) exceptions.TypeError: __init__() takes exactly 2 arguments (1 given)
Python is really helpful here, omitting the class name which has the bad __init__, but I guess you can track that down with a little effort (and there are probably only a small number of candidates).
The next one I see is this:
2007-08-08 09:46:39-0400 [TelnetTransport,0,127.0.0.1] Unhandled Error Traceback (most recent call last): File "/home/exarkun/Projects/Twisted/trunk/twisted/python/log.py", line 51, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/log.py", line 36, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 59, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "/home/exarkun/Projects/Twisted/trunk/twisted/python/context.py", line 37, in callWithContext return func(*args,**kw) --- <exception caught here> --- File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/selectreactor.py", line 139, in _doReadOrWrite why = getattr(selectable, method)() File "/home/exarkun/Projects/Twisted/trunk/twisted/internet/tcp.py", line 362, in doRead return self.protocol.dataReceived(data) File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 483, in dataReceived self.negotiate(commands) File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 526, in negotiate cmdFunc(bytes) File "/home/exarkun/Projects/Twisted/trunk/twisted/conch/telnet.py", line 833, in telnet_NAWS self.protocol.terminalProtocol.terminalSize(width, height) exceptions.AttributeError: 'NoneType' object has no attribute 'terminalSize'
This may just be a consequence of the first one, so I'd try resolving the first one before looking more at this.
Jean-Paul
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Andrew Roden
-
Jean-Paul Calderone