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

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 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序