1.背景

如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,自己去下载然后安装(注意下载符合自己OS的版本)

2.实例

先上代码

#encoding=utf-8
import win32serviceutil
import win32service
import win32event
import os
import logging
import inspect
import servicemanager class PythonService(win32serviceutil.ServiceFramework): _svc_name_ = "PythonService" #服务名
_svc_display_name_ = "Python Service Test" #服务在windows系统中显示的名称
_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")) 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.run = False if __name__=='__main__':
if len(sys.argv) == 1:
try:
evtsrc_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(PythonService)
servicemanager.Initialize('PythonService', evtsrc_dll)
servicemanager.StartServiceCtrlDispatcher()
except win32service.error, details:
if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(PythonService)

解释一下代码:

1).在类PythonService的__init__函数执行完后,系统服务开始启动,windows系统会自动调用SvcDoRun函数,这个函数的执行不可以结束,因为结束就代表服务停止。所以当我们放自己的代码在SvcDoRun函数中执行的时候,必须确保该函数不退出。

2).当停止服务的时候,系统会调用SvcDoStop函数,该函数通过设置标志位等方式让SvcDoRun函数退出,就是正常的停止服务。例子中是通过event事件让SvcDoRun函数停止等待,从而退出该函数,从而使服务停止。系统关机时不会调用SvcDoStop函数,所以这种服务是可以设置为开机自启的。

3.服务操作命令

#1.安装服务

python PythonService.py install

#2.让服务自动启动

python PythonService.py --startup auto install 

#3.启动服务

python PythonService.py start

#4.重启服务

python PythonService.py restart

#5.停止服务

python PythonService.py stop

#6.删除/卸载服务

python PythonService.py remove

4.使用pyinstaller打包exe

pyinstaller.exe -F -c winService.py

效果:

5.管理windows服务操作

#!/usr/bin/env python
# -*- coding: UTF8 -*-
#
import win32service
import win32con
import time, sys
import datetime
reload(sys)
sys.setdefaultencoding("utf8")
class ServiceManager(object):
"""管理window服务""" def __init__(self, name):
"""
name: 服务的名称
"""
self.name = name #启动或停止服务时等待操作成功等待时间
self.wait_time = 0.5
#启动或停止服务时最大等待时间,超过时返回超时提示
self.delay_time = 10
self.scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_ALL_ACCESS) if self.is_exists():
try:
self.handle = win32service.OpenService(self.scm, self.name, win32service.SC_MANAGER_ALL_ACCESS)
except Exception, e:
self.log(e)
else:
print '服务 %s 没有安装'.encode('gbk') % self.name def is_stop(self):
"""检查服务是否停止"""
flag = False
try:
if self.handle:
ret = win32service.QueryServiceStatus(self.handle)
flag = ret[1] != win32service.SERVICE_RUNNING
except Exception, e:
self.log(e)
return flag def start(self):
"""开启服务"""
try:
if self.handle:
win32service.StartService(self.handle, None)
except Exception, e:
self.log(e)
status_info = win32service.QueryServiceStatus(self.handle) if status_info[1] == win32service.SERVICE_RUNNING:
return '启动服务%s成功'.encode('gbk') % self.name
elif status_info[1] == win32service.SERVICE_START_PENDING:
#如果服务正在启动中则延迟返回启动信息,直到启动成功,或返回启动时间过长信息
start_time = datetime.datetime.now()
while True:
if (datetime.datetime.now() - start_time).seconds > self.delay_time:
return '启动服务%s时间太长'.encode('gbk') % self.name time.sleep(self.wait_time)
if win32service.QueryServiceStatus(self.handle)[1] == win32service.SERVICE_RUNNING:
return '启动服务%s成功'.encode('gbk') % self.name
else:
return '启动服务%s失败'.encode('gbk') % self.name def stop(self):
"""停止服务"""
try:
status_info = win32service.ControlService(self.handle, win32service.SERVICE_CONTROL_STOP)
except Exception, e:
self.log(e)
if status_info[1] == win32service.SERVICE_STOPPED:
return '停止服务%s成功'.encode('gbk') % self.name
elif status_info[1] == win32service.SERVICE_STOP_PENDING:
start_time = datetime.datetime.now()
while True:
if (datetime.datetime.now() - start_time).seconds > self.delay_time:
return '停止服务%s时间太长'.encode('gbk') % self.name time.sleep(self.wait_time)
if win32service.QueryServiceStatus(self.handle)[1] == win32service.SERVICE_STOPPED:
return '停止服务%s成功'.encode('gbk') % self.name
else:
return '停止服务%s失败'.encode('gbk') % self.name def restart(self):
"""重启服务"""
if not self.is_stop():
self.stop()
self.start()
return win32service.QueryServiceStatus(self.handle) def status(self):
"""获取运行的状态"""
try:
status_info = win32service.QueryServiceStatus(self.handle)
status = status_info[1]
if status == win32service.SERVICE_STOPPED:
return "STOPPED"
elif status == win32service.SERVICE_START_PENDING:
return "STARTING"
elif status == win32service.SERVICE_STOP_PENDING:
return "STOPPING"
elif status == win32service.SERVICE_RUNNING:
return "RUNNING"
except Exception, e:
self.log(e) def close(self):
"""释放资源"""
try:
if self.scm:
win32service.CloseServiceHandle(self.handle)
win32service.CloseServiceHandle(self.scm)
except Exception, e:
self.log(e) def is_exists(self):
"""windows服务是否已安装"""
statuses = win32service.EnumServicesStatus(self.scm, win32service.SERVICE_WIN32, win32service.SERVICE_STATE_ALL)
for (short_name, desc, status) in statuses:
if short_name == self.name:
return True
return False def log(self, exception): print(exception) if __name__=='__main__': app= ServiceManager('PythonService')
msg= app.is_exists() # 判断是否安装 (以下操作必须先判断服务是否存在)
#msg= app.is_stop() # 判断服务是否停止
#msg= app.status() # 查看服务的状态
#msg= app.start() # 开启服务
#msg= app.stop() # 暂停服务 (服务开启才能停止,else error)
#msg= app.restart() # 重启服务 print(msg)

