我试图在PC上建立一个mqtt客户端,所以我开始用python编写脚本。当我从Python IDLE运行脚本时,它运行良好且稳定。我下载了AlwaysUp并创建了一个服务。当我运行服务时,它会在5秒后停止。
import paho.mqtt.client as mqtt #import the client1
import time
import os
strshtdwn=0
def on_log(clint, userdata,level, buf):
print("log: "+buf)
def on_connect(client,userdata, flags ,rc):
if rc==0:
print("Connected OK")
else:
print("Bad connection Returned code=",rc)
def on_disconnect(client, userdata, flags, rc=9):
print("DisConnected result code "+str(rc))
def on_message(clinet, userdata, msg):
topic=msg.topic
m_decode=str(msg.payload.decode("utf-8","ignore"))
if m_decode=="SHUTDOWN":
os.system("shutdown /p")
client.publish("stat/hwa_rang_pc/POWER","Shuting Down")
elif m_decode=="RESTART":
os.system("shutdown /r")
client.publish("stat/hwa_rang_pc/POWER","Restarting")
elif m_decode=="LOGOFF":
os.system("shutdown /l")
client.publish("stat/hwa_rang_pc/POWER","Logging OFF")
print("message received: ",m_decode)
print("strshtdwn",strshtdwn)
broker="192.168.1.3"
client = mqtt.Client("Hwa_Rang_PC") #create new instance
client.on_connect=on_connect
client.on_disconnect=on_disconnect
#client.on_log=on_log
client.on_message=on_message
print("Connecting to broker ",broker)
client.connect(broker) #connect to broker
client.loop_start()
client.subscribe("cmnd/hwa_rang_pc/power")
client.publish("stat/hwa_rang_pc/POWER","OFF")
#client.loop_stop()
#client.disconnect() #disconnect
事件查看器的错误尝试次数为3。两个ID为“ 2”,一个ID为“ 7023”。 请帮助!!!
答案 0 :(得分:0)
我正在使用pirmasoft的runassvc为python脚本生成Windows服务。
以下是必填字段:
Path to exe: c:\python\pythonw.exe
Parameter: c:\python\pythonscript.py
Working folder: c:\python\