首先需要安装paho-mqtt库,在Python中使用pip安装即可:pip install paho-mqtt 然后可以使用如下代码实现MQTT订阅,并使用用户名和密码进行认证:
import paho.mqtt.client as mqtt
# 连接回调函数
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("topic")
# 接收消息回调函数
def on_message(client, use