gufengxiaoxiao 2022-11-07 09:18 采纳率: 50%
浏览 678
已结题

python连接mqtt时报[WinError 10061] 由于目标计算机积极拒绝,无法连接。

问题遇到的现象和发生背景 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及端口无误,之前一直都能连接

  • 写回答

1条回答 默认 最新

  • JiaoEleven 2022-11-07 11:15
    关注

    1.首先查看你的mqtt服务是否正常,可以选择使用mqqt.fx工具测试下
    2.在代码中加client_id

    import random
    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_id = f'python-mqtt-{random.randint(0, 100)}'
    client = mqtt.Client(client_id=client_id,protocol=3)
    # client.username_pw_set("admin", "password")
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(host="192.168.3.243", 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()
    
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 创建了问题 11月7日

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