[Twisted-Python] NT Service

Thad Jacobs tjacobs at kinematic.com
Wed Mar 5 16:01:24 EST 2003


I've got the service up and running, but it doesn't do anything.  All I want
to do is make a twisted web application that listens on port 8080 and serves
files out of c:\web  -- or better yet, it would be nice if  I could simply
instantiate the web.tap file just as I would using "twistd -f web.tap".

I'm willing to get under the hood and make this happen, but unfortunately
I'm under a bit of a time constraint.  I've been RTFM'ing all morning, but
the documentation is scattered, and I am a bit green in the realms of
asynchronous server programming, and have been coding in python for less
than a week.

Could you advise me as to what direction to take in instantiating a twisted
web application from within the service?  I was thinking of simply importing
twisted.py, but I can't figure out how to make it run with options I dictate
programatically, than the system command line switches.

Once I've figured this out, I'll  be sure to post the full source, so others
may benefit.

Here's the basic service, almost Identical to the one Bob posted, though I
had to import twisted.internet.reactor to get it to work.

import win32serviceutil
import win32service
import win32event
class KAQueryService(win32serviceutil.ServiceFramework):
   _svc_name_ = "KAQueryService"
   _svc_display_name_ = "Kinematic Query Service"
   def __init__(self, args):
     win32serviceutil.ServiceFramework.__init__(self, args)
     self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

   def SvcStop(self):

   def SvcDoRun(self):
     from twisted.python.log import startLogging
     # we don't have the console, so we log somewhere
     # the NT service logging facilities probably isn't the right place

   def CheckForQuit(self):
     from twisted.internet import reactor
     #print "Check For Quit"
     rv = win32event.WaitForSingleObject(self.hWaitStop, 10.0)
     if not rv == win32event.WAIT_TIMEOUT:
       print "Received Quit from Win32"
     reactor.callLater(1.0, self.CheckForQuit)

def startTwisted():
   from twisted.internet.app import Application

   #How do I specify a port or load a tap file?
   app = Application("web")
   print "Finished"

if __name__=='__main__':
   # you could call startTwisted to run as not-a-service
   # or use it for cross-platform reasons

-----Original Message-----
From: Glyph Lefkowitz [mailto:glyph at twistedmatrix.com]
Sent: Wednesday, March 05, 2003 11:15 AM
To: twisted-python at twistedmatrix.com
Subject: Re: [Twisted-Python] NT Service

Hash: SHA1

On Wednesday, March 5, 2003, at 12:41 PM, Bob Ippolito wrote:

> I'm assuming you've already RTFM'ed about win32all's win32service and 
> Twisted.  Here's a stripped example of what has worked very well for 
> me for the few times I was forced to deploy under Windows:

It would be really great if someone could genericize this and create a 
version that had a configuration dialog that allowed you to select a 
particular .tap file to load, rather than instantiating the Application 
inline.  That way deploying on Win32 would be much more directly 
analagous to deploying on unix.
Version: GnuPG v1.2.1 (Darwin)


Twisted-Python mailing list
Twisted-Python at twistedmatrix.com

More information about the Twisted-Python mailing list