summaryrefslogtreecommitdiff
path: root/doc/core/howto/tutorial/listings/finger/finger11.tac
diff options
context:
space:
mode:
Diffstat (limited to 'doc/core/howto/tutorial/listings/finger/finger11.tac')
-rwxr-xr-xdoc/core/howto/tutorial/listings/finger/finger11.tac34
1 files changed, 34 insertions, 0 deletions
diff --git a/doc/core/howto/tutorial/listings/finger/finger11.tac b/doc/core/howto/tutorial/listings/finger/finger11.tac
new file mode 100755
index 0000000..aae8ca6
--- /dev/null
+++ b/doc/core/howto/tutorial/listings/finger/finger11.tac
@@ -0,0 +1,34 @@
+# Read username, output from non-empty factory, drop connections
+# Use deferreds, to minimize synchronicity assumptions
+# Write application. Save in 'finger.tpy'
+
+from twisted.application import internet, service
+from twisted.internet import protocol, reactor, defer
+from twisted.protocols import basic
+
+class FingerProtocol(basic.LineReceiver):
+ def lineReceived(self, user):
+ d = self.factory.getUser(user)
+
+ def onError(err):
+ return 'Internal error in server'
+ d.addErrback(onError)
+
+ def writeResponse(message):
+ self.transport.write(message + '\r\n')
+ self.transport.loseConnection()
+ d.addCallback(writeResponse)
+
+class FingerFactory(protocol.ServerFactory):
+ protocol = FingerProtocol
+
+ def __init__(self, **kwargs):
+ self.users = kwargs
+
+ def getUser(self, user):
+ return defer.succeed(self.users.get(user, "No such user"))
+
+application = service.Application('finger', uid=1, gid=1)
+factory = FingerFactory(moshez='Happy and well')
+internet.TCPServer(79, factory).setServiceParent(
+ service.IServiceCollection(application))