xscanserver.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!-*-coding:utf-8 -*-
  2. import sys
  3. import win32serviceutil
  4. import win32service
  5. import win32event
  6. from twainscan import server
  7. import servicemanager
  8. import winerror
  9. class PythonService(win32serviceutil.ServiceFramework):
  10. #服务名
  11. _svc_name_ = "PythonService"
  12. #服务在windows系统中显示的名称
  13. _svc_display_name_ = "Python Service Test"
  14. #服务的描述
  15. _svc_description_ = "This code is a Python service Test"
  16. def __init__(self, args):
  17. win32serviceutil.ServiceFramework.__init__(self, args)
  18. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  19. def SvcDoRun(self):
  20. # 把自己的代码放到这里,就OK
  21. # 等待服务被停止
  22. server()
  23. win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
  24. def SvcStop(self):
  25. # 先告诉SCM停止这个过程
  26. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  27. # 设置事件
  28. win32event.SetEvent(self.hWaitStop)
  29. if __name__=='__main__':
  30. if len(sys.argv) == 1:
  31. try:
  32. evtsrc_dll = os.path.abspath(servicemanager.__file__)
  33. servicemanager.PrepareToHostSingle(PythonService)
  34. servicemanager.Initialize('PythonService', evtsrc_dll)
  35. servicemanager.StartServiceCtrlDispatcher()
  36. except win32service.error, details:
  37. if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
  38. win32serviceutil.usage()
  39. else:
  40. win32serviceutil.HandleCommandLine(PythonService)