1、首先要安装pywin32-220.win-amd64-py2.7.exe

2、

  SvcDoRun:服务启动的时候会执行的方法
  SvcStop:服务停止的时候会执行的方法
# coding=utf-8
import sys
import logging
from logging.handlers import RotatingFileHandler
import datetime
import os
import win32serviceutil
import win32service
import win32event
import time reload(sys)
sys.setdefaultencoding('utf-8') def log():
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
  # 必须要指定 sys.path[0] 得到的是这个文件所在的路径  如果不指定就跑到win32所在的路径下了
logPath = os.path.join(sys.path[0], 'log') if not os.path.exists(logPath):
os.makedirs(logPath)
handler = RotatingFileHandler(
os.path.join(logPath, '%s.txt' % datetime.date.today()), maxBytes=5 * 1024 * 1024, backupCount=10)
handler.setLevel(level=logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter)
logger.addHandler(handler) logger.error('error') class PythonService(win32serviceutil.ServiceFramework): # 服务名
_svc_name_ = "WinServiceTest"
# 服务显示名称
_svc_display_name_ = "WinServiceTest"
# 服务描述
_svc_description_ = "WinServiceTest description" def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcDoRun(self):
while True: # 要加while循环 否则服务只能运行1次 启动服务时有提示
log()
       #死循环 服务停止不了
       if win32event.WaitForSingleObject(self.hWaitStop, 5000) == win32event.WAIT_OBJECT_0:

          break

def SvcStop(self):
# 先告诉SCM停止这个过程
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# 设置事件
win32event.SetEvent(self.hWaitStop)
# 必须要加 否则会出现Python could not import the service's module 错误代码1
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(PythonService)

注意点:

  1)if __name__ == '__main__'必须要加,否则会报错。

  2)while True也要加,如果只想运行一次可以不加。会有提示,说只运行了1次。

Python之Windows服务的更多相关文章

  1. Python做windows服务

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

  2. 用python写windows服务

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

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

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

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

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

  5. python管理Windows服务

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

  6. 写一个Python的windows服务

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

  7. python实现编写windows服务

    使用python编写windows服务 最近测试服务器上经常发生磁盘空间不足,每次手动清除比较麻烦,所以写个windows服务定时清理下.中间也遇到过几个坑,一起记录下来. 1.python实现win ...

  8. Python搭建调用本地dll的Windows服务(浏览器可以访问,附测试dll64位和32位文件)

    一.前言说明 博客声明:此文链接地址https://www.cnblogs.com/Vrapile/p/14113683.html,请尊重原创,未经允许禁止转载!!! 1. 功能简述 (1)本文提供生 ...

  9. 一次使用Python连接数据库生成二维码并安装为windows服务的工作任务

    最近有一个需求,在现有生产系统上的人员库中增加一个此人员关键信息的二维码,支持文字版和跳转版两种方式,与报表工具关联,可打印.以windows服务方式,定时检查,只要发现某人员没有此二维码信息,就生成 ...

随机推荐

  1. 一篇文章了解_docker

    (一)Docker介绍 2018年10月6日 星期六 15:04 什么就Docker? Docker是一个开源项目, 诞生于2013年初,最初是dotCloud公司内部的一个业余项目.它基于Googl ...

  2. php include文件包含

    XCTF题目:Web_php_include <?php show_source(__FILE__);//高亮显示源文件 echo $_GET['hello']; $page=$_GET['pa ...

  3. [web安全原理分析]-文件上传漏洞基础

    简介 前端JS过滤绕过 待更新... 文件名过滤绕过 待更新 Content-type过滤绕过 Content-Type用于定义网络文件的类型和网页编码,用来告诉文件接收方以什么形式.什么编码读取这个 ...

  4. Linux学习 - 02 使用 - Centos8 - 『更换rpm/epel包源为国内源』

    1. Centos8 - 『更换rpm/epel包源为国内源』 centos 8 默认是会读取centos.org的mirrorlist的,所以一般来说是不需要配置镜像的. 如果你的网络访问mirro ...

  5. TCP的ACK机制

    下面是整个的tcp的三次握手和四次挥手的协议 TCP四次挥手 在客户端先发送一个FIN的包,表示要close(),客户端想和连接断开,发完之后出于FIN_WAIT_1状态下:服务端收到之后就变成CLO ...

  6. ABBYY FineReader 15 PDF文档查看功能

    PDF文档查看功能是ABBYY FineReader 15(Windows系统)OCR文字识别软件中PDF编辑器的一项基础功能,可供用户查看,搜索PDF文档,无需进入编辑模式,也可复制其中的文本,图片 ...

  7. MathType如何打出带圆圈的点

    学习了圆的知识后,知道可以用带圆圈的点表示圆,比如"⊙O"表示圆O,即圆心为O的圆.那么要怎么在MathType中打出带圆圈的点? 方法一.直接在工具栏上选择输入 打开MathTy ...

  8. 安装swoole扩展

    wget https://github.com/swoole/swoole-src/archive/v1.9.3-stable.tar.gz tar -zxvf v1.9.3-stable.tar.g ...

  9. recovery.sh

    #!/bin/bash source /etc/profile Time=`date +%F-%H-%M` Dir=/data/any.service.recovery if [ ! -d $Dir ...

  10. 再论<? extends T>和<? super T>

    参考: https://www.cnblogs.com/drizzlewithwind/p/6100164.html Java 泛型 <? super T> 中 super 怎么 理解?与 ...