-
MQTT 不是普通的 client server 模型,他还加了一个 代理者。
根据剑锋的提示,先下载了 paho-mqtt 模块, ubuntu 14.04 上下载方法如下:
sudo apt-get install python-pip
sudo pip install paho-mqtt
source paho-mqtt/bin/activate
sudo apt-get install python-virtualenv
virtualenv paho-mqtt
参考: http://www.steves-internet-guide.com/install-mosquitto-linux/#install-test
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
sudo server mosquitto start
mosquitto -v
// 带调试信息的启动
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("chat")
def on_message(client, userdata, msg):
print(msg.topic+" " + ":" + str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)
client.loop_forever()
import paho.mqtt.client as mqtt
HOST = "127.0.0.1"
PORT = 1883
def test():
client = mqtt.Client()
client.connect(HOST, PORT, 60)
client.publish("chat","hello chenfulin",2)
client.loop_forever()
if __name__ == '__main__':
test()
python server.py
python client.py