Python在windows下的服务程序
Python程序作为Windows服务启动,需要安装pywin32包。下载路径:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import win32serviceutil
import win32service
import win32event
class PythonService(win32serviceutil.ServiceFramework):
"""
Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]'
Options for 'install' and 'update' commands only:
--username domain\username : The Username the service is to run under
--password password : The password for the username
--startup [manual|auto|disabled|delayed] : How the service starts, default = manual
--interactive : Allow the service to interact with the desktop.
--perfmonini file: .ini file to use for registering performance monitor data
--perfmondll file: .dll file to use when querying the service for
performance data, default = perfmondata.dll
Options for 'start' and 'stop' commands only:
--wait seconds: Wait for the service to actually start or stop.
If you specify --wait with the 'stop' option, the service
and all dependent services will be stopped, each waiting
the specified period.
"""
#服务名
_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 = 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....")
while self.isAlive:
self.logger.error("I am alive.")
time.sleep(1)
# 等待服务被停止
#win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
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__':
win32serviceutil.HandleCommandLine(PythonService)
|
- 安装服务
1
|
python PythonService.py install
|
- 让服务自动启动
1
|
python PythonService.py --startup auto install
|
- 启动服务
1
|
python PythonService.py start
|
- 重启服务
1
|
python PythonService.py restart
|
- 停止服务
1
|
python PythonService.py stop
|
- 删除/卸载服务
1
|
python PythonService.py remove
|
Python在windows下的服务程序的更多相关文章
- python 在windows下的 虚拟环境
解决 python 环境问题 windows 下安装 pip install virtualenv virtualenv的基本使用 1.1 创建虚拟环境 virtualenv venv 为环境指定Py ...
- Python调用windows下DLL详解
Python调用windows下DLL详解 - ctypes库的使用 2014年09月05日 16:05:44 阅读数:6942 在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分 ...
- python 在windows下监听键盘按键
python 在windows下监听键盘按键 使用到的库 ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数) 使用的Win32API SetWindowsHookEx(), ...
- Python在windows下的安装与配置
安装python 文件准备: A. python安装文件:我用的是python-3.4.3.amd64.msi: 安装很简单,直接双击点下一步即可: 配置环境变量,在windows系统变量中找到pat ...
- Python 在Windows下安装matplotlib
windows下安装很麻烦,使用easy_install 安装报错 提示缺少freetype 和png 后经多方查询,最终安装成功 以下是安装过程 前提你的Python环境已经搭建好了 1.前提需要 ...
- Python监控Windows下的文件变化
windows下监控文件系统的变化.用python非常方便.实例代码例如以下,非常easy.也不多说了. import os import win32file import win32con ACTI ...
- 【python】 Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
刚在机器上windows环境下装上pip方便以后安装包的时候使用,谁知道第一次使用pip安装asyncio的时候就报错. 在Windows7x64下使用pip安装包的时候提示报错:Microsoft ...
- python在windows下连接mysql数据库
一,安装MySQL-python python 连接mysql数据库需要 Python interface to Mysql包,包名为 MySQL-python ,PyPI上现在到了1.2.5版本.M ...
- python学习:Windows 下 Python easy_install 的安装
Windows 下 Python easy_install 的安装 下载安装python安装工具下载地址:http://pypi.python.org/pypi/setuptools 可以找到 ...
随机推荐
- ubuntu系统安装和配置
1.分区信息 1.1 /boot分区 这个分区包括了操作系统的内核和在启动系统过程中所要用到的文件.假设有了一个单独的/boot启动分区,即使基本的根分区出现了问题,计算机依旧可以启动.这个分区的大小 ...
- 【u208】修复公路
Time Limit: 1 second Memory Limit: 128 MB [问题描述] A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 给出A地区的村庄数 ...
- 利用SendMessage实现窗口拖动
原文:利用SendMessage实现窗口拖动 利用SendMessage实现窗口拖动 周银辉 想想以前用跟踪鼠标位 ...
- 远程ssh执行命令时提示找不到命令
最开始的时候碰到这种问题,是在hadoop003上配置了jdk1.8, 在hadoop002上执行ssh hadoop003 java -version提示没有命令,先ssh hadoop003然后执 ...
- 共享Session
概述 现在的大型网站中,会面临实现多台服务器中的session数据共享问题.当使用多台服务器架设成集群之后,我们通过负载均衡的方式,同一个用户(或者ip)访问时被分配到不同的服务器上,假设在A服务器登 ...
- linux下安装sqlite3
1.介绍:sqlite3是linux上的小巧的数据库,一个文件就是一个数据库.2.安装: 要安装sqlite3,可以在终端提示符后运行下列命令: sudo apt-get install sqli ...
- WPF 自定义控件的坑(蠢的:自定义控件内容不显示)
原文:WPF 自定义控件的坑(蠢的:自定义控件内容不显示) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/detai ...
- SQL Server 统计某个月周末的天数
---注意:这里统计的周末包括周5,周6,但不包括周日ALTER FUNCTION [dbo].[GetWeekDaysByMonth] ( @Year INT, @Month INT, @Day I ...
- C#委托五(自定义事件)
事件: "在发生其他类或对象关注的事情时,类或对象可以通过事件通知他们.发送(或引发)事件的类称为"发行者",接受(或处理)事件的类称为"订户".&q ...
- 在动态THML语句中调用JS函数传递带空格参数的问题
刚刚遇到一个问题,调用js函数的参数里带空格,造成调用失败的问题. 部分代码如下: html+="<div><a href=javascript:confirm(&qu ...