Python创建Windows 服务,python创建windows,Python 写wind
Python创建Windows 服务,python创建windows,Python 写wind
Python 写windows 服务,需要使用 pywin32包。
直接上代码:
#encoding=utf8'''Created on 2014-7-1@author: wangmengnan'''import osimport sysimport win32serviceutilimport win32serviceimport win32eventclass PythonService(win32serviceutil.ServiceFramework): #服务名 _svc_name_ = "PythonService" #服务显示名称 _svc_display_name_ = "Python Service Demo" #服务描述 _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 = loggint.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...") while self.isAlive: self.logger.error("I am alive.") time.sleep(1) def SvcStop(self): self.logger.error("svc do stop...") self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #设置事件 win32event.SetEvent(self.hWaitStop) self.isAlive = Falseif __name__ == '__main__': win32serviceutil.HandleCommandLine(PythonService)
程序写好后,需要对服务进行安装、以及启动等操作,命令如下:
安装服务:
python service.py install
让服务自动启动:
python service.py --startup auto install
启动服务:
python service.py start
重启服务:```python service.py restart
停止服务:```python service.py stop
删除/卸载服务
python service.py remove
安装并启动服务后,可以通过计算机->管理->服务和应用程序->服务里面找到我们自己写的服务,图片如下:
转载请注明出处:http://blog.csdn.net/sogouauto
相关内容
- urllib2下载文件,,#!/usr/bin/p
- 画折线图的程序,画折线图程序,#_*_coding:u
- python 去除html标签的代码,python去除html标签,#! /usr/bin
- python模拟按键,自动翻页看u17漫画,pythonu17,#-*- coding
- 地月关系统模拟图示,月关模拟图示,#!/usr/bin/e
- 蒙提霍尔问题,蒙提霍尔,#coding: utf
- python通过pil生成缩略图的简单代码,pythonpil,python通过
- python抓取图片示例,,#!/usr/bin/p
- 文件结构扁平化,结构扁平化,def flatten(
- Python计算-对列表元素分类统计,python计算列表元素,l
评论关闭