[Twisted-Python] Re : Re : Re : Telnet server using Twisted and AuthenticatingTelnetProtocol

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Sep 2 10:27:40 EDT 2009

On 11:44 am, filoufake-python at yahoo.fr wrote:
>>It's hard to say what's going wrong without being able to see all of 
>>code.  A short, self contained, correct example (<http://sscce.org/>)
>>would help a lot.
>Hereafter is the simplest code that generates the problem.
>If you run it, you will see that after entering the password nothing
>happened. The "telnet_Command" method of MyTelnet is never called.
>I think the problem is what requestAvatar returns.
>Thanks again for your support
>import sys
>from zope.interface import implements
>from twisted.internet import protocol
>from twisted.python import log
>from twisted.cred import error
>from twisted.cred import portal
>from twisted.cred import checkers
>from twisted.cred import credentials
>from twisted.conch.telnet import AuthenticatingTelnetProtocol
>from twisted.conch.telnet import StatefulTelnetProtocol
>from twisted.conch.telnet import ITelnetProtocol
>from twisted.conch.telnet import TelnetTransport
>class Realm:
>  implements(portal.IRealm)
>  def requestAvatar(self, avatarId, mind, *interfaces):
>    if ITelnetProtocol in interfaces:
>      av = MyTelnet()
>      return ITelnetProtocol, av, lambda:None
>    raise NotImplementedError("Not supported by this realm")
>class MyTelnet(StatefulTelnetProtocol):
>  def telnet_Command(self, line):
>    print "line received via telnet", line
>def main():
>  r = Realm()
>  p = portal.Portal(r)
>  c = checkers.InMemoryUsernamePasswordDatabaseDontUse()
>  c.addUser("AA", "aa")
>  p.registerChecker(c)
>  p.registerChecker(checkers.AllowAnonymousAccess())
>  f = protocol.ServerFactory()
>  f.protocol = lambda: TelnetTransport(AuthenticatingTelnetProtocol, p)
>  log.startLogging(sys.stdout)
>  from twisted.internet import reactor
>  reactor.listenTCP(4738, f)
>  reactor.run()
>if __name__ == '__main__':
>  main()

The default state of StatefulTelnetProtocol is "Discard".  If you change 
the avatar's state to "Command" before returning it from requestAvatar, 
I think you'll see the behavior you want.


More information about the Twisted-Python mailing list