Skip to content

Instantly share code, notes, and snippets.

@ralight
Created March 17, 2013 16:56
Show Gist options
  • Select an option

  • Save ralight/5182443 to your computer and use it in GitHub Desktop.

Select an option

Save ralight/5182443 to your computer and use it in GitHub Desktop.
Testing Python and mosquitto.py utf8 encoding.
#!/usr/bin/python
# vim: set fileencoding=utf8 :
import mosquitto
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def on_message(mosq, obj, msg):
print(msg.topic + " : " + msg.payload)
print(msg.topic + " : " + str(msg.payload.__class__))
print("")
if msg.topic == "utf8/cn":
sys.exit()
def on_subscribe(mosq, obj, mid, granted_qos):
mosq.publish("utf8/en", "message", 2)
mosq.publish("utf8/cn", "中国人", 2)
mqttc = mosquitto.Mosquitto()
mqttc.on_message = on_message
mqttc.on_subscribe = on_subscribe
mqttc.connect("test.mosquitto.org", 1883, 60)
mqttc.subscribe("utf8/#", 2)
mqttc.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment