[Twisted-Python] Telnet server example?

Eugene Coetzee projects at reedflute.com
Tue Aug 10 14:17:39 EDT 2004


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







More information about the Twisted-Python mailing list