Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import socket
  2. from main_bot import *
  3. import win32serviceutil
  4.  
  5. import servicemanager
  6. import win32event
  7. import win32service
  8.  
  9.  
  10.  
  11. class SMWinservice(win32serviceutil.ServiceFramework):
  12. '''Base class to create winservice in Python'''
  13. _svc_name_ = 'pythonService'
  14. _svc_display_name_ = 'Python Service'
  15. _svc_description_ = 'Python Service Description'
  16.  
  17. @classmethod
  18. def parse_command_line(cls):
  19. '''
  20. ClassMethod to parse the command line
  21. '''
  22. win32serviceutil.HandleCommandLine(cls)
  23.  
  24. def __init__(self, args):
  25. '''
  26. Constructor of the winservice
  27. '''
  28. win32serviceutil.ServiceFramework.__init__(self, args)
  29. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  30. socket.setdefaulttimeout(60)
  31.  
  32. def SvcStop(self):
  33. '''
  34. Called when the service is asked to stop
  35. '''
  36. self.stop()
  37. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  38. win32event.SetEvent(self.hWaitStop)
  39.  
  40. def SvcDoRun(self):
  41. '''
  42. Called when the service is asked to start
  43. '''
  44. self.start()
  45. servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
  46. servicemanager.PYS_SERVICE_STARTED,
  47. (self._svc_name_, ''))
  48. self.main()
  49.  
  50. def start(self):
  51. connect()
  52. '''
  53. Override to add logic before the start
  54. eg. running condition
  55. '''
  56. pass
  57.  
  58. def stop(self):
  59. '''
  60. Override to add logic before the stop
  61. eg. invalidating running condition
  62. '''
  63. pass
  64.  
  65. def main(self):
  66. '''
  67. Main class to be ovverridden to add logic
  68. '''
  69. pass
  70.  
  71. # entry point of the module: copy and paste into the new module
  72. # ensuring you are calling the "parse_command_line" of the new created class
  73. if __name__ == '__main__':
  74. SMWinservice.parse_command_line()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement