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
 
 
 
 
http://daodaoliang.com/Python%E5%AD%A6%E4%B9%A0/2015/06/09/Python%E5%AD%A6%E4%B9%A0-2015-06-09-python%E5%9C%A8windows%E4%B8%8B%E7%9A%84%E6%9C%8D%E5%8A%A1/

Python在windows下的服务程序的更多相关文章

  1. python 在windows下的 虚拟环境

    解决 python 环境问题 windows 下安装 pip install virtualenv virtualenv的基本使用 1.1 创建虚拟环境 virtualenv venv 为环境指定Py ...

  2. Python调用windows下DLL详解

    Python调用windows下DLL详解 - ctypes库的使用 2014年09月05日 16:05:44 阅读数:6942 在python中某些时候需要C做效率上的补充,在实际应用中,需要做部分 ...

  3. python 在windows下监听键盘按键

    python 在windows下监听键盘按键 使用到的库 ctypes(通过ctypes来调用Win32API, 主要就是调用钩子函数) 使用的Win32API SetWindowsHookEx(), ...

  4. Python在windows下的安装与配置

    安装python 文件准备: A. python安装文件:我用的是python-3.4.3.amd64.msi: 安装很简单,直接双击点下一步即可: 配置环境变量,在windows系统变量中找到pat ...

  5. Python 在Windows下安装matplotlib

    windows下安装很麻烦,使用easy_install 安装报错  提示缺少freetype 和png 后经多方查询,最终安装成功 以下是安装过程 前提你的Python环境已经搭建好了 1.前提需要 ...

  6. Python监控Windows下的文件变化

    windows下监控文件系统的变化.用python非常方便.实例代码例如以下,非常easy.也不多说了. import os import win32file import win32con ACTI ...

  7. 【python】 Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat

    刚在机器上windows环境下装上pip方便以后安装包的时候使用,谁知道第一次使用pip安装asyncio的时候就报错. 在Windows7x64下使用pip安装包的时候提示报错:Microsoft ...

  8. python在windows下连接mysql数据库

    一,安装MySQL-python python 连接mysql数据库需要 Python interface to Mysql包,包名为 MySQL-python ,PyPI上现在到了1.2.5版本.M ...

  9. python学习:Windows 下 Python easy_install 的安装

    Windows 下 Python easy_install 的安装     下载安装python安装工具下载地址:http://pypi.python.org/pypi/setuptools 可以找到 ...

随机推荐

  1. 集群搭建Solr

    Solr集群搭建 SolrCloud需要solr基于zookeeper部署,zookeeper是一个集群管理软件,由于SolrCloud需要由多台服务器组成.由zookeeper来进行协调管理.Zoo ...

  2. Erlang 位串和二进制数据

    http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=25876834&id=3300393 因为在本人工作中,服务端Erla ...

  3. easyexcel 读写测试

    <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId> ...

  4. 关于Dagger 2的文章汇总

    首先是我真正看懂了的第一篇文章 代码GG之家的 Dagger2图文详解 这篇文章很直接,还配有代码demo.至少我是看懂了. Dagger2 使用详解 这篇文章同样配有demo,同时文末还有很多有用的 ...

  5. hadoop 3.x 服役 | 退役数据节点

    在服役前要配置好新增主机的环境变量,ssh等信息,个人环境介绍 hadoop002(namenode),hadoop003(resourcemanager),hadoop004(secondaryna ...

  6. hadoop 3.x 无法访问hdfs(50070,8088)的web界面

    1.启动hadoop.然后netstat -nltp|grep 50070,如果,没有找到进程,说明没有配置web界面的端口修改hdfs-site,xml中加上如下配置 再次启动后,netstat - ...

  7. 配置ANDROID_HOME

    原文:配置ANDROID_HOME 1.在环境变量中设置一个名为ANDROID_HOME,变量值为SDK路径 2.添加至Path中 备注:ANDROID_HOME的变量值仅允许一个

  8. Spring MVC的RequestContextHolder使用误区 good

    JShop简介:jshop是一套使用Java语言开发的B2C网店系统,致力于为个人和中小企业提供免费.好用的网店系统. 项目主页:http://git.oschina.net/dinguangx/js ...

  9. java基础篇---文件上传(组件)

    转载自:http://www.cnblogs.com/oumyye/p/4234969.html 文件上传几乎是所有网站都具有的功能,用户可以将文件上传到服务器的指定文件夹中,也可以保存在数据库中,本 ...

  10. 用 AJAX 读取xml 节点属性值

    <html> <head> <title>AjaxTest</title> <script> var xmlHttp; function c ...