使用Python写Windows Service服务程序的更多相关文章

  1. Python 写Windows Service服务程序

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

  2. python实现windows Service服务程序

    python实现windows Service服务程序 win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现. 通过SvcDoR ...

  3. Python3 写Windows Service服务程序

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

  4. 用python写windows服务

    用python写windows服务(1) 以python2.5 为例需要软件 * python 2.5        * pywin32(与2.5 版本相匹配的) Service Control Ma ...

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

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

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

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

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

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

  8. 【python】使用python写windows服务

    背景 运维windows服务器的同学都知道,windows服务器进行批量管理的时候非常麻烦,没有比较顺手的工具,虽然saltstack和ansible都能够支持windows操作,但是使用起来总感觉不 ...

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

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

随机推荐

  1. 这交互炸了:饿了么是怎么让Image变成详情页的

    这交互炸了:饿了么是怎么让Image变成详情页的 晚上叫外卖,打开饿了么,发现推了一个版本,更新以后,点开了个鸡腿,哇,交互炫炸了. 本文同步自wing的地方酒馆 不过还是有槽点.我是无意中才发现可以 ...

  2. iOS之自定义控件

    一.使用纯代码方式 initWithFrame:中添加子控件 layoutSubViews中设置子控件的fame 对外设置数据接口,重写setter方法给子控件设置数据显示 在的viewControl ...

  3. sql server 对象资源管理器(二)

    SQL会缓存大量的数据页面,他还会缓存很多其他信息,包括存储过程的执行计划 ,特定用户的安全上下文等 如果这些信息没有在数据库中缓存,SQL都要重新计算一遍,花额外的时间,所以SQLSERVER对内存 ...

  4. 圣诞老人去哪?Power BI告诉你

    随着圣诞节的来临,微软的Power BI团队使用Power BI来回答大家一直以来所关心的问题:圣诞老人去哪? 要回答这个问题,来自社交网络的数据是最合适不过的了.于是Power BI团队用以下关键字 ...

  5. 11g新特性-dba_users安全性的一些增强

    1.dba_user表的password(除了GLOBAL和EXTERNAL的密码)不再保存密码. 查询10g的dba_user表 SQL> select username,password f ...

  6. 【CentOS】学习Bash

    一.特性 命令历史  history , !! , !$ , !字符 , !n (n为第几条命令) Tab可以补全文件路径或者命令 alias a="b",  unalias a ...

  7. python base64的加密与解密

    Base64编码是一种“防君子不防小人”的编码方式.广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符. 优点:速度快,ascii字符, ...

  8. html学习第二天—— 第七章——CSS样式基本知识

    外部式css样式,写在单独的一个文件中外部式css样式(也可称为外联式)就是把css代码写一个单独的外部文件中,这个css样式文件以“.css”为扩展名,在<head>内(不是在<s ...

  9. js整理4

    异步处理 错误处理 同步 function A() { B(); } function B() { C(); } function C() { throw new Error('something h ...

  10. Websocket简单例子

    websocket是Html5的一个协议,也就是说距离我们2016年就几年时间,其他原理我就不说了,直接讲例子 一.准备材料:1.一个开发工具必须支持javaEE7的,原因是javaEE6或以下不支持 ...