1. # -*- coding: utf-8 -*-
    2. import paho.mqtt.client as mqtt
    3. # The callback for when the client receives a CONNACK response from the server.
    4. def on_connect(client, userdata, flags, rc):
    5. print("Connected with result code "+str(rc))
    6. # The callback for when a PUBLISH message is received from the server.
    7. def on_message(client, userdata, msg):
    8. #在这里处理业务逻辑
    9. print(msg.topic+" "+str(msg.payload))
    10. client = mqtt.Client()
    11. client.on_connect = on_connect
    12. client.on_message = on_message
    13. client.connect("106.14.142.242", 1883, 60)
    14. client.subscribe("junjietest")
    15. # Blocking call that processes network traffic, dispatches callbacks and
    16. # handles reconnecting.
    17. # Other loop*() functions are available that give a threaded interface and a
    18. # manual interface.
    19. client.loop_forever()