Nt of services using python write: Mei Jinsong If we want the system to start when you execute a program, windows system and unix systems are not the same, only need for unix command to be executed by the rc.local into the system again You can load it when you start. Windows is more troublesome. If you put the program into the boot group, only the program is only entered, if you want to execute the program when you start, you must use NT services. How to use NT services under Python is actually very simple. Download Python's Win32 support. I am using: pywin32-202.win32-py2.3.exe can be used to write our service. Let's first create an empty service, build Test1.py this file, and write the following code: # - * - CODING: CP936 - * - Import Win32ServiceUtilimport Win32ServiceImport Win32event
class test1 (win32serviceutil.ServiceFramework): _svc_name_ = "test_python" _svc_display_name_ = "test_python" def __init __ (self, args): win32serviceutil.ServiceFramework .__ init __ (self, args) self.hWaitStop = win32event.CreateEvent (None, 0, 0, NONE)
DEF SVCSTOP (Self): # Tell the SCM first stop this process Self.ReportServiceStatus (win32service.service_stop_pending) # Set the event Win32Event.sevent (Self.hwaitStop)
Def Svcdorun (Self): # Waiting Service Stop Win32Event.waitforsingleObject (Self.hwaitstop, Win32Event.infinite)
IF __name __ == '__ main__': Win32ServiceUtil.Handlecommandline (TEST1) Note here, if you need to change the file name, such as the Test1 in Win32Serviceutil.HandleCommandline (TEST1) to your file name, while Class also needs to be with your file The name is consistent, otherwise the problem that the service cannot be started. Execute it in the command window, test1.py can see help prompts c: /> test1.pyusage: 'Test1.py [options] install | Update | Remove | Start [...] | STOP | Restart [...] | DEBUG [...] Options for 'INSTALL' AND 'UPDATE' Commands Only: --usename Domain / Username: The Username The Service IS To Run Under - Password Password: The Password for the UserName --Startup [Manual | AUTO | Disabled]: How the service starts, default = manual --INTERACTIVE: Allow the service to interact with the desktop.c: /> Install our service C: /> Test1.py InstallInstalling Service Test_python to Python Class C: / TEST1.TEST1SERVICE INSTALLED
C: /> We can manage our services in commands or in the Control Panel - Administrative Tools - "service. You can see Test_Python in the service, although this service does not do, but you can start and stop him. Let's take a service of a complex point. Write in the next day, you have to get off work.