我已经编写了一个简单的Windows服务,它应该使用Websockets报告VirtualBox机器的状态。
安装并启动服务后,我的Websocket服务器会收到连接请求,但连接几乎立即关闭。
服务启动时服务器输出:
安装并启动服务后,我的Websocket服务器会收到连接请求,但连接几乎立即关闭。
服务启动时服务器输出:
running on port 8888
new connection
connection closed
使用pythonservice.exe -debug myservice
运行服务会打开一个websocket连接并发送我所期望的数据。
当我使用调试标志启动服务时,服务器输出如下:
running on port 8888
new connection
message received VM NAME: win1, Memory: 111, CPUS: 1
message received VM NAME: win2, Memory: 266, CPUS: 1
message received VM NAME: win3, Memory: 256, CPUS: 1
message received VM NAME: lin1, Memory: 256, CPUS: 1
message received VM NAME: lin2, Memory: 200, CPUS: 1
message received VM NAME: lin3, Memory: 222, CPUS: 1
connection closed
服务来源:
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
import logging
import virtualbox
from websocket import create_connection
ws = create_connection("ws://192.168.56.1:8888/ws")
class VMInventory(win32serviceutil.ServiceFramework):
_svc_name_ = "VMInventory"
_svc_display_name_ = "VMInventory service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.stop_event = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
self.stop_requested = False
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.stop_event)
self.stop_requested = True
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,'')
)
self.main()
def main(self):
# Simulate a main loop
vb = virtualbox.VirtualBox()
while True:
vms = vb.machines
if self.stop_requested:
break
for vm in vms:
ws.send("VM NAME: %s, Memory: %s, CPUS: %s" % (vm.name, str(vm.memory_size), str(vm.cpu_count)))
time.sleep(5)
ws.close()
return
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(HelloWorldSvc)
-debug
选项所做的就是将stdout/stderr附加到终端,因此您的服务代码可能会尝试将输出发送到其中之一,但由于在非调试模式下它们不存在,因此该尝试仅在以该模式运行时才会导致致命错误。 - martineautry/except
子句。 - martineau