The Evolution of Finger: a Twisted finger client

  1. Introduction
  2. Finger Proxy

Introduction

This is the ninth part of the Twisted tutorial Twisted from Scratch, or The Evolution of Finger.

In this part, we develop a client for the finger server: a proxy finger server which forwards requests to another finger server.

Finger Proxy

Writing new clients with Twisted is much like writing new servers. We implement the protocol, which just gathers up all the data, and give it to the factory. The factory keeps a deferred which is triggered if the connection either fails or succeeds. When we use the client, we first make sure the deferred will never fail, by producing a message in that case. Implementing a wrapper around client which just returns the deferred is a common pattern. While less flexible than using the factory directly, it's also more convenient.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

# finger proxy from twisted.application import internet, service from twisted.internet import defer, protocol, reactor from twisted.protocols import basic from twisted.python import components from zope.interface import Interface, implements def catchError(err): return "Internal error in server" class IFingerService(Interface): def getUser(user): """Return a deferred returning a string""" def getUsers(): """Return a deferred returning a list of strings""" class IFingerFactory(Interface): def getUser(user): """Return a deferred returning a string""" def buildProtocol(addr): """Return a protocol returning a string""" class FingerProtocol(basic.LineReceiver): def lineReceived(self, user): d = self.factory.getUser(user) d.addErrback(catchError) def writeValue(value): self.transport.write(value) self.transport.loseConnection() d.addCallback(writeValue) class FingerFactoryFromService(protocol.ClientFactory): implements(IFingerFactory) protocol = FingerProtocol def __init__(self, service): self.service = service def getUser(self, user): return self.service.getUser(user) components.registerAdapter(FingerFactoryFromService, IFingerService, IFingerFactory) class FingerClient(protocol.Protocol): def connectionMade(self): self.transport.write(self.factory.user+"\r\n") self.buf = [] def dataReceived(self, data): self.buf.append(data) def connectionLost(self, reason): self.factory.gotData(''.join(self.buf)) class FingerClientFactory(protocol.ClientFactory): protocol = FingerClient def __init__(self, user): self.user = user self.d = defer.Deferred() def clientConnectionFailed(self, _, reason): self.d.errback(reason) def gotData(self, data): self.d.callback(data) def finger(user, host, port=79): f = FingerClientFactory(user) reactor.connectTCP(host, port, f) return f.d class ProxyFingerService(service.Service): implements(IFingerService) def getUser(self, user): try: user, host = user.split('@', 1) except: user = user.strip() host = '127.0.0.1' ret = finger(user, host) ret.addErrback(lambda _: "Could not connect to remote host") return ret def getUsers(self): return defer.succeed([]) application = service.Application('finger', uid=1, gid=1) f = ProxyFingerService() internet.TCPServer(7779, IFingerFactory(f)).setServiceParent( service.IServiceCollection(application))

Index

Version: 11.0.0 Site Meter