Created
March 17, 2013 16:56
-
-
Save ralight/5182443 to your computer and use it in GitHub Desktop.
Testing Python and mosquitto.py utf8 encoding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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