[Twisted-Python] debugging listenTCP

Matthew Pounsett matt at conundrum.com
Mon Mar 11 21:40:30 EDT 2013

I've got a weird one here.   I've just come back to some code that I haven't worked on in a while, which is a simple XMLRPC server.  Last time I touched it I was working on adding cred support, so I'd gone from using a direct call to server.Site() in my listenTCP call to using my own factory/portal/etc.

When I fired it up this time, listenTCP was failing to bind to the requested ports, but not raising an exception.  I started writing up an email to ask about debugging this, but while doing information collection it started working again.  Then not.  Now it's working again.  It looks like I've got some sort of intermittent failure, but I have no idea how to track it down.

    # 'interfaces' is just a simple list of IP addresses.  The map()
    # call provides formatting for the log message
    logger.debug("Connecting to port {} on interfaces: {}".format(
        port, ", ".join(map( lambda x: "'{}'".format(x), interfaces))))
    for iface in interfaces:
        reactor.listenTCP(port, factory, interface=iface)

The application should bind to [::1]:8602 and, and logging seems to indicate this is what it's trying to do.

01:28:31 [server      ] Connecting to port 8602 on interfaces: '::1', ''

However, intermittently lsof and netstat are showing no binding.

> netstat -an | grep -c 8602

I tried turning on twisted's internal logging, but that is giving me nothing useful.  I see no way to turn up the debugging level on this log, but I normally use python's standard logger and perhaps I've missed some piece of documentation.  This is a sample from when it was failing:

2013-03-12 01:28:31+0000 [-] Log opened.
2013-03-12 01:28:31+0000 [-] ServerFactory starting on 8602
2013-03-12 01:28:31+0000 [-] Starting factory <insurrection.server.ServerFactory instance at 0x85a81488>
2013-03-12 01:28:31+0000 [-] ServerFactory starting on 8602

Can anyone suggest anything more I can do to track down the problem next time it crops up?

More information about the Twisted-Python mailing list