这个MQTT客户端示例为什么失败?

0 投票
1 回答
4017 浏览
提问于 2025-04-18 05:10

我第一次用Python和mosquitto库尝试MQTT。下面是我的客户端程序。我想使用公共的演示MQTT服务器,地址是http://www.mqtt-dashboard.com/subscribe。但是我的客户端代码出现了错误,下面是错误信息。有没有人知道这是怎么回事?

#!/usr/bin/env python

import mosquitto

client = mosquitto.Mosquitto("fredtest", clean_session=True)
client.connect("broker.mqttdashboard.com", 1883)

client.publish("fred.test", "hello world", 1)

client.loop_forever()

错误信息:

C:\tmp>python mqttclient.py
Traceback (most recent call last):
  File "mqttclient.py", line 6, in 
    client.connect("broker.mqttdashboard.com", 1883)
  File "build\bdist.win-amd64\egg\mosquitto.py", line 582, in connect
  File "build\bdist.win-amd64\egg\mosquitto.py", line 657, in reconnect
  File "c:\python27\lib\socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 10060] A connection attempt failed because the connected pa
rty did not properly respond after a period of time, or established connection f
ailed because connected host has failed to respond

1 个回答

2

我现在无法把任何客户端连接到 broker.mqttdashboard.com:1883,所以这可能不是你代码的问题。

为了确认一下,你有没有试着连接到另一个服务器,比如 iot.eclipse.org:1883 呢?

撰写回答