一个最简单的用PYTHON实现的WINDOWS服务程序
2013-04-11 08:38
981 查看
一个最简单的用PYTHON实现的WINDOWS服务程序
(2009-12-05 02:54:07)转载▼
标签:音乐win32servicepythonname杂谈 | 分类:PYTHON |
没有更新,而且SvcDoRun写得不完整(见http://www.chinaunix.net/jh/55/558190.html,
不知道是不是原始出处);而mail.python.org中的没有定义_svc_name_等变量(见
http://mail.python.org/pipermail/python-list/2005-December/315190.html)
2、这个实现功能很简单,就是把当前时间写入‘c:\\temp\\time.txt’文件,一看即知,
大家可以随意扩充。
3、用
service install 安装
service start 启动
service stop 停止
service debug 调试
service remove 删除
service.py
---代码开始
#coding:utf-8
import win32serviceutil
import win32service
import win32event
import win32evtlogutil
import time
class service(win32serviceutil.ServiceFramework):
_svc_name_ = "test_python"
_svc_display_name_ = "test_python"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
print '服务开始'
def SvcDoRun(self):
import servicemanager
#------------------------------------------------------
# Make entry in the event log that this service started
#------------------------------------------------------
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_,''))
#-------------------------------------------------------------
# Set an amount of time to wait (in milliseconds) betweenruns
#-------------------------------------------------------------
self.timeout=100
while 1:
#-------------------------------------------------------
# Wait for service stop signal, if I timeout, loop again
#-------------------------------------------------------
rc=win32event.WaitForSingleObject(self.hWaitStop,self.timeout)
#
# Check to see if self.hWaitStop happened
#
if rc == win32event.WAIT_OBJECT_0:
#
# Stop signal encountered
#
break
else:
#
# Put your code here
#
#
f=open('c:\\temp\\time.txt','w',0)
f.write(time.ctime(time.time()))
f.close()
print '服务运行中'
time.sleep(1)
#
# Only return from SvcDoRun when you wish to stop
#
return
def SvcStop(self):
#---------------------------------------------------------------------
# Before we do anything, tell SCM we are starting the stopprocess.
#---------------------------------------------------------------------
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
#---------------------------------------------------------------------
# And set my event
#---------------------------------------------------------------------
win32event.SetEvent(self.hWaitStop)
print '服务结束'
return
if __name__=='__main__':
win32serviceutil.HandleCommandLine(service)
---代码结束
相关文章推荐
- 用Python实现一个简单的文件传输协议
- 一个最简单的博客园文章密码暴力破解器-python3实现
- 实现一个简单的python小脚本的一些必要步骤
- Python 实现一个简单的http服务器
- 用python实现一个简单的测试用例
- 基于python实现一个简单的神经网络
- Python实现一个简单的购物车逻辑
- 用Python实现一个简单的多线程TCP服务器的教程
- Python实现一个简单的微信跳一跳辅助
- 用Python 实现刷钻网上抢任务,并实现一个简单的限制使用时间的功能
- Python web入门:Django学习与实践二(简单页面实现和创建一个模板)
- python 实现一个简单的 WebSocket 服务器
- python基础学习——利用Flask实现一个简单博客
- 用Python实现一个简单的能够发送带附件的邮件程序的教程
- python代码简单实现一个词频统计
- 一个简单的 python 实现 图片同步脚本 ,对于有图片群集的项目有参考价值
- Python(17)python使用tkinter实现一个简单的CSGO幸运转盘抽奖游戏
- 《Python - 实现一个简单的装饰器》--- 输出程序运行时间
- 5行Python代码实现一个简单的在线英汉词典
- python实现一个简单的登录注册Demo