import paho.mqtt.client as mqtt from time import strftime client = mqtt.Client() def callback(func): setattr(client, func.__name__, func) return func @callback def on_message(*args): (_, _, message) = args # Display all received messages published on topics to which we subscribed print '[%s] Message on %s:\n%s\n\n' % ( strftime('%Y-%m-%d %H:%M:%S'), message.topic, message.payload, ) @callback def on_connect(*_): # Connection losses also lead to subscriptions being lost. Therefore, it is advantageous to handle subscriptions # in this callback function. # NB: MQTT allows subscribing to a set of topics in one packet. Doing so is highly encouraged. client.subscribe([ # Subscribe to all topics prefixed '/oneM2M/'. ('/oneM2M/#', 1), # In case you are curious: Topics under $SYS/ are exposing interesting extra information (such as number of # connected clients, traffic, etc.) on some brokers. This is a current best practice that may end up in the # official standard. See also <https://github.com/mqtt/mqtt.github.io/wiki/SYS-Topics> #('$SYS/#', 1), ]) # Adjust this to your needs. Alternative ports can be specified through the 'port' parameter client.connect('140.116.247.71') client.loop_forever()