Mike Smith wrote: > 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) > > > ------------------------------------------------------------------------ > > _______________________________________________ > python-win32 mailing list > python-win32@python.org > http://mail.python.org/mailman/listinfo/python-win32
This seems to come up about every other week. The problem is that you haven't given your service a way to KNOW that you have sent a stop signal. Below is a skeleton of a correct SvcDoRun method: def SvcDoRun(self): import servicemanager #--------------------------------------------------------------------- # Make entry in the event log that this service started #--------------------------------------------------------------------- servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) self.timeout=1000 # In milliseconds (update every second) while 1: #------------------------------------------------------------------- # Wait for service stop signal, if I timeout, loop again #------------------------------------------------------------------- rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout) # # Check to see if self.hWaitStop happened # if rc == win32event.WAIT_OBJECT_0: # # Stop signal encountered # break else: # # Insert your functional code here # #--End while-- #--------------------------------------------------------------------- # Log stopped message to EventLog #--------------------------------------------------------------------- servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STOPPED, (self._svc_name_, '')) return def SvcStop(self): #--------------------------------------------------------------------- # Before we do anything, tell SCM we are beginning the stop process. #--------------------------------------------------------------------- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #--------------------------------------------------------------------- # And set my event #--------------------------------------------------------------------- win32event.SetEvent(self.hWaitStop) return You have to register WaitForSingleObject so that you will know that the stop signal has been sent and break out of the loop. It is also important that you "sleep" (timeout) so that other processes can run in the background. You don't want your service to take over 100% of the machine. The amount of time you set in self.timeout determines how often the loop runs. You must determine how often that will be depending on your application. You may want to invest in a book: "Python Programming On Win32" it helped me a lot when I was getting started. Note: I don't think you want to define your RequestHandler class inside SvcDoRun and instantiate the HTTPserver every time through the loop. You should just update the information on the page each time through the loop. Hope information helps. Larry _______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32