赞
踩
pip install paho-mqtt
import paho.mqtt.client as mqtt
def on_connect(client,userdata,flags,rc):
print("Connected with result code"+str(rc))
def on_message(client,userdata,msg):
print(msg.topic+" " + ":"+str(msg.payload))
client = mqtt.Client()
import paho.mqtt.client as mqtt def on_connect(client,userdata,flags,rc): print("Connected with result code"+str(rc)) def on_message(client,userdata,msg): print(msg.topic+" " + ":"+str(msg.payload)) client = mqtt.Client() #当连接时调用on_connect函数 client.on_connect = on_connect #当接到推送时调用on_message函数 client.on_message = on_message #设置用户名和密码 client.username_pw_set("wtf","lol") #设置连接地址和端口还有timeout client.connect("127.0.0.1",1883,60) #设置订阅主题 client.subscribe("emq") #publish方法,第一个参数是d主题,另一个是消息内容 client.publish("emq","hello world",2) #设置一个循环 当接收到待推送消息时立马调用on_message函数打印 client.loop_forever()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。