醉里乾坤大,壶中日月长

Python Windows Services

上一篇 / 下一篇  2009-10-20 16:10:41

Name:Essien Ita Essien
Location:Nigeria
http://blog.csdn.net/wujilin/archive/2008/04/05/2253141.aspx

We had project last friday to write a windows service that would poll an Exchange mail box and write to a database. It was executed in C#. Well... i got thinking... hmmm... can't python be used to do this? Because if it can, then that project would not last more than a couple of hours (actualy, the C# project shouldn't also last more than a couple of hours if you know what you'r doing, but hey.. i have to have something to say about why i decided to do it in Python write? i mean.... I LOVE Python may not cut it for some folks :) )

OK... lets get to the meat of it.

I first hit google, looking for links to python and windows services. I then found some mailing list archives and in one of them, there was a reference to a chapter on Windows Services in the book, Programming Python on Win32.

Well... i had access to the book, and went headlong. Long and short of it, 15 mins of perusing latter, I had a windows service running. Lets attempt to get this stuff to work together now.


First there are two very important modules that helps our lives out. win32service and win32serviceutil

We're going to write a service that just writes to the event log every 10 seconds. U'll have to stop this service soon or else, you'll run out of EvenLog space ;)

First some important pre-parations and background on python-windows services. There is a program called pythonservice.exe, that actually handles everything that concerns windows services written in Python. I'm walking a thin rope here with this explanation now, but this is how i understand it, just don't quote me in a conference where there are other Pythonistas and Lords Of the Eggs, I'll deny it vehemently ;)

