如何在 Paho-MQTT 中设置用户名和密码

使用以下命令在 Paho-MQTT 中设置用户名和密码

paho_set_username_password.py
client.username_pw_set("user","password")
client.username_pw_set("myusername", "aeNg8aibai0oiloo7xiad1iaju1uch")

你需要在调用 connect() 之前调用它!

如何使用用户名和密码连接的示例:

paho_connect_example.py
client = mqtt.Client("clientid")
client = mqtt.Client("mqtt-test") # client ID "mqtt-test"
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("myusername", "aeNg8aibai0oiloo7xiad1iaju1uch")
client.connect('127.0.0.1', 1883)
client.loop_forever()  # 启动网络守护进程

Check out similar posts by category: MQTT, Python