[Twisted-Python] Sample Twisted Web NT Service

Thad Jacobs tjacobs at kinematic.com
Wed Mar 5 17:20:58 EST 2003

#This sample code demonstrates how to mount Twisted Web as a 
#Windows XP / 2000 / NT Service using win32all's win32service

import win32serviceutil
import win32service
import win32event
class TwistedWebNTService(win32serviceutil.ServiceFramework):
   _svc_name_ = "TwistedWebNTService"
   _svc_display_name_ = "Twisted Web NT 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
   app = Application("web")
   from twisted.web import server, static, twcgi, script, trp
   root = static.File("c:\\web") #Serve files out of c:\\web

  #Add the appropriate processors if you want to serve scripts
   root.processors = {
            '.cgi': twcgi.CGIScript,
            '.php3': twcgi.PHP3Script,
            '.php': twcgi.PHPScript,
            '.epy': script.PythonScript,
            '.rpy': script.ResourceScript,
            '.trp': trp.ResourceUnpickler,
   site = server.Site(root)

   app.listenTCP(8080, site)

   print "Finished"

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

More information about the Twisted-Python mailing list