使用python编写的脚本应用程序,在运行的时候需要有python的运行环境,但是我们肯定是希望整个python程序能够像应用程序一样打包生成一个包括其运行环境的exe文件包,这是第一步,但是要想使用net start这样的方式启动, 还需要将该exe注册到服务里面去,使用exe install完成。
#SmallestService.py
#AsampledemonstratingthesmallestpossibleservicewritteninPython.
importwin32serviceutil
importwin32service
importwin32event
classSmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_="SmallestPythonService"
_svc_display_name_="ThesmallestpossiblePythonService"
def__init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
#Createaneventwhichwewillusetowaiton.
#The"servicestop"requestwillsetthisevent.
self.hWaitStop=win32event.CreateEvent(None,0,0,None)
defSvcStop(self):
#Beforewedoanything,telltheSCMwearestartingthestopprocess.
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
#Andsetmyevent.
win32event.SetEvent(self.hWaitStop)
defSvcDoRun(self):
#把你的程序代码放到这里就OK了
win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
if__name__=='__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService)