1 # Read username, output from non-empty factory, drop connections
2 # Use deferreds, to minimize synchronicity assumptions
4 from twisted.internet import protocol, reactor, defer
5 from twisted.protocols import basic
7 class FingerProtocol(basic.LineReceiver):
8 def lineReceived(self, user):
9 d = self.factory.getUser(user)
12 return 'Internal error in server'
15 def writeResponse(message):
16 self.transport.write(message + '\r\n')
17 self.transport.loseConnection()
18 d.addCallback(writeResponse)
20 class FingerFactory(protocol.ServerFactory):
21 protocol = FingerProtocol
23 def __init__(self, **kwargs):
26 def getUser(self, user):
27 return defer.succeed(self.users.get(user, "No such user"))
29 reactor.listenTCP(1079, FingerFactory(moshez='Happy and well'))