The way i figure it, pythonservice.exe is to python services what the usual python.exe is for all other normal python scripts. Normally on windows, once .py is associated with python.exe, each time you run a script, the python intepreter, python.exe is called to 'intepret' it. For services, apparently some funky stuff goes on, so instead of calling python.exe as the intepreter, pythonservice.exe is called as the intepreter (well, not exactly intepreter i guess, but it is the process that runs the services.) You can also look at it like this: You say service start, windows identifies it as a python service, and starts pythonservice.exe passing it parameters to find the service itself. Pythonservice.exe locates the service, and starts it. As far as windows is concerned, it is running a process called PythonService.exe (You'll see that without special tricks, when writting to eventlog, PythonService is the one that does the writing)

Now the preceeding means that windows has to know to associate python services with pythonservice.exe . This is essentially called registeration. So pythonservice.exe must be registered with windows to handle python windows services. to do that, locate where the python win32 extensions are on your box. They'll probably be in your site-packages folder. Look for the win32\ subdirectory, and you'll locate pythonservice.exe sitting somewhere there:

Mine is at C:\Python24\Lib\site-packages\win32\pythonservice.exe

you can change to that directory:

C:\Python24\Lib\site-packages\win32

and then do: pythonservice.exe /register

You should see a message about registering the Python Service Manager.

After that, if there are no errors, we're ready to plunge.


First, we need to import the two all important modules.

For those who don't understand Python... the lines beginning with #, are just comments.




import win32service
import win32serviceutil
import time

#at this point. We're ready to go.
#Put simply, a python windows service inherits from win32serviceutils.ServiceFramework
#simply extending that class, sets up all you ever need to do.

class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "aservice"
_svc_display_name_ = "aservice - It Does nothing"

def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
#at this point service is created pefectly.
#you could stop here and jump to setting up the '__main__' section,
#but you wont be able to stop your service, and it won't do anything.
#at the very least, you need to implement SvcDoRun() and better still SvcStop():
#This next attribute is used when its stopping time.
self.isAlive = True

def SvcDoRun(self):
import servicemanager
while self.isAlive:
#remember when i said you needed only two modules?
#well... i think i lied. If you're going to do anything
#usefull, you're going to obviously need more modules.
#This next module servicemanager, has some funny
#properties that makes it only to be visible when
#the service is properly setup. This means it can't be imported
#in normal python programs, and can't even be imported
#in the Global Namespace, but only in local functions that
#will be called after the service is setup. Anyway,
#this module contains some utilities for writing to EventLog.

servicemanager.LogInfoMsg("aservice - is alive and well")
time.sleep(10)
servicemanager.LogInfoMsg("aservice - Stopped")

def SvcStop(self):
#before you stop, you'll want to inform. windows that
#you've recieved a stop signal, and you're trying to stop.
#in the windows Service manager, this is what shows the status message as
#'stopping'. This is important, since SvcDoRun() may take sometime before it stops.
import servicemanager
servicemanager.LogInfoMsg("aservice - Recieved stop signal")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.isAlive = False #this will make SvcDoRun() break the while loop at the next iteration.

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(aservice) #this line sets it all up to run properly.



Uhh... just incase you haven't guessed. That is all for the service.
The next part is installing and starting it. You can save this as aservice.py

cd to the directory where it is saved and do:

aservice.py install

Note that 'aservice.py remove' will remove the service

you can start and stop with Windows Service manager or:

aservice.py start

and

aservice.py stop


OK... that's it... play around, flesh out... anything.

You may or may not have figured out that the entire functionality of the serivice gets started from SvcDoRun()


That's all folks... i hope this is usefull :)
Hey... i just had a brainwave. I'll repeat the code here without any comments :)

import win32service
import win32serviceutil
import time

class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "aservice"
_svc_display_name_ = "aservice - It Does nothing"

def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.isAlive = True

def SvcDoRun(self):
import servicemanager

while self.isAlive:
servicemanager.LogInfoMsg("aservice - is alive and well")
time.sleep(10)
servicemanager.LogInfoMsg("aservice - Stopped")

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(aservice)


Note that if you're going to copy and paste this stuff, you may have some white space issues, since i'm just typing straight and editing here.


gtg


TAG:

逍遥客 引用 删除 xiaoyaoke   /   2009-10-20 16:25:34
class ServiceFramework:
    # Required Attributes:
    # _svc_name_ = The service name
    # _svc_display_name_ = The service display name

    # Optional Attributes:
    _svc_deps_ = None        # sequence of service names on which this depends
    _exe_name_ = None        # Default to PythonService.exe
    _exe_args_ = None        # Default to no arguments
    _svc_description_ = None # Only exists on Windows 2000 or later, ignored on windows NT

    def __init__(self, args):
        import servicemanager
        self.ssh = servicemanager.RegisterServiceCtrlHandler(args[0], self.ServiceCtrlHandlerEx, True)
        servicemanager.SetEventSourceName(self._svc_name_)
        self.checkPoint = 0

    def GetAcceptedControls(self):
        # Setup the service controls we accept based on our attributes. Note
        # that if you need to handle controls via SvcOther[Ex](), you must
        # override this.
        accepted = 0
        if hasattr(self, "SvcStop"): accepted = accepted | win32service.SERVICE_ACCEPT_STOP
        if hasattr(self, "SvcPause") and hasattr(self, "SvcContinue"):
            accepted = accepted | win32service.SERVICE_ACCEPT_PAUSE_CONTINUE
        if hasattr(self, "SvcShutdown"): accepted = accepted | win32service.SERVICE_ACCEPT_SHUTDOWN
        return accepted

    def ReportServiceStatus(self, serviceStatus, waitHint = 5000, win32ExitCode = 0, svcExitCode = 0):
        if self.ssh is None: # Debugging!
            return
        if serviceStatus == win32service.SERVICE_START_PENDING:
            accepted = 0
        else:
            accepted = self.GetAcceptedControls()

        if serviceStatus in [win32service.SERVICE_RUNNING,  win32service.SERVICE_STOPPED]:
            checkPoint = 0
        else:
            self.checkPoint = self.checkPoint + 1
            checkPoint = self.checkPoint

        # Now report the status to the control manager
        status = (win32service.SERVICE_WIN32_OWN_PROCESS,
                 serviceStatus,
                 accepted, # dwControlsAccepted,
                 win32ExitCode, # dwWin32ExitCode;
                 svcExitCode, # dwServiceSpecificExitCode;
                 checkPoint, # dwCheckPoint;
                 waitHint)
        win32service.SetServiceStatus( self.ssh, status)

    def SvcInterrogate(self):
        # Assume we are running, and everyone is happy.
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)

    def SvcOther(self, control):
        try:
            print "Unknown control status - %d" % control
        except IOError:
            # services may not have a valid stdout!
            pass

    def ServiceCtrlHandler(self, control):
        self.ServiceCtrlHandlerEx(control, 0, None)

    # The 'Ex' functions, which take additional params
    def SvcOtherEx(self, control, event_type, data):
        # The default here is to call self.SvcOther as that is the old behaviour.
        # If you want to take advantage of the extra data, override this method
        self.SvcOther(control)

    def ServiceCtrlHandlerEx(self, control, event_type, data):
        if control==win32service.SERVICE_CONTROL_STOP:
            self.SvcStop()
        elif control==win32service.SERVICE_CONTROL_PAUSE:
            self.SvcPause()
        elif control==win32service.SERVICE_CONTROL_CONTINUE:
            self.SvcContinue()
        elif control==win32service.SERVICE_CONTROL_INTERROGATE:
            self.SvcInterrogate()
        elif control==win32service.SERVICE_CONTROL_SHUTDOWN:
            self.SvcShutdown()
        else:
            self.SvcOtherEx(control, event_type, data)

    def SvcRun(self):
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        self.SvcDoRun()
        # Once SvcDoRun terminates, the service has stopped.
        # We tell the SCM the service is still stopping - the C framework
        # will automatically tell the SCM it has stopped when this returns.
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
 

评分:0

我来说两句

Open Toolbar