pythonmqtt客户端的实现-创新互联
python mqtt 客户端的实现?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联2013年开创至今,先为凤冈等服务建站,凤冈等地企业,进行企业商务咨询服务。为凤冈企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。安装paho-mqtt
pip install paho-mqtt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
python消息收发实现
import paho.mqtt.client as mqtt from multiprocessing import Process import camera_person_num MQTTHOST = "172.19.4.4" MQTTPORT = 1883 mqttClient = mqtt.Client()
# 连接MQTT服务器 def on_mqtt_connect(): mqttClient.connect(MQTTHOST, MQTTPORT, 60) mqttClient.loop_start()
# 消息处理函数 def on_message_come(lient, userdata, msg): print(msg.topic + ":" + str(msg.payload.decode("utf-8"))) # 消息处理开启多进程 p = Process(target=talk, args=("/camera/person/num/result", msg.payload.decode("utf-8"))) p.start()
# subscribe 消息订阅 def on_subscribe(): mqttClient.subscribe("test", 1) # 主题为"test" mqttClient.on_message = on_message_come # 消息到来处理函数 # publish 消息发布 def on_publish(topic, msg, qos): mqttClient.publish(topic, msg, qos);
# 多进程中发布消息需要重新初始化mqttClient def talk(topic, msg): cameraPsersonNum = camera_person_num.CameraPsersonNum(msg) t_max, t_mean = cameraPsersonNum.personNum() mqttClient = mqtt.Client() mqttClient.connect(MQTTHOST, MQTTPORT, 60) mqttClient.loop_start() mqttClient.publish(topic, '{"max":' + str(t_max) + ',"mean:"' + str(t_mean) + '}', 1)
def main(): on_mqtt_connect() on_subscribe() while True: pass if __name__ == '__main__': main()
关于python mqtt 客户端的实现问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
当前文章:pythonmqtt客户端的实现-创新互联
文章URL:http://pwwzsj.com/article/hiscj.html