【问题标题】:Python paho-mqtt Connect to MQTT brokerPython paho-mqtt 连接到 MQTT 代理
【发布时间】:2018-09-11 17:38:02
【问题描述】:

我正在使用此 python 脚本来实现 Paho(MQTT) 订阅者,但我无法获得任何响应消息。我可以使用 mosquitto_sub -t "" -d -h -p 8883 --psk foo --psk-identity bar --insecure --tls-version tlsv1 订阅 mqtt brokerin 命令提示符

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("*********")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("*********", 8883, 60)

client.loop_forever()

当我在 python 脚本上运行时,它不会响应任何错误或消息,而是继续循环,我也逐行运行它,当我运行 client.connect("*********", 8883, 60) 时,它只显示 0 .请注意,如果没有 psk 和 psk-identity,我们将无法连接到代理。

谢谢

【问题讨论】:

  • 我不确定 Python Paho 客户端是否支持 PSK TLS 连接
  • 谢谢,PSK TLS 支持有哪些选项?
  • 您是否真的为代理配置了 PSK TLS?为什么不能使用普通的 TLS 证书和用户名/密码或客户端证书来识别客户端?
  • Mqtt代理在其他国家配置,我们需要连接它的主题,他们为我们提供了mqtt fqdn, psk, psk-identity, topic
  • 然后按照文档了解如何连接 TLS 以及用户名和证书

标签: python mqtt mosquitto messagebroker


【解决方案1】:

请仔细检查您的主题,有时缺少 / 或 # 会导致此问题。

试试这个

def on_message(client, userdata, msg):
   print("Message Recieved from broker: " + msg.payload.decode())

【讨论】: