[Twisted-Python] Running a twisted server as a WinNT service

Andrew Bennetts andrew-twisted at puzzling.org
Wed Dec 12 02:54:30 EST 2001

On Tue, Dec 11, 2001 at 10:49:20PM -0600, Glyph Lefkowitz wrote:
> On Sun, 2001-12-09 at 20:46, Andrew Bennetts wrote:
> > I wonder if there is a more integrated way to do this, though?
> Well, the "integrated" way to do it would probably to be something
> similar to moshe's 'tap2deb' -- a Twisted service would probably be a
> .tap or .tac file which got loaded, similar to the way twistd does, and
> used the 'startLogging' call to open a log file and start writing to
> it.  A good default would be the doc/examples/coil.tac currently in CVS
> (and in the coopweb.org "breakfast builds").

Hmm.  I'm not sure how this would work... Python supports WinNT services
by providing a special executable "PythonService.exe", which runs the
script on your behalf, and passes things like "Stop" messages to your
SvcStop method.  The HandleCommandLine bit at the end of that script I
gave is a conveniece thing to wrap up installation and removal of
services -- typical usage of a script is:

    myservice.py install        # installs the service

    myservice.py start          # starts the service, this can also be
                                # done via the control panel

    myservice.py remove         # removes the service from the system

Services are just processes that can be automatically started on boot,
and can be stopped and started.  My point here is that whatever twisted
does, it will need to do it inside of a class with SvcDoRun, etc, that
needs to be registered as a service.

Apologies if I've just told you what you already know...

> Does win32 have a standardized logging mechanism?  Twisted can log to
> any file-like object, so if we could put log messages wherever winnt
> expects to find them that would be good ;-)  (I vaguely recall something
> called the "Event Log"?)

It does, but it's more for logging "events" than individual messages,
i.e. each line of the current log file should *not* be an entry in the
NT event log, unless you're okay with spamming the log ;)

Typically, programs will only report unusual events in the Event Log,
e.g. "could not replicate filesystem", "the service 'blah' could not be
started", etc.  There is a notion of severity, though.  Basically, I'm
not sure that the NT Event Log is going to properly fit the way Twisted
logs, especially seeing as services failing already gets logged
automatically, and PythonService.exe sees to it that the traceback is

I'm not sure of any better standard places though.  I believe IIS
typically logs to %SYSTEMROOT%\Logs\W3SVC\yyyymmdd.log (SYSTEMROOT is an
environment variable that typically is set to C:\WINNT). Perhaps a
similar scheme would be appropriate for Twisted?

If you are interested in the Event Log route, just Google for "python
event log", or read the relevant chapter of the _Programming Python on
Win32_ book.


More information about the Twisted-Python mailing list