python 服务注册_python注册Windows服务
注册windows服务程序框架
安装pywin32
pip install pywin32
按照下述程序框架编写代码,业务逻辑写main函数里即可。
'''
SMWinservice
by Davide Mastromatteo
Base class to create winservice in Python
-----------------------------------------
Instructions:
1. Just create a new class that inherits from this base class
2. Define into the new class the variables
_svc_name_ = "nameOfWinservice"
_svc_display_name_ = "name of the Winservice that will be displayed in scm"
_svc_description_ = "description of the Winservice that will be displayed in scm"
3. Override the three main methods:
def start(self) : if you need to do something at the service initialization.
A good idea is to put here the inizialization of the running condition
def stop(self) : if you need to do something just before the service is stopped.
A good idea is to put here the invalidation of the running condition
def main(self) : your actual run loop. Just create a loop based on your running condition
4. Define the entry point of your module calling the method "parse_command_line" of the new class
5. Enjoy
'''
import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
class SMWinservice(win32serviceutil.ServiceFramework):
'''Base class to create winservice in Python'''
_svc_name_ = 'pythonService'
_svc_display_name_ = 'Python Service'
_svc_description_ = 'Python Service Description'
@classmethod
def parse_command_line(cls):
'''
ClassMethod to parse the command line
'''
win32serviceutil.HandleCommandLine(cls)
def __init__(self, args):
'''
Constructor of the winservice
'''
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
'''
Called when the service is asked to stop
'''
self.stop()
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
'''
Called when the service is asked to start
'''
self.start()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def start(self):
'''
Override to add logic before the start
eg. running condition
'''
pass
def stop(self):
'''
Override to add logic before the stop
eg. invalidating running condition
'''
pass
def main(self):
'''
Main class to be ovverridden to add logic
'''
pass
# entry point of the module: copy and paste into the new module
# ensuring you are calling the "parse_command_line" of the new created class
if __name__ == '__main__':
SMWinservice.parse_command_line()
编写注册业务逻辑
这个服务是每5秒,在C盘目录下创建一个文件
"""
PythonCornerExample.py
"""
import time
import random
from pathlib import Path
from SMWinservice import SMWinservice
class PythonCornerExample(SMWinservice):
_svc_name_ = "PythonCornerExample"
_svc_display_name_ = "Python Corner's Winservice Example"
_svc_description_ = "That's a great winservice! :)"
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
i = 0
while self.isrunning:
random.seed()
x = random.randint(1, 1000000)
Path(f'c:\\{x}.txt').touch()
time.sleep(5)
if __name__ == '__main__':
PythonCornerExample.parse_command_line()
安装服务
> python PythonCornerExample.py install
Installing service PythonCornerExample
Service installed
更新服务
> python PythonCornerExample.py update
Changing service configuration
Service updated
查看服务
> mmc Services.msc
停止服务
> net stop PythonCornerExample
常见问题
a. 检查Python执行路径是否在PATH变量中。可以在命令行窗口,输入python来确认。
b. 确认 C:\Program Files\Python36\Lib\site-packages\win32\pywintypes36.dll 存在(注意: “36” 是指python安装版本)。如果这个文件不存在,从C:\Program Files\Python36\Lib\site-packages\pywin32_system32\pywintypes36.dll 拷贝到上述目录下。
c. 仍旧存在问题,输入下列命令调试。
python PythonCornerExample.py debug
示例代码
python 服务注册_python注册Windows服务相关推荐
- python 打包成exe 1053_Python Windows服务pyinstaller可执行文件错误1053
我在 python中编写了一个 Windows服务.如果我从命令提示符运行我的脚本 python runService.py 当我这样做时,服务安装并正确启动.我一直在尝试使用pyinstaller创 ...
- WCF服务寄宿IIS与Windows服务 - C#/.NET
WCF是Windows平台下程序间通讯的应用程序框架.整合和 .net Remoting,WebService,Socket的机制,是用来开发windows平台上分布式开发的最佳选择.wcf程序的运行 ...
- win10注册mysql到windows服务报错:Install/Remove of the Service Denied
win10系统安装mysql的时候报错:Install/Remove of the Service Denied! 在cmd里输入:mysqld install MySQL8.0 会直接报错,无法按 ...
- 注册和启动windows服务
如何将exe注册为windows服务,直接从后台运行 方法一:使用windows自带的命令sc使用sc create 方法创建.如:sc create CaptureScreen binpath= F ...
- python windows服务_Python创建Windows服务
首先让我们开始安装Python for Windows扩展: c:test>pip install pywin32 完成后,让我们编写该基类,您的Windows服务将是该基类的子类. ''' S ...
- python创_Python创建Windows 服务
Python 写windows 服务,需要使用 pywin32包. 直接上代码: #encoding=utf8 ''' Created on 2014-7-1 @author: wangmengnan ...
- python开发服务程序_Python 编写Windows服务程序:将Python作为Windows服务启动 | 学步园...
Python程序作为Windows服务启动,需要安装pywin32包.下载路径: #-*- coding:utf-8 -*- import win32serviceutil import win32s ...
- python定时器 循环_Python循环定时服务功能(相似contrab)
Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...
- python句柄无效_作为Windows服务运行的Python:OSError:[WinError 6]句柄无效
subprocess.py中的第1117行是: p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE) 这让我怀疑服务流程没有与之关联的STD ...
最新文章
- rp-provide-from-last
- java01基础简介
- “约见”面试官系列之常见面试题之第八十二篇之MVC(建议收藏)
- LeetCode 542. 01 矩阵(BFS DP)
- C++引用与指针的不同
- Java 捕获 mybatis异常_3 springboot集成mybatis和全局异常捕获
- python中tell函数_PYTHON学习14.09:Python seek()和tell()函数详解
- 杭电1014 Uniform Generator
- 搜狗浏览器智慧版_4月浏览器份额榜单出炉:Edge登上第二、Chrome第一无人撼动...
- 生活随记 - 2020国庆第四天
- python图片分析中央气象台降水_python 画降水量色斑图问题
- 01.J2EE开发环境搭建
- 三星v版系统更新无法连接到服务器,美国V版三星S9/S9+迎来安卓9更新 展示新One UI用户界面...
- jsdroid 教程_电气设计编程视频教程,涉及PLC、电气绘图、仿真等共400多份资料...
- 安卓手机管理器_安卓平台上功能最全面强大的文件管理器!
- win10如何关闭自动更新及修改更新时间
- 装饰工程预结算教程电子书_干货集 | 栏杆百叶工程预结算总结
- cnpm使用报错-最佳方案
- 使用woboq_codebrowser工具以html形式浏览项目源码
- MMDetection亲测安装教程
热门文章
- elxel表格纸张尺寸_一本书的诞生:纸张知识
- 语言程序设计第4版黄洪艺_谭浩强《C程序设计》第4版网授精讲班【教材精讲+考研真题串讲】视频网课讲义课程资料...
- cad里面f命令用不了_CAD出现命令无效、失灵等问题?不用慌,两招帮你快速解决...
- CoreAnimation编程指南(一)概念
- Linux的实际操作:文件和文件夹的权限解读
- JAVA入门级教学之(myeclipse的使用)
- python 大学教授整理_剑桥大学教授用时35天亲自整理,Python超详细的基础笔记
- c语言线程经常段错误的是,由pthread_create引起的段异常
- mysql集群fuzhi_MySQL集群 和MySQL主从复制的不同
- 西安电子科技大学研究生计算机专业王宇平教授学生就业岗位,西安电子科技大学计算机学院导师信息情况...