python实现windows Service服务程序

win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现。

  • 通过SvcDoRun方法,实现服务启动,运行服务内的业务代码。
  • 通过SvcStop方法,停止服务。

WinPollManager.py代码如下:

import win32serviceutil
import win32service
import win32event
import winerror
import servicemanager
import time
import sys
import os class WinPollManager(win32serviceutil.ServiceFramework):
"""
#1.安装服务
python WinPollManager.py install #2.让服务自动启动
python WinPollManager.py --startup auto install #3.启动服务
python WinPollManager.py start #4.重启服务
python WinPollManager.py restart #5.停止服务
python WinPollManager.py stop #6.删除/卸载服务
python WinPollManager.py remove
""" _svc_name_ = "py_agent_poll_manager" # 服务名
_svc_display_name_ = "py_agent_poll_manager" # 服务在windows系统中显示的名称
_svc_description_ = "python windows monitor agent" # 服务的描述 def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.isAlive = True
self._poll_intvl = 30 def SvcDoRun(self):
while self.isAlive:
print 'monitor testing'
time.sleep(self._poll_intvl) def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.isAlive = False if __name__ == '__main__':
if len(sys.argv) == 1:
try:
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(WinPollManager)
servicemanager.Initialize('WinPollManager', evtsrc_dll)
servicemanager.StartServiceCtrlDispatcher()
except win32service.error, details:
if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(WinPollManager) # 括号里参数可以改成其他名字,但是必须与class类名一致;

打包exe文件

# -*- coding: utf-8 -*-

"""
pip install pyinstaller
pyinstaller -F -w WinPollManager.py
"""
from PyInstaller.__main__ import run if __name__ == '__main__':
params = ['WinPollManager.py', '-F', '-c', '--icon=favicon.ico']
run(params)

打包成功后在dist目录下生成exe文件

执行方式

  • 安装服务  WinPollManager.exe install
  • 服务自动启动  WinPollManager.exe --startup auto install
  • 启动服务  WinPollManager.exe start
  • 重启服务  WinPollManager.exe restart
  • 停止服务  WinPollManager.exe stop
  • 删除/卸载服务  WinPollManager.exe remove

参考文章:

http://zhangweide.cn/archive/2013/windows-service-example-using-pyinstaller.html

http://www.cnblogs.com/dcb3688/p/4496934.html

http://blog.csdn.net/dysj4099/article/details/21896085

python实现windows Service服务程序的更多相关文章

  1. 使用Python写Windows Service服务程序

    1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...

  2. Python 写Windows Service服务程序

    1.需求 为什么要开发一个windows服务呢?之前做一个程序,必须要读取指定目录文件License, 因为其他程序也在读取这指定目录的License文件,且License不同时会修改License的 ...

  3. C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计

    C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...

  4. Python3 写Windows Service服务程序

    用Python开发Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,下载路径:https://pypi.org/project/pywin32/#files ...

  5. 使用Advanced Installer 13.1打包发布 Windows Service服务程序

    原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...

  6. C#Windows Service程序的创建安装与卸载

    C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...

  7. C#写Windows Service(windows服务程序)

    背景:        要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...

  8. C# 编写Windows Service(windows服务程序)【转载】

    [转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...

  9. 让自己的C++程序(非服务程序)运行为一个windows service

    因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...

随机推荐

  1. Ubuntu 守护进程

    项目中用的Qt开发的GUI程序,需要随机自启动. 最初尝试过使用SuperVisor,但是会出现下面的错误. qt.qpa.screen: QXcbConnection: Could not conn ...

  2. 机器学习之线性回归使用Python和tensorflow实现

    导入依赖包 import tensorflow as tf import numpy as np import matplotlib.pylab as plt from pylab import mp ...

  3. 2017中国人工智能公司榜单发布,颠覆AT的AI独角兽全在这

    每年12月,创业邦研究中心评选并报道“中国创新成长企业100强”,这个榜单我们已经连续做了8年,是中国最有潜力的创业新贵榜,受到了业内广泛认可.从2015年开始我们发现,人工智能上榜企业明显增多,20 ...

  4. JAVA每日一旅2

    1.关于类型转换 两个数值进行二元操作时,会有如下的转换操作: 如果两个操作数其中有一个是double类型,另一个操作就会转换为double类型. 否则,如果其中一个操作数是float类型,另一个将会 ...

  5. linux第四章笔记

    第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间.进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统. 最大限度利用处理器时间的原则:只要有可以执行 ...

  6. 常用校验码(奇偶校验码、海明校验码、CRC校验码)

    一.奇偶校验码 二.海明校验码 三.CRC校验码   计算机系统运行时,各个部之间要进行数据交换.交换的过程中,会有发生误码的可能(即0变成1或1变成0),由于计算机的储存是通过二进制代码来实现的的, ...

  7. 基于Struts2+Hibernate的朋友圈留言网站开发的质量属性

    在课堂上我们了解了质量高于功能,质量属性是指影响质量的相关因素,是对质量的描述.现在我们对基于SSH的交友网站开发的质量属性可以从以下几个方向进行分析: (1)可用性: 当错误发生时,使用可用性战术进 ...

  8. 05 方法与数组笔记【JAVA】

    ---恢复内容开始--- 1:方法(掌握) (1)方法:就是完成特定功能的代码块. 注意:在很多语言里面有函数的定义,而在Java中,函数被称为方法. (2)格式: 修饰符 返回值类型 方法名(参数类 ...

  9. Photoshop一些常用的快捷键

    1.按住Alt键,点击图层上的小眼睛,只显示当前图层 2.新建纯色图层,抠图 3.Fn+ num 调整不透明度 4.Shift + num 调整流量 5.调整图层透明度,在移动工具状态下,输入数字:0 ...

  10. Beta阶段冲刺四

    Beta阶段冲刺四 Task1:团队TSP 团队任务 预估时间 实际时间 完成日期 新增其他学院的爬虫 180 130 11.30 新增其他学院的数据库字段修改 180 160 12.1 新增其他学院 ...