[Twisted-Python] Twisted UDP & makeConnection

James Saker jamessaker at firepole.com
Sat Sep 3 13:17:07 MDT 2005


Got a bit of a puzzle trying to make a minimal syslog relay out of twisted and 
am wondering if it's due to an incompatibility between ServerFactory and 
Twisted's UDP, or if I'm simply missing something obvious.

In trying to learn Twisted better, I've taken the finger13.py sample and have 
changed it to listen to the syslog UDP port and echo messages (code follows 
this message). One minor variation is that I want to only take UDP messages 
from hosts in my hostlist (granted UDP can be easily spoofed, but that's 
another matter).

I'm invoking finger13 with twistd, and end up with twistd complaining about 
the ServerFactory instance not having an attribute "makeConnection.":

2005/09/03 14:06 CDT [-] AttributeError: ServerFactory instance has no 
attribute 'makeConnection'

 I've traced this message back to Twisted's udp.py (line 96, 
_connectToProtocol) which refers to self.protocol, and self.protocol is the 
protocol passed in when class Port is initialized (self.protocol = proto). 
I've tried putting makeConnection in the BSDSyslogProtocol class but that 
doesn't work; actually, I'm surprised it's complaining because the parent 
twisted DatagramProtocol class has a makeConnection method.

Perhaps factories need to be handled differently for UDP? I'm stumped!

Jamie


# syslogd13.py Based on finger13.py 
from twisted.application import internet, service
from twisted.internet import protocol, reactor, defer
from twisted.internet.protocol import DatagramProtocol
from twisted.protocols import basic

class BSDSyslogProtocol(DatagramProtocol):
    
    def datagramReceived(self, data, (host, port)):
        self.factory.getMessage(data
        ).addErrback(lambda _: "Internal error in server"
        ).addCallback(self.transport.loseConnection())
    def makeConnection(self): pass

class ValidHostSetterProtocol(basic.LineReceiver):
    def connectionMade(self): self.lines = []
    
    def lineReceived(self, line): 
        self.lines.append(line)
    def connectionLost(self, _connDropMsg):
        for line in self.lines:
            print "Received the following." % line

class SyslogRelayService(service.Service):
    def __init__(self, *args, **kwargs):
        self.parent.__init__(self, *args)
        self.hosts = kwargs
    def getHost(self, host):
        return defer.succeed(self.hosts.get(host, "No such host"))
    def getHostFactory(self):
        h = protocol.ServerFactory()
        h.protocol, h.getHost = BSDSyslogProtocol, self.getHost
        return h

application = service.Application('syslog-relay', uid=1, gid=1)
slr = SyslogRelayService('syslog-relay', hosts='127.0.0.1')
serviceCollection = service.IServiceCollection(application)
internet.UDPServer(514, slr.getHostFactory()
                   ).setServiceParent(serviceCollection)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: </pipermail/twisted-python/attachments/20050903/eb0fdc75/attachment.sig>


More information about the Twisted-Python mailing list