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

2009-09-02 Thread filoufake-python
Hello,

>It's hard to say what's going wrong without being able to see all of the 
>code.  A short, self contained, correct example () 
>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()


  


___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] twistd plugin or .tac file?

2009-09-02 Thread Itamar Shtull-Trauring
On Tue, 2009-09-01 at 13:39 -0400, Martin-Louis Bright wrote:

> Which is the recommended or preferred way to deploy an app that will
> leverage twistd: designing the app as a twistd plugin or creating a
> Service and using a .tac file?

A plugin is nicer in that you can have command-line options, e.g.

$ twistd -n web --path=/tmp --port=8080

> Also, if you need to expose your functionality as a Service to
> properly use the twisted application framework, how do you achieve
> this if your app doesn't need a network port?

Just subclass Service and override startService and stopService to do
whatever you need to start and stop.

--Itamar


___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


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

2009-09-02 Thread exarkun
On 11:44 am, filoufake-pyt...@yahoo.fr wrote:
>Hello,
>>It's hard to say what's going wrong without being able to see all of 
>>the
>>code.  A short, self contained, correct example ()
>>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.

Jean-Paul

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python