Skip to content

Instantly share code, notes, and snippets.

@nakagami
Created November 15, 2015 04:26
Show Gist options
  • Select an option

  • Save nakagami/f6b6b49a947bae4cabea to your computer and use it in GitHub Desktop.

Select an option

Save nakagami/f6b6b49a947bae4cabea to your computer and use it in GitHub Desktop.
A sample of MQTT subscriber (QoS=0)
#!/usr/bin/env python2.7
from __future__ import print_function
import paho.mqtt.client as mqtt # https://pypi.python.org/pypi/paho-mqtt/
topic = "foo/bar"
def on_connect(client, userdata, flags, respons_code):
print('connect:status {0}'.format(respons_code))
client.subscribe(topic)
def on_message(client, userdata, msg):
print("recieve message %s:%s" % (msg.topic, str(msg.payload)))
if __name__ == '__main__':
client = mqtt.Client(protocol=mqtt.MQTTv311)
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set('subscriber', 'pass')
client.connect('localhost')
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment