我使用 Python 3.7 编写窗口服务。我使用此命令安装了服务
python myservice.py install
然后它出现在services.msc中,但是当我尝试启动时出现此错误。
当我使用此代码调试服务时
python myservice.py debug
我得到了这个回报:
C:\WINDOWS\system32>python C:\Users\lenovo\Desktop\py\Service.py debug
Debugging service Service- press Ctrl+C to stop.
Error 0xC0000003 - The instance's SvcRun() method failed
<Error getting traceback - traceback.print_exception() failed
(null): (null)
我搜索并尝试了Internet上的所有方法,但无法解决。 win32service可能包含错误吗?我该如何解决这个问题?
我的python脚本:
import win32event
import win32serviceutil
import servicemanager
import win32service
import uuid,re
import socket
import time
class Service(win32serviceutil.ServiceFramework):
try:
_svc_name_ = "Service"
_svc_display_name_ = "Service"
_svc_description_ = "Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_,''))
self.stop()
def SvcDoRun(self):
self.run()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def stop(self):
self.isrunning=False
pass
def start(self):
self.isrunning=True
pass
def main(self):
while self.isrunning:
time.sleep(5)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
"RUNNING",
(self._svc_name_,''))
#there were my socket codes
def parse_command_line():
win32serviceutil.HandleCommandLine(Service)
except:
print("error")
if __name__ == '__main__':
Service.parse_command_line()