Before we move to some of the advanced topics, we will develop the basis for a real Python service that actually does something useful!
The first version of our service starts by accepting connections over a named pipe and comes complete with a client that connects to the service. You then enhance the service by writing to the Event Log and by providing Performance Monitor data.
The first cut looks very much like the
SmallestPythonService, except it has more meat in
method. The main thread creates a named
pipe and waits for either a client to connect or a service control
More information on named pipes can be found in Chapter 17.This example also shows a number of concepts important when using named pipes. It shows how to use overlapped I/O, and how to create a special security object useful for named-pipe services:
# PipeService1.py # # A sample demonstrating a service which uses a # named-pipe to accept client connections. import win32serviceutil import win32service import win32event import win32pipe import win32file import pywintypes import winerror class PipeService(win32serviceutil.ServiceFramework): _svc_name_ = "PythonPipeService" _svc_display_name_ = "A sample Python service using named pipes" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) # Create an event which we will use to wait on. # The "service stop" request will set this event. self.hWaitStop = win32event.CreateEvent(None, 0, ...