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. 【第十一课】Tomcat原理解析【转】

    目录 一.Tomcat顶层架构 二.Tomcat顶层架构小结: 三.Connector和Container的微妙关系 四.Connector架构分析 五.Container架构分析 六.Contain ...

  2. ECMAScript6——Set数据结构

    /** * 数据结构 Set */ // ----------------------------------------------------- /** * 集合的基本概念:集合是由一组无序且唯一 ...

  3. 手撸orm框架

    一 前言 1 我在实例化一个user对象的时候,可以user=User(name='lqz',password='123') 2 也可以 user=User() user['name']='lqz' ...

  4. ASP.NET Core采用Web Deploy方式发布到 Windows Server 2012 IIS上

    小白一枚,租个服务器,发布下网站,满足下好奇心,讲的粗糙,请大家谅解哈~ 1.配置服务器环境.这部分网上教程比较多. (1)开启IIS,参考其他博客的,担心的话,将Web服务器(IIS)全选哈~ (2 ...

  5. Unity程序协同问题,传送时屏幕变黑变亮的解决,常规操作的行为集合

    在unity中运行某段程序时往往需要运行另外一段不相干但是却对功能上有需求的程序,比如进行场景传送,在传送点处,点击I键,屏幕慢慢变黑,场景传送到另外一个场景,场景又慢慢变亮.这里首先涉及两个物体,一 ...

  6. Mac用户抓包软件Charles 4.0 破解 以及 抓取Https链接设置

    相信大家曾经都是Window的用户,作为前端哪能没有一款抓包工具,抓包工具可以非常便捷的帮助我们分析接口返回报文数据,快速定位问题. 曾经横扫window用户的Fiddler便是我们的挚爱,然而,作为 ...

  7. PHP中Cookie的使用

    1.什么是Cookie? Cookie保存在客户端浏览器中,cookie是Http头的一部分,通过浏览器请求页面时,它会被通过Http头的形式发送过去.被请求的页面,可以通过PHP来获取cookie的 ...

  8. 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web

    Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...

  9. chrome-extension & inject.js

    chrome-extension & inject.js chrome-extension://gppongmhjkpfnbhagpmjfkannfbllamg/js/inject.js in ...

  10. Semantic Versioning Specification & 语义化版本

    Semantic Versioning Specification & 语义化版本 Semantic Versioning Specification http://semver.org 16 ...