1.环境设置
python要开发windows程序,首先必须先安装好pywin32,还要安装pyinstaller
pip install pywin32 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
2.编写代码,以下是通用的python编译成windows服务程序的模板,不要问我为什么,我也是网上找的,可在SvcDoRun编写自己的业务代码
#encoding=utf-8
import win32serviceutil
import win32service
import win32event
import os
import logging
import inspect
class PySerTest(win32serviceutil.ServiceFramework):
_svc_name_ = "PySerTest"
_svc_display_name_ = "Py Service Test"#服务显示的名称,可以自己修改
_svc_description_ = "This is a python service test code "//服务显示的描述
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.run = True
def _getLogger(self):
logger = logging.getLogger('[PythonService]')
this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
# handler = logging.FileHandler(os.path.join(dirpath, "service.log"))
handler = logging.FileHandler("G:\\service.log")
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
def SvcDoRun(self):
#在此编写自己的业务程序
import time
self.logger.info("service is run....")
while self.run:
self.logger.info("I am runing....")
time.sleep(2)
def SvcStop(self):
self.logger.info("service is stop....")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
self.run = False
if __name__=='__main__':
# win32serviceutil.HandleCommandLine(PythonService)
import sys
import servicemanager
if len(sys.argv) == 1:
try:
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(PySerTest) #如果修改过名字,名字要统一
servicemanager.Initialize('PySerTest',evtsrc_dll) #如果修改过名字,名字要统一
servicemanager.StartServiceCtrlDispatcher()
except win32service.error as details:
import winerror
if details == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(PySerTest) #如果修改过名字,名字要统一
3.假如保存之后的文件名为winService.py,运行以下命令即可生成exe程序
pyinstaller -F winService.py
4.编译成功后,可通过以下命令对服务进行控制
winService.exe install //仅安装服务程序
winService.exe --startup auto install //安装服务程序,并设置开机启动
winService.exe start //启动服务
winService.exe stop //停止服务
winService.exe remove //移除服务
下面是我自己创建的服务程序,可在系统服务里看