12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!-*-coding:utf-8 -*-
- import sys
- import win32serviceutil
- import win32service
- import win32event
- from twainscan import server
- import servicemanager
- import winerror
-
- class PythonService(win32serviceutil.ServiceFramework):
- #服务名
- _svc_name_ = "PythonService"
- #服务在windows系统中显示的名称
- _svc_display_name_ = "Python Service Test"
- #服务的描述
- _svc_description_ = "This code is a Python service Test"
-
- def __init__(self, args):
- win32serviceutil.ServiceFramework.__init__(self, args)
- self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
-
- def SvcDoRun(self):
- # 把自己的代码放到这里,就OK
- # 等待服务被停止
- server()
- win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
-
- def SvcStop(self):
- # 先告诉SCM停止这个过程
- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
- # 设置事件
- win32event.SetEvent(self.hWaitStop)
-
- if __name__=='__main__':
- if len(sys.argv) == 1:
- try:
- evtsrc_dll = os.path.abspath(servicemanager.__file__)
- servicemanager.PrepareToHostSingle(PythonService)
- servicemanager.Initialize('PythonService', evtsrc_dll)
- servicemanager.StartServiceCtrlDispatcher()
- except win32service.error, details:
- if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
- win32serviceutil.usage()
- else:
- win32serviceutil.HandleCommandLine(PythonService)
|