python mqtt 客戶端的實現(xiàn)?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)2013年開創(chuàng)至今,先為鳳岡等服務(wù)建站,鳳岡等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為鳳岡企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。安裝paho-mqtt
pip install paho-mqtt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
python消息收發(fā)實現(xiàn)
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服務(wù)器 def on_mqtt_connect(): mqttClient.connect(MQTTHOST, MQTTPORT, 60) mqttClient.loop_start()
# 消息處理函數(shù) 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 # 消息到來處理函數(shù) # publish 消息發(fā)布 def on_publish(topic, msg, qos): mqttClient.publish(topic, msg, qos);
# 多進程中發(fā)布消息需要重新初始化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()
關(guān)于python mqtt 客戶端的實現(xiàn)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。