用python写windows服务(1)

以python2.5 为例
需要软件

* python 2.5
        * pywin32(与2.5 版本相匹配的)

Service Control Manager (SCM)

服务管理器(SCM) 是windows NT的 一部分,所有服务必须通过SCM 注册,SCM负责启动,停止服务等。

当一个进程通过SCM注册后, 有如下特质:

* 运行该进程的用户,未必是当前登录的用户。
        * 该进程如果依赖其他服务,哪么该服务启动前,依赖服务回启动。该服务停止后,依赖服务会停止。(估计是应用计数减1)
        * 服务可知计算机启动后自动启动,或者手动启动。

windows NT 通过执行一个进程开始相应服务。一旦这个进程执行,它需要告知SCM它实际上是作为一个服务运行。还需要传给SCM一个控制句柄(control handler)。其实就是一个函数,用于处理SCM 发来的相关信息。 当服务被停止时, SCM传信息给控制句柄。服务本身负责处理该请求,并停止本身服务。
pywin32 服务相关module

* win32service 实现了Win32服务功能。
        * win32serviceutil 对api的包装,始面向用户的接口更友好。
        * PythonService.exe 使用pywin32 服务器,它必须先注册。

下面重点讲 win32serviceutil
服务框架类

win32serviceutil.ServiceFramework

__init__

构造函数,注册ServiceCtrlHandler给SCM

ServiceCtrlHandler

本服务的control handler 的默认实现。该函数会查询类内的函数名,用以判断该服务提供哪些控制接口,比如类内有SvcPause 函数。则会认为该服务可以被暂停。

SvcRun

服务入口点。服务运行,就是运行这个函数。

简单示例
代码:

# SmallestService.py
#
# A sample demonstrating the smallest possible service written in Python. import win32serviceutil
import win32service
import win32event class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "SmallestPythonService"
_svc_display_name_ = "The smallest possible Python Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self):
# Before we do anything, tell the SCM we are starting the stop process.
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# And set my event.
win32event.SetEvent(self.hWaitStop) def SvcDoRun(self):
# We do nothing other than wait to be stopped!
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) if __name__=='__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService)

安装服务
进入PythonService.exe所在目录, 命令行下执行命令

(PATH)>PythonService.exe /register
Registering the Python Service Manager...
安装服务

C:\Scripts> SmallestService.py install
Installing service SmallestPythonService to Python class
   C:\Scripts\SmallestService.SmallestPythonService
Service installed
C:\Scripts>
启动服务

C:\Scripts> python.exe SmallestService.py start
Starting service SmallestPythonService
C:\Scripts>
启动确认

C:\Scripts> python.exe SmallestService.py start
Starting service SmallestPythonService
Error starting service: An instance of the service is already running.
C:\Scripts>
停止服务

C:\Scripts> python.exe SmallestService.py stop
Stopping service SmallestPythonService
C:\Scripts>

用python写windows服务的更多相关文章

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

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

  2. 不用写Windows服务实现定时器功能(FluentScheduler )

    MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲击与影响,那这个世界永远像现在不变就行了,大家都好好的,待在自 ...

  3. Python做windows服务

    Python做windows服务(多进程服务),并结束多进程 Python中_,__,__xx__的区别 在注册MyWinService服务时,再使用 "sc delete 服务器名称&qu ...

  4. 写一个Python的windows服务

    1. 安装pywin32和pyinstaller pip install pywin32 pip install pyinstaller 2.写一个服务Demo # -*- coding: utf-8 ...

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

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

  6. Python-windows服务-重启自动化

    一. 前言 有了上一篇的“python初学”的基础,咱们就有了python的开发包,有了开发环境IDE,那我们就可以干活了.我的第一个选题就是让我们的windows服务可以按照我们的意愿进行自动重启. ...

  7. python管理Windows服务

    上一篇介绍了pywin32模块,它的win32service子模块提供了对服务管理API的包装,相关API如下: ChangeServiceConfig ChangeServiceConfig2 Cl ...

  8. c#写windows服务

    序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...

  9. Python 写Windows Service服务程序

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

随机推荐

  1. BUPT复试专题—树查找(2011)

    https://www.nowcoder.com/practice/9a10d5e7d99c45e2a462644d46c428e4?tpId=67&tqId=29641&rp=0&a ...

  2. angular 的 GET 请求 和 POST 请求的 区别 及 实现

    1.GET 请求 .factory('AlarmService', ['$rootScope','ENV','$resource','$http','ionicToast',function($roo ...

  3. MYSQL时间戳的处理

    date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值格式化 例如: mysql>SELECT FROM ...

  4. jk_proxy实现apache+tomcat负载均衡

    Apache + tomcat实现server集群 主要參照:http://blog.csdn.net/welun521/article/details/4169879 watermark/2/tex ...

  5. 基于PHP函数的alert弹框

    可以设置弹出信息,跳转地址,跳转的时间,跳转的信息标题提示: 手机端加上<meta name='viewport' content='width=device-width, initial-sc ...

  6. Ajax的简单实现(JQuary)

    还是之前的例子,相对来说,用JQ就简单了很多,真的多,因为JQ直接把方法都写好了,直接调用就行了,,ԾㅂԾ,, php文件就不需要多做修改了,如下: <?php //改变Content-Type ...

  7. MUI-折叠面板效果accordion

    在做开发的过程中我们经经常使用到折叠面板. 那我们来看下折叠面板到底是怎么使用. 废话不多说. 代码粘下来: <!DOCTYPE html> <html> <head&g ...

  8. SpringInAction4笔记——装配

    重点:常用的上下文环境 AnnotationConfigApplicationContext ClassPathXmlApplicationContext FileSystemXmlApplicati ...

  9. em和i , b和Strong 的区别

    这两对标签最大区别就是一个给搜索引擎看的,一个是给用户看的. b标签和strong标签给我们的主观感受都是加粗,但对搜索引擎来说b标签和普通的文字并没有什么区别,而strong标签却是起强调作用的. ...

  10. web中使用svg失量图形及ie8以下浏览器的处理方式

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...