python程序封裝為win32服務的方法
更新時間:2021年03月07日 06:53:48 作者:kd丹妮兒
這篇文章主要為大家詳細介紹了python程序封裝為win32服務的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python程序封裝為win32服務的具體代碼,供大家參考,具體內容如下
# encoding=utf-8
import os
import sys
import winerror
import win32serviceutil
import win32service
import win32event
import servicemanager
class PythonService(win32serviceutil.ServiceFramework):
# 服務名
_svc_name_ = "PythonService1"
# 服務顯示名稱
_svc_display_name_ = "PythonServiceDemo"
# 服務描述
_svc_description_ = "Python service demo."
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
self.isAlive = True
def _getLogger(self):
import logging
import os
import inspect
logger = logging.getLogger('[PythonService]')
this_file = inspect.getfile(inspect.currentframe())
dirpath = os.path.abspath(os.path.dirname(this_file))
handler = logging.FileHandler(os.path.join(dirpath, "service.log"))
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
return logger
def SvcDoRun(self):
import time
self.logger.error("svc do run....")
try:
while self.isAlive:
self.logger.error("I am alive.")
time.sleep(1)
# 等待服務被停止
# win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
except Exception as e:
self.logger.error(e)
time.sleep(60)
def SvcStop(self):
# 先告訴SCM停止這個過程
self.logger.error("svc do stop....")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# 設置事件
win32event.SetEvent(self.hWaitStop)
self.isAlive = False
if __name__ == '__main__':
if len(sys.argv) == 1:
try:
src_dll = os.path.abspath(servicemanager.__file__)
servicemanager.PrepareToHostSingle(PythonService)
servicemanager.Initialize("PythonService", src_dll)
servicemanager.StartServiceCtrlDispatcher()
except Exception as e:
print(e)
#if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
#win32serviceutil.usage()
else:
win32serviceutil.HandleCommandLine(PythonService) # 參數(shù)和上述定義類名一致
#pip install pywin32
# 安裝服務
# python PythonService.py install
# 讓服務自動啟動
# python PythonService.py --startup auto install
# 啟動服務
# python PythonService.py start
# 重啟服務
# python PythonService.py restart
# 停止服務
# python PythonService.py stop
# 刪除/卸載服務
# python PythonService.py remove
# 在用戶變量處去掉python路徑,然后在環(huán)境變量加入python路徑
# C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywin32_system32;
# C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\Lib\site-packages\win32;
# C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\Scripts\;
#C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python全棧之文件函數(shù)和函數(shù)參數(shù)
這篇文章主要為大家介紹了Python的文件函數(shù)和函數(shù)參數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12
python基于watchdog庫實現(xiàn)文件系統(tǒng)監(jiān)控
Watchdog庫是Python中一個用于監(jiān)控文件系統(tǒng)變化的第三方庫,它能夠實時監(jiān)測文件或目錄的創(chuàng)建,修改,刪除等操作,下面我們來看看如何利用watchdog實現(xiàn)文件系統(tǒng)監(jiān)控吧2025-04-04
Python使用pypinyin實現(xiàn)中文拼音轉換
pypinyin是一個Python庫,用于將中文漢字轉換為拼音,這篇文章主要為大家詳細介紹了pypinyin的基本用法并探討其應用場景,需要的可以參考下2024-02-02
淺談keras通過model.fit_generator訓練模型(節(jié)省內存)
這篇文章主要介紹了淺談keras通過model.fit_generator訓練模型(節(jié)省內存),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python?pyasn庫解析和生成ASN.1數(shù)據(jù)結構
這篇文章主要介紹了Python?pyasn庫實現(xiàn)ASN.1數(shù)據(jù)結構的解析和生成實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

