问题遇到的现象和发生背景 python连接mqtt时报错
用代码块功能插入代码,请勿粘贴截图
import time
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("连接成功")
print("Connected with result code " + str(rc))
def on_message(client, userdata, msg):
msg_16 = msg.payload.hex()
ct = time.time()
local_time = time.localtime(ct)
data_head = time.strftime("%Y%m%d%H%M%S", local_time)
data_secs = (ct - int(ct)) * 1000
time_stamp = "%s%03d" % (data_head, data_secs) # 17位时间戳
print(time_stamp)
print(msg.topic + " " + msg_16)
client = mqtt.Client(protocol=3)
# client.username_pw_set("admin", "password")
client.on_connect = on_connect
client.on_message = on_message
client.connect(host="192.168.1.101", port=1883, keepalive=60) # 订阅频道
time.sleep(1)
# client.subscribe("public")
# client.subscribe([("M-scsMonitor/M-smiOS", 0), ("M-wirelessDCM/M-puAmr", 0), ("M-scsMonitor/Broadcast/0006/000B", 0)])
client.subscribe([ ("M-scsMonitor/Broadcast/0006/0003", 0),("M-scsMonitor/Broadcast/0006/0002", 0),("M-scsMonitor/Broadcast/0006/0005", 0)])
# client.subscribe([("M-wirelessDCM/M-puAmr", 0), ("test", 2)])
client.loop_forever()
运行结果及报错内容
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
我的解答思路和尝试过的方法
确定ip及端口无误,之前一直都能连接