1

I am using the following script from the Twisted tutorial (with slight modification):

from twisted.application import internet, service
from twisted.internet import reactor, protocol, defer
from twisted.protocols import basic
from twisted.web import client

class FingerProtocol(basic.LineReceiver):

    def lineReceived(self, user):
        d = self.factory.getUser(user)

        def onError(err):
            return "Internal server error"
        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, prefix):
        self.prefix = prefix

    def getUser(self, user):
        return client.getPage(self.prefix + user)

application = service.Application('finger', uid=1, gid=1)
factory = FingerFactory(prefix="http://livejournal.com/~")
internet.TCPServer(7979, factory).setServiceParent(
    service.IServiceCollection(application))

which I save as finger_daemon.tac and run with

twistd -y finger_daemon.tac \ 
    -l /home/me/twisted/finger.log \
    --pidfile=/home/me/twisted/finger.pid

but of course it won't bind to 79, since it's a privileged port. I tried also running with sudo, no difference there.

I then tried changing the TCPServer port to 7979 and then connecting to the daemon once running with

telnet 127.0.0.1 7979

and I get Connection Refused error. What's going on here specifically? How is daemonizing supposed to work in Twisted?

lollercoaster
  • 15,969
  • 35
  • 115
  • 173

1 Answers1

1

When I run this code, I see the following log message:

2013-10-02 23:50:34-0700 [-] failed to set uid/gid 1/1 (are you root?) -- exiting.

and then twistd exits. So you'd need to do sudo twistd and then that adds a whole bunch of python path management problems...

Why are you setting the uid and gid arguments? You're trying to run it as the daemon user? You don't need to do that in order to daemonize. Just removing the uid=1, gid=1 arguments to Application makes it work for me.

Glyph
  • 31,152
  • 11
  • 87
  • 129
  • also importantly - how did you see the error? twistd runs silently for me – lollercoaster Oct 03 '13 at 21:57
  • 1
    I work on Twisted, so a direct link to the tutorial that mentions this example would be useful :-). – Glyph Oct 06 '13 at 02:18
  • I saw the error by running `twistd -n -y finger_daemon.tac`. With your invocation, you should have seen the same error though, just in `/home/me/twisted/finger.log`. (Although startup errors are not always reported correctly; it would be interesting to know whether you saw that or not!) – Glyph Oct 06 '13 at 02:19
  • Here's one example of the uid/gid = 1 indictions: http://twistedmatrix.com/documents/current/core/howto/tutorial/style.html – lollercoaster Oct 07 '13 at 00:52