The Evolution of Finger: a Twisted finger client


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.

Additionally, because this code now programmatically receives its host and port, it’s a bit less convenient to use clientFromString. Instead, we move to using the specific endpoint we want. In this case, because we’re connecting as a client over IPv4 using TCP, we want the TCP4ClientEndpoint.


# finger proxy
from twisted.application import internet, service, strports
from twisted.internet import defer, protocol, reactor, endpoints
from twisted.protocols import basic
from twisted.python import components
from zope.interface import Interface, implementer

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)
        def writeValue(value):

class FingerFactoryFromService(protocol.ClientFactory):

    protocol = FingerProtocol

    def __init__(self, service):
        self.service = service

    def getUser(self, user):
        return self.service.getUser(user)


class FingerClient(protocol.Protocol):

    def connectionMade(self):
        self.buf = []

    def dataReceived(self, data):

    def connectionLost(self, reason):

class FingerClientFactory(protocol.ClientFactory):

    protocol = FingerClient

    def __init__(self, user):
        self.user = user
        self.d = defer.Deferred()

    def clientConnectionFailed(self, _, reason):

    def gotData(self, data):

def finger(user, host, port=79):
    f = FingerClientFactory(user)
    endpoint = endpoints.TCP4ClientEndpoint(reactor, host, port)
    return f.d

class ProxyFingerService(service.Service):

    def getUser(self, user):
            user, host = user.split('@', 1)
            user = user.strip()
            host = ''
        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()
strports.service("tcp:7779", IFingerFactory(f)).setServiceParent(