首先让我们开始安装Python for Windows扩展:

c:test>pip install pywin32

完成后,让我们编写该基类,您的Windows服务将是该基类的子类。

'''

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()

让我们检查一下我们刚刚介绍的课程。

def SvcDoRun(self):请求服务启动时将调用的方法。

def SvcStop(self):请求服务停止时将调用的方法。

def start(self):这是一种方法,在服务启动时(启动之前),您需要重写是否需要做某事

def stop(self):服务停止时(停止之前)是否需要执行某些操作,将要求您覆盖此方法

def main(self):这是将包含脚本逻辑的方法,通常是在循环中使其保持活动状态直到服务停止。

def parse_command_line(cls):这是处理命令行界面的方法,可用于安装和更新Windows服务

您能看到使用pywin32与系统交互以创建Windows服务有多么容易吗?最后提到的是以下变量:

svc_name = "PythonCornerExample"

svc_display_name = "Python Corner's Winservice Example"

svc_description = "That's a great winservice! :)"

这只是三个变量,其中包含服务的名称,“友好名称”(Windows将使用该名称在mmc控制台上显示名称)以及服务的简短说明。

一如既往,足够多的讨论,让我们编写一些有用的代码!

假设我们要创建一个Winservice,该Winservice在启动时每5秒在C:驱动器上创建一个随机文件。

什么?你觉得这很蠢吗?好了,将其安装在您的老板PC上,将目标文件夹设置为其用户的桌面,您将改变主意。:)

但是,如何才能达到这个结果呢?超级容易。

子类化我们刚刚遇到的SMWinservice类。

在新类上,覆盖三个变量_svc_name_,_svc_display_name_和_svc_description_。

覆盖“开始”方法以设置运行条件。设置一个布尔变量就足够了。

当请求停止服务时,重写“stop”方法以使运行状况无效。

覆盖“main”方法以添加每5秒创建一个随机文件的逻辑

将调用添加到“parse_command_line”函数以处理命令行界面。

结果应该是这样的:

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()

而已!现在是时候安装我们新创建的winservice了。只需打开命令提示符,导航到脚本目录并使用以下命令安装服务:

C:test>python PythonCornerExample.py install

Installing service PythonCornerExample

Service installed

将来,如果您想更改服务代码,只需对其进行修改并使用以下命令重新安装该服务

C:test>python PythonCornerExample.py update

Changing service configuration

Service updated

现在,打开“服务” m​​sc管理单元

C:test>mmc Services.msc

找到新的PythonCornerExamplewinservice,然后右键单击并选择属性。在这里,您可以启动服务并随意配置。

现在尝试启动服务,然后查看C:文件夹的内容。

您可以看到所有这些文件都已创建到C:文件夹吗?是的,这有效!

但是现在是时候停止它了!:)您可以在以前的窗口中执行此操作,也可以仅使用命令行来执行此操作

C:test>net stop PythonCornerExample

Il servizio Python Corner's Winservice Example sta per essere arrestato..

Servizio Python Corner's Winservice Example arrestato.

如果出问题了...

用Python编写Windows服务可能会发生几个已知的问题。如果您已经成功安装了该服务但启动了该服务,则会收到错误消息,请按照以下步骤对服务进行故障排除:

检查Python是否在您的PATH变量中。它必须在那里。要检查这一点,只需打开命令提示符,然后尝试通过键入“ python”来启动python解释器。如果开始,那很好。

确保具有该文件C:\Program Files\Python36\Lib\site-packages\win32\pywintypes36.dll(请注意,“ 36”是您的Python安装版本)。如果您没有此文件,请从中C:\Program Files\Python36\Lib\site-packages\pywin32_system32\pywintypes36.dll复制并复制到C:\Program Files\Python36\Lib\site-packages\win32

如果仍然有问题,请尝试以调试模式执行Python脚本。要在上一个示例中尝试此操作,请打开一个终端,导航到脚本所在的目录,然后键入

c:test>python PythonCornerExample.py debug

好的,今天就这些,这只是使用Python开发Windows服务的简短介绍。自己尝试一下,…快乐编码!

python windows服务_Python创建Windows服务相关推荐

  1. Android服务一 创建启动服务

    若要学习创建绑定服务,请查看下篇Android服务二 创建绑定服务 启动服务 基于Service package service;import android.app.Service; import ...

  2. python创_Python创建Windows 服务

    Python 写windows 服务,需要使用 pywin32包. 直接上代码: #encoding=utf8 ''' Created on 2014-7-1 @author: wangmengnan ...

  3. python 编程服务_Python编写Windows Service服务程序

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

  4. python开发服务程序_Python 编写Windows服务程序:将Python作为Windows服务启动 | 学步园...

    Python程序作为Windows服务启动,需要安装pywin32包.下载路径: #-*- coding:utf-8 -*- import win32serviceutil import win32s ...

  5. C# windows服务:创建Windows服务(Windows Services)的一般步骤

    C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...

  6. python控制系统进程_python 监控windows进程

    python os.startfile python实现双击运行程序 python监控windows程序 监控进程不在时重新启动 用python监控您的window服务 原创作品,允许转载,转载时请务 ...

  7. python环境下载_Python for Windows 64位下载

    PythonWin 64位是Windows 64为系统下的Python开发环境,由Python官方提供,Python是一种面向对象脚本语言,无论你是使用Python开发程序还是学习Python,第一步 ...

  8. Android服务二 创建绑定服务

    若对AIDL感兴趣,请参考跨进程通信二 AIDL 绑定服务 Ibinder实现绑定服务 功能:主要为了实现组件与服务的交互,在绑定的组件可以调用服务端的功能函数 package service;imp ...

  9. openstack 制作windows镜像,创建windows虚拟机,虚拟机添加virt-io驱动

    如果想要dashboard上创建一个Windows的虚拟机,就要有一个Windows的镜像,而Windows的虚拟机里需要virtio作为网卡驱动.所以需要下载virtio-win iso 准备: o ...

