[Twisted-Python] Telnet server example?

Britt Green seamonkeys at gmail.com
Wed Aug 11 02:55:08 EDT 2004


Hi Eugene,

Thanks for the example. Just a couple of quick questions if you don't mind.

1) Does this create a telnetd daemon? The checkUserAndPass() method
seems like it checks with the /etc/passwd file to enable a user to
logon. Since I don't have access to my Unix box tonite I can't check
this myself. Please correct me if I'm mistaken.

2) What does defer do exactly?

3) Say I'm writing a program that takes connections via Telnet, like a
chat server. Would using Twisted's Telnet class be the best way to
accomplish this? Or should I just write my own via the more low-level
Twisted classes?

Thanks!

Britt

On Tue, 10 Aug 2004 20:17:39 +0200, Eugene Coetzee
<projects at reedflute.com> wrote:
> 
> 
> Britt Green wrote:
> 
> >So I got my Echo server working...kind of. There were some issues with
> >Putty in Telnet versus Raw Mode, but that's a posting for elsewhere.
> >Anyways, rather than re-invent the wheel I'd like to take advantage of
> >Twisted's Telnet classes. Unfortunately I've not been able to find any
> >examples of a server that uses Twisted's Telnet. I've tried writing my
> >own code but I've not had any luck getting it to work.
> >
> >Therefore...would anyone mind showing me a minimal example of a server
> >that uses the Telnet library?
> >
> >Many thanks,
> >
> >Britt
> >
> >_______________________________________________
> >Twisted-Python mailing list
> >Twisted-Python at twistedmatrix.com
> >http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
> >
> >
> >
> >
> Excuse the mess:
> 
> from twisted.protocols.telnet import *
> from twisted.application import service,internet
> from twisted.internet import protocol,defer
> 
> class TelnetProtocol(Telnet):
> def connectionMade(self):
> print "TelnetProtocol.connectionMade"
> Telnet.connectionMade(self)
> 
> def telnet_Password(self, paswd):
> """I accept a password as an argument, and check it with the
> checkUserAndPass method. If the login is successful, I call
> loggedIn()."""
> self.write(IAC+WONT+ECHO+"*****\r\n")
> #try:
> # checked = self.checkUserAndPass(self.username, paswd)
> #except:
> # return "Done"
> #if not checked:
> # return "Done"
> self.loggedIn()
> return "Command"
> 
> def dataReceived(self,data):
> print "TelnetProtocol.dataReceived"
> print data
> Telnet.dataReceived(self,data)
> 
> class TelnetFactory(protocol.ServerFactory):
> protocol = TelnetProtocol
> def __init__(self, **kwargs): self.users = kwargs
> def getUser(self, user):
> print "TelnetFactory.getUser"
> print user
> return defer.succeed(self.users.get(user, "No such user"))
> 
> application=service.Application('serial',uid=1000,gid=100)
> 
> factory = TelnetFactory()
> internet.TCPServer(1021, factory).setServiceParent(
> service.IServiceCollection(application))
> 
> Do not trust this code - it will open a telnet session without any user
> authentication - you have to play around a little with the class as
> documented at :
> 
> http://www.twistedmatrix.com/documents/current/api/twisted.protocols.telnet.Telnet.html
> 
> regards,
> 
> Eugene
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>




More information about the Twisted-Python mailing list