Python 实现windows后台服务
# -*- coding: utf-8 -*-
import sys
import win32api
import win32con
import win32event
import win32service
import win32serviceutil
import servicemanager
import logging
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
class HTTPFilerServer(win32serviceutil.ServiceFramework):
_svc_name_ = "HTTPFileServer"
_svc_display_name_ = "HTTP File Server"
_svc_description_ = "HTTP File Server"
_svc_data_dir = 'D:\\ScreenRecorder'
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.stop_event = win32event.CreateEvent(None, 0, 0, None)
self.logger = self._getLogger()
def _getLogger(self):
logger = logging.getLogger('[HTTPFileServer]')
dirpath = os.path.abspath(self._svc_data_dir)
handler = logging.FileHandler(os.path.join(dirpath, self._svc_name_ + ".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):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
try:
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
self.logger.info('HTTP File Server is Starting ...')
self.start()
self.logger.info('HTTP File Server Started')
import time
time.sleep(3)
os.chdir(self._svc_data_dir)
httpd = HTTPServer(('', 8080), CGIHTTPRequestHandler)
httpd.serve_forever()
win32event.WaitForSingleObject(self.stop_event, win32event.INFINITE)
self.logger.info('HTTP File Server Shutdown')
except BaseException as e:
self.logger.warn('Exception : %s' % e)
self.SvcStop()
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.logger.info('HTTP File Server is Stopping ...')
self.stop()
self.logger.info('HTTP File Server Service Stopped')
win32event.SetEvent(self.stop_event)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
def start(self):
if not os.path.exists(self._svc_data_dir):
os.mkdir(self._svc_data_dir)
win32api.SetFileAttributes(self._svc_data_dir, win32con.FILE_ATTRIBUTE_HIDDEN)
else:
pass
def stop(self):
pass
def log(self, msg):
servicemanager.LogInfoMsg(str(msg))
if __name__ == "__main__":
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(HTTPFilerServer)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(HTTPFilerServer)
Python 实现windows后台服务的更多相关文章
- 【JavaService】部署Java jar为Windows后台服务
将Java jar文件部署为Windows后台服务有多种方法:Service Installer.Java service Wrapper.JavaService.exe等等.这里介绍下使用JavaS ...
- 使用instsrv.exe和srvany.exe将应用程序安装成windows后台服务
好的参考链接: http://www.jb51.net/softjc/374631.html 利用这两个工具,将 exe程序或者 bat文件,做成Windows后台服务.
- 番外篇:使用nssm工具将ES、Kibana、Logstash或者其他.bat文件部署为Windows后台服务的方法
使用NSSM工具安装bat文件为Windows服务 nssm是一个可以把bat批处理文件部署为Windows服务的小工具.例如很多.net项目可能还是在Windows服务器上面跑的,但是很多组件只提供 ...
- python杀死Windows后台程序
检测 "sogou-gui.exe" 的进程可用tasklist命令 tasklist /FI "IMAGENAME eq sogou-gui.exe" FI: ...
- linux/windows下启用和停止VMware后台服务的脚本
linux/windows下启用和停止VMware后台服务的脚本 linux/windows下启用和停止VMware后台服务的脚本 linux平台 windows平台 本文由乌合之众 lym瞎编,欢迎 ...
- Python做windows服务
Python做windows服务(多进程服务),并结束多进程 Python中_,__,__xx__的区别 在注册MyWinService服务时,再使用 "sc delete 服务器名称&qu ...
- 在windows中:双击运行Python程序、后台运行Python程序
在windows中:双击运行Python程序.后台运行Python程序 安装Python解释器的windows环境,如果双击运行*.py的文件,会闪退.怎样避免闪退呢? 我们用python的日志输出程 ...
- redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(多机)
redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(多机) 一.概述 此教程介绍如何在windows系统中多个服务器之间,布置redis哨兵模式(主从复制),同时要以后台服务的模式运行 ...
- redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(单机)
redis在Windows下以后台服务一键搭建哨兵(主从复制)模式(单机) 一.概述 此教程介绍如何在windows系统中单机布置redis哨兵模式(主从复制),同时要以后台服务的模式运行.布置以脚本 ...
随机推荐
- 利用shell编程,部署项目到服务器
现在在前后端分离的开发形式中,每次前端将VUE项目打包之后,需要后端程序员部署到服务器上.这过程为何没有用git,因为每次vue打包后的文件都不相同与前一次打包,git为何的话,会包含过大迭代版本,同 ...
- webservice的cxf和spring整合发布
1.新建一个web项目 2.导入cxf相应的jar包,并部署到项目中 3.服务接口 package com.xiaostudy; /** * @desc 服务器接口 * @author xiaostu ...
- Spring boot 外部资源配置
tomcat配置访问图片路径映射到磁盘路径 首先,我在调试页面的时候发现,图片路径为: 1 /webapps/pic_son/img/1234565456.jpg 但是,tomcat中webapp ...
- C++中的指针和数组
最近看C++编程思想,看到第十三章动态内存管理的时候把自己给绕进去了,主要是在数据和指针这块弄混了.现在把找到的一些资料总结如下: 1. 数组是数组,指针是指针,两者并不等价: 2.数组在作为左值的时 ...
- python学习笔记glob模块
python有许多的类库,现将学习记录下来,以供以后回顾复习: 1.glob模块 用于文件名操作,匹配指定目录下的文件,返回的是目录加文件名,常用的有两个函数: glob(pattern),返回匹配的 ...
- replace()函数用法
replace()函数表示将用一个字符串替换字符串中的所出现的特定内容. 语法为:replace(字段1,字段2,字段3),意思为字段3将会替换字段1里与字段2相同的内容 列如: table1 st ...
- Linux权限控制
文件属性 权限说明 文件用户组调 权限设置建议 文件属性 在shell环境里输入:ls -l 可以查看当前目录文件.如: drwxr-xr-x. 14 root root 4096 Apr 5 18: ...
- NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [洛谷U62364]三次函数极值
U62364 三次函数极值 题面 给定一个三次函数\(f(x)=a_3x^3+a_2x^2+a_1x+a_0\) 求其极值. 格式 输入包括一行四个整数\(a_3,a_2,a_1,a_0\) 输出包括 ...
- 设计模式--观察者模式C++实现
观察者模式C++实现 1定义 Observer/Publish/subscribe发布订阅模式 定义对象间一种一对多的依赖关系,使得当一个对象改变状态时,所有依赖他的对象都能获得通知并被自动更新 2类 ...