最新文章

  1. 日常生活小技巧 --WIN7出现“发生验证错误 要求的函数不受支持”问题
  2. 和gdi绘图效率比较_堪称效率神器!5款日常插件分享,错过哪一个都无比遗憾...
  3. 去医院看病如何开开心心出来? | 今日最佳
  4. VC2010 项目的创建
  5. 解决Element的 InfiniteScroll 无限滚动组件报错
  6. Python自动化运维_批量执行py脚本
  7. 安卓背景音乐开关_奢华大屏背景音乐系统-圣巴赫S820,行业典范!
  8. 官方 Material Design App
  9. 使用wsdl2java编写webservice客户端
  10. 想要定制专属AI声音?这是一份来自微软的保姆级攻略
  11. JSP汽车故障诊断系统myeclipse开发mysql数据库web结构java编程
  12. layui tree组件更改图标
  13. java读加密excel,JAVA读取加密的Excel文件
  14. 渗透测试常用工具-目标识别
  15. jdk,jre,jvm的包含关系
  16. 自动驾驶中的模型预测控制(Model Predictive Control)
  17. ssd重装系统的详细教程
  18. WebLogic 10.3.1 下载地址
  19. python输入输入:input、map
  20. 根据word自动生成html代码文件

热门文章

  1. 关于 SAP Spartacus CSR fallback 之后,是否仍然会继续进行 SSR 的处理
  2. SAP Spartacus的开发人员来自全球各地
  3. SAP Spartacus layout设计原理
  4. JSP的session处理
  5. How is call from CXF delegated to our application code?
  6. do filtering will real delete note in DB
  7. SAP UI5 global configuration interface
  8. SAP云平台Neo环境支持nodejs module吗
  9. GM6 frontend Shell._resolveHashFragment when tile is clicked
  10. COM_PRODUCT_READ_SINGLE called by composite API when line item is deleted