python实现windows Service服务程序
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服务程序的更多相关文章
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
- Python 写Windows Service服务程序
1.需求 为什么要开发一个windows服务呢?之前做一个程序,必须要读取指定目录文件License, 因为其他程序也在读取这指定目录的License文件,且License不同时会修改License的 ...
- C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
C#Windows Service服务程序的安装/卸载.启动/停止 桌面客户端管理程序设计 关于Windows Service程序的安装与卸载如果每次使用命令行操作,那简直要奔溃了,太麻烦而且还容易出 ...
- Python3 写Windows Service服务程序
用Python开发Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,下载路径:https://pypi.org/project/pywin32/#files ...
- 使用Advanced Installer 13.1打包发布 Windows Service服务程序
原文: 使用Advanced Installer 13.1打包发布 Windows Service服务程序 项目中需要用到一个定时推送案件状态的需求,本人小菜一只,在同事建议下要写成一个windows ...
- C#Windows Service程序的创建安装与卸载
C#Windows Service程序的创建安装与卸载 一.开发环境 操作系统:Windows7x64 sp1 专业版 开发环境:Visual studio 2013 编程语言:C# .NET版本: ...
- C#写Windows Service(windows服务程序)
背景: 要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...
- C# 编写Windows Service(windows服务程序)【转载】
[转]http://www.cnblogs.com/bluestorm/p/3510398.html Windows Service简介: 一个Windows服务程序是在Windows操作系统下能完成 ...
- 让自己的C++程序(非服务程序)运行为一个windows service
因为项目的一些变化和原因,需要把数据处理的一个后台程序创建为一个windows服务,运行以下命令能创建成功: sc create "MyApp Service Name" binP ...
随机推荐
- pygame 入门实例
本文基于win7(64) + py3.5(64)环境. 本文是这里的一篇学习笔记.加入了自己的理解. 本文最终目的是实现一个飞机躲避导弹的游戏. 1.核心概念 pygame 的核心概念有: Surfa ...
- Redux系列x:源码解析
写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. ...
- ats 安全
Controlling Access ats可以配置为仅允许某些客户端使用代理缓存. 1. 为ip_allow.config添加一行,以获取允许访问ats的每个IP地址或IP地址范围; 2. traf ...
- 转-PHP 设计模式 之策略模式 应用场景 Strategy Pattern
一.前言 关于设计模式的文章,园子里实在是太多太多,而且讲解的也非常精彩,那为什么我还要在这里记录下这篇文章?本文以实际项目应用“自己动手写工具--XSmartNote”为切入点,来讲述策略模式的应用 ...
- java调用不了你本以为有的方法的问题
大部分是因为你的jar版本不对引起的,我就试过
- mysql左外连接
左外连接的概念性不说了,这次就说一说两个表之间的查询步骤是怎么样的? 例如 SELECT ut.id,ut.name,ut.age, ut.sex,ut.status,st.score,st.subj ...
- Iterative Quantization,ITQ
Abstract 针对大规模的图像检索问题,论文提出了一个高效的ITQ算法.该算法先将中心化后的数据映射到超立方体的顶点上,再通过优化过程寻找一个旋转矩阵,使得数据点经过旋转后,与超立方体的顶点数据具 ...
- Android控件第2类——ImageView
1.ImageView不仅仅可以显示图片,ImageView可以显示任何Drawable对象. adjustViewBounds:设置ImageView是否调整自己的边界来保证图片的长宽比. crop ...
- DevOps简介
DevOps 是一个完整的面向IT运维的工作流,以 IT 自动化以及持续集成(CI).持续部署(CD)为基础,来优化程式开发.测试.系统运维等所有环节. DevOps的概念 DevOps一词的来自于D ...
- 异构数据库之间完全可以用SQL语句导数据
告诉你一个最快的方法,用SQLServer连接DBF 在SQLServer中执行 SELECT * into bmk FROM OpenDataSource( ‘Microsoft.Jet.OLEDB ...