日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python windows服务_Python创建Windows服务

發布時間:2023/12/19 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python windows服务_Python创建Windows服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先讓我們開始安裝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服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。