Python 实现windows后台服务,python后台,# -*- codi
Python 实现windows后台服务,python后台,# -*- codi
# -*- coding: utf-8 -*-import sysimport win32apiimport win32conimport win32eventimport win32serviceimport win32serviceutilimport servicemanagerimport loggingimport osfrom http.server import HTTPServer, CGIHTTPRequestHandlerclass 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后台服务
相关内容
- pycharm+QT5+python3安装与环境配置,,开发环境:MAC O
- Python iter() 函数,pythoniter函数,iter() 函数用
- python日常—爬取豆瓣250条电影记录,python豆瓣250条,#
- Python之如何删除pandas DataFrame的某一/几列,,删除pandas
- xlwt:python的写excel模块,xlwtpython,最近工作时碰到了将
- 【Python3练习题 019】 有一分数序列:2/1,3/2,5/3,8/5,
- python实现原地刷新方式输出-可用于百分比进度显示输出
- Windows下 Python Selenium PhantomJS 抓取网页并截图,,安装P
- python---await/async关键字,,推文:玩转 Pyth
- python-for循环与while循环,,while 循环格式
评论关闭