[Twisted-Python] How to write a simpletelnet client?

Michał Tyde ajchos at wp.pl
Tue Sep 27 09:10:54 EDT 2005

Sorry that i answer so late (busssy week).
> I could even switch off the echo (your next thread).

Switching off ECHO i realized by sending a commands:

unset PS1 (switching off prompt)
stty -echo (swiching off echo)

> What I don't understand is how to 'inject' some commands and collect
> their result.
I don't understand how You want to "inject" commands. Now I used a
defers to handling a commands (sending and getting answers).
dataReceived is responsible for collecting what is comming to Your
client as answer. 
> What you tried in your "command" function in __main__ can't work,
> because the protocol has no transport.
This is the factory now i used to connect (maybe thiss will help You):

class MyFactory( ClientFactory ):
    protocol = TelnetProtocol
    '''all printouts from this class will have F at the beggining - form
Factory and they are green - <g>'''    
    def __init__( self ):
        print "<g>F __init__"
        self.nextName = None
        self.t0 = time.time()
    def buildProtocol( self, addr ):
        print "<g>F buildProtocol"
        p = self.protocol( self.nextName )
        p.factory = self
        self.protos[self.nextName] = p
        self.nextName = None
        return p
    def getClient( self, name ):
        print "<g>F getClient"
        t0 = time.time()
        while True:
                return self.protos[name]
            except KeyError:
                reactor.run( 0.1 )
        print name
        print self.nextName
        while name not in self.nextName:
            reactor.iterate( 0.1 )
            if time.time() - self.t0 > CLIENT_CONNECTION_TIMEOUT:
                raise TimeoutError, "Timeout on connection"
    def addActive( self, proto ):
        print "<g>F addActive"
        self.engine._storeClient( proto )

More information about the Twisted-Python mailing list