paho.mqtt.python无法传递用户名和密码



我正在尝试使用paho创建mqtt连接。发布后,rc返回成功状态,但消息尚未传递。

代码如下:

import os
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
print("connected with rc: "+str(rc))
pass

def on_publish(client, data, result):
print("data published")
pass

client = mqtt.Client()
client.username_pw_set(username="test", password="test" )
client.on_connect = on_connect 
client.on_publish = on_publish
client.connect("some-host", 1883)

topic = "/something"
value = "cool"
ret = client.publish(topic, value)
if ret.rc == 0:
return {
"statusCode": 200,
"body": "The payload was successfully published"
}
else:
return {
"statusCode": 500,
"body": "There was an error publishing this payload"
}

有人知道这里发生了什么吗?我看到了这篇文章,但我的问题并没有通过添加client.tls_set()来解决。

将发布移动到on_connect回调。代码试图在连接完成之前发布,并添加它是QoS 0消息,它只是被丢弃

正如英国人所说,你还没有启动客户端网络循环

相关内容

  • 没有找到相关文章

最新更新