原理:利用python的win32模块,注册服务,让代码在后台运行,检测光盘并拷贝文件
启动的方法就是直接在cmd下,main.py install ,然后去windows 的服务下就可以看到The smallestpossible Python Service 这个服务,你可以启动,停止,还可以设置成开机自动启动。启动服务后,会自动检测光盘并在后台拷贝文件
main.py
import win32serviceutil
import win32service
import win32event
import CopyDvd2Hard
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.
win32

通过Python的win32模块,该方案实现了在Windows后台注册服务,持续监测光盘并自动复制其内容。启动服务的方式是在CMD中运行main.py install,随后可在Windows服务中管理此服务,包括开机自动启动。服务启动后,将无声无息地执行光盘文件拷贝任务。
最低0.47元/天 解锁文章
458





