自从上次写了那篇《待机唤醒后自动连接宽带》后,就尝试用刚学的Python来实现那个功能了,语句很简单,要用到Tim Golden编写的wmi.py 和 Mark Hammond的win32 extensions for Python ,两个缺一不可。
code:
import wmi
import os
c = wmi.WMI()
watcher = c.Win32_PowerManagementEvent.watch_for(EventType=7) # 监视待机事件;
while True:
os.system("kdlj.vbs") # 运行“连接宽带“的程序,这里还是用了上次那位仁兄的vbs代码;
watcher()
由于运行时Python的控制台窗口一直在那儿,看着有点碍事儿。于是乎想到要是能把他以
windows service的方式运行,就像其他在windows服务管理器里的程序一样。
最终,在"Python Programming On Win32"(by Mark Hammond)这本书里找到了相关介绍,它里
面有一个简单的模版,把程序代码放入相应位置就可以了:
# SmallestService.py
#
# A sample demonstrating the smallest possible service written in Python.
class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "SmallestPythonService"
_svc_display_name_ = "The smallest possible Python Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
# Before we do anything, tell the SCM we are starting the stop process.
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# And set my event.
win32event.SetEvent(self.hWaitStop)