基于python怎么安装MQTT客户端
这篇文章主要讲解了“基于python怎么安装MQTT客户端”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于python怎么安装MQTT客户端”吧!
站在用户的角度思考问题,与客户深入沟通,找到伊吾网站设计与伊吾网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都网站设计、企业官网、英文网站、手机端网站、网站推广、申请域名、网站空间、企业邮箱。业务覆盖伊吾地区。
MQTT是一个轻量级的消息协议。从2014年12月IOIT大会上得到的消息,该协议已经被OASIS标准组织接收,成立了专门的工作组,以意味着该规范正式走向了标准化之路。
一个轻量级的MQTT服务器是:http://www.mosquitto.org,可以运行ARM/MIPS的嵌入式linux系统上,如OpenWRT。
很多客户端模块现在被Eclipse基金会接管,发展很快。
下面介绍基于python的MQTT的客户端的安装:
git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git cd org.eclipse.paho.mqtt.python sudo python setup.py install
注意,如果使用Sublime,最好到里面的控制台进行安装,缺省运行在独立的环境中,跟系统的控制台安装的程序不一样。
然后,发送消息:
#!/usr/bin/python # This shows a service of an MQTT subscriber. # Copyright (c) 2010-2015, By openthings@163.com. import sys import datetime import socket, sys import paho.mqtt.publish as publish def transmitMQTT(strMsg): #strMqttBroker = "localhost" strBroker = "112.124.67.178" strMqttChannel = "/inode/mychannel" print(strMsg) publish.single(strMqttChannel, strMsg, hostname = strMqttBroker) if __name__ == '__main__': transmitMQTT("Hello,MQTT Proxy, I am client inside python.") print "Send msg ok."
创建一个MQTT服务器程序:
#!/usr/bin/python # This shows a service of an MQTT subscriber. # Copyright (c) 2010-2015, By openthings@163.com. import sys import datetime import socket, sys #====================================================== #MQTT Initialize.-------------------------------------- try: import paho.mqtt.client as mqtt except ImportError: print("MQTT client not find. Please install as follow:") print("git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.python.git") print("cd org.eclipse.paho.mqtt.python") print("sudo python setup.py install") #====================================================== def on_connect(mqttc, obj, rc): print("OnConnetc, rc: "+str(rc)) def on_publish(mqttc, obj, mid): print("OnPublish, mid: "+str(mid)) def on_subscribe(mqttc, obj, mid, granted_qos): print("Subscribed: "+str(mid)+" "+str(granted_qos)) def on_log(mqttc, obj, level, string): print("Log:"+string) def on_message(mqttc, obj, msg): curtime = datetime.datetime.now() strcurtime = curtime.strftime("%Y-%m-%d %H:%M:%S") print(strcurtime + ": " + msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) on_exec(str(msg.payload)) def on_exec(strcmd): print "Exec:",strcmd strExec = strcmd #===================================================== if __name__ == '__main__': mqttc = mqtt.Client("mynodeserver") mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_log = on_log #strBroker = "localhost" strBroker = "112.124.67.178" mqttc.connect(strBroker, 1883, 60) mqttc.subscribe("/inode/mychannel", 0) mqttc.loop_forever()
感谢各位的阅读,以上就是“基于python怎么安装MQTT客户端”的内容了,经过本文的学习后,相信大家对基于python怎么安装MQTT客户端这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!
本文名称:基于python怎么安装MQTT客户端
URL链接:http://ybzwz.com/article/jidepj.html