[python-win32] HTTP Server as a Windows Service
Mike Smith
MSmith at carilion.com
Fri Feb 22 15:23:51 CET 2008
I've written the following script based on information I have found on the web. The purpose of the script is to start an HTTP listener on the machine it's running on that will give status on a particular process running on that system that can be polled by an external load balancer to determine if the host is a viable candidate for traffic to be sent to. For testing, I'm just using notepad.exe as the process I'm looking for. I've tried to create this as a service on the windows server it's running on, and it will install fine. The HTTP server starts up and provides the information I want. The problem I'm having is that the service will not stop when I tell it to. When I issue the stop request it tries to stop for a long time and throws back an error that it couldn't. Within the service control panel is just stays in a state of stopping. Any help that anyone could provide would be greatly appreciated. I'm new to python so please let me know if there is a more efficient way to do what I'm trying to do. The code follows:
import win32service
import win32serviceutil
import wmi
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
class BlobCheck(win32serviceutil.ServiceFramework):
_svc_name_ = "BlobCheck"
_svc_display_name_ = "BlobCheck"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.isAlive = True
def SvcDoRun(self):
import servicemanager
c = wmi.WMI()
while self.isAlive:
class RequestHandler(BaseHTTPRequestHandler):
def _writeheaders(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_HEAD(self):
self._writeheaders()
def do_GET(self):
self._writeheaders()
running = c.Win32_Process (name="notepad.exe")
if running:
self.wfile.write("""<HTML><HEAD><TITLE>Monitor</TITLE></HEAD>
<BODY>Kool-Aid!!!</BODY></HTML>""")
else:
self.wfile.write("""<HTML><HEAD><TITLE>Monitor</TITLE></HEAD>
<BODY>Oh No!!!</BODY></HTML>""")
serveraddr = ('', 12345)
srvr = HTTPServer(serveraddr, RequestHandler)
srvr.handle_request()
def SvcStop(self):
import servicemanager
servicemanager.LogInfoMsg("aservice - Recieved stop signal")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.isAlive = False
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(BlobCheck)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20080222/02cc073b/attachment.htm
More information about the python-win32
mailing list