diff --git a/examples/client_mqtt_clear_retain.py b/examples/client_mqtt_clear_retain.py index 95518bfa3a70b47a8b4510d8657915d9124d1108..996a00709745163b78d501ee8f51bf4d114cddc6 100644 --- a/examples/client_mqtt_clear_retain.py +++ b/examples/client_mqtt_clear_retain.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2013 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -18,45 +19,43 @@ import sys import getopt -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt final_mid = 0 + def on_connect(mqttc, userdata, flags, rc): if userdata == True: - print("rc: "+str(rc)) + print("rc: " + str(rc)) + def on_message(mqttc, userdata, msg): global final_mid if msg.retain == 0: pass - #sys.exit() + # sys.exit() else: if userdata == True: - print("Clearing topic "+msg.topic) + print("Clearing topic " + msg.topic) (rc, final_mid) = mqttc.publish(msg.topic, None, 1, True) + def on_publish(mqttc, userdata, mid): global final_mid if mid == final_mid: sys.exit() + def on_log(mqttc, userdata, level, string): print(string) + def print_usage(): - print("mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic") + print( + "mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic") + def main(argv): debug = False @@ -70,7 +69,8 @@ def main(argv): verbose = False try: - opts, args = getopt.getopt(argv, "dh:i:k:p:P:t:u:v", ["debug", "id", "keepalive", "port", "password", "topic", "username", "verbose"]) + opts, args = getopt.getopt(argv, "dh:i:k:p:P:t:u:v", + ["debug", "id", "keepalive", "port", "password", "topic", "username", "verbose"]) except getopt.GetoptError as s: print_usage() sys.exit(2) @@ -114,6 +114,6 @@ def main(argv): mqttc.subscribe(topic) mqttc.loop_forever() + if __name__ == "__main__": main(sys.argv[1:]) - diff --git a/examples/client_pub-wait.py b/examples/client_pub-wait.py index e10214f79fd5f8c4019fe9ca1ea1de6d9f6dec2f..184040952b24d68fadfe7c38fda2dc1363bf871f 100644 --- a/examples/client_pub-wait.py +++ b/examples/client_pub-wait.py @@ -1,12 +1,13 @@ -#!/usr/bin/python3.5 +#!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: diff --git a/examples/client_session_present.py b/examples/client_session_present.py index b5e2271b7d033ace814e72df0ef593cb06d095ba..bf40c58f477b33ca91b6bec9d8d5e41704292204 100644 --- a/examples/client_session_present.py +++ b/examples/client_session_present.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: diff --git a/examples/client_sub-class.py b/examples/client_sub-class.py index cb908db36941815849378be7a77a0d82fc400855..dcf3e00c7cd4af6af8a9ef7db8843ac916e00b93 100644 --- a/examples/client_sub-class.py +++ b/examples/client_sub-class.py @@ -1,4 +1,5 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2013 Roger Light # @@ -14,20 +15,8 @@ # This example shows how you can use the MQTT client in a class. -import sys -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt class MyMQTTClass(mqtt.Client): diff --git a/examples/client_sub-multiple-callback.py b/examples/client_sub-multiple-callback.py index 4377f0ce3ea35a8409156c0993082e7ee97cad6f..9a879c63550399f14c76f6b9f64feb96501acefd 100644 --- a/examples/client_sub-multiple-callback.py +++ b/examples/client_sub-multiple-callback.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -15,37 +16,29 @@ # This shows a simple example of an MQTT subscriber using a per-subscription message handler. -import sys -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt + def on_message_msgs(mosq, obj, msg): # This callback will only be called for messages with topics that match # $SYS/broker/messages/# - print("MESSAGES: "+msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) + print("MESSAGES: " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) + def on_message_bytes(mosq, obj, msg): # This callback will only be called for messages with topics that match # $SYS/broker/bytes/# - print("BYTES: "+msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) + print("BYTES: " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) + def on_message(mosq, obj, msg): # This callback will be called for messages that we receive that do not # match any patterns defined in topic specific callbacks, i.e. in this case # those messages that do not have topics $SYS/broker/messages/# nor # $SYS/broker/bytes/# - print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) + print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) + mqttc = mqtt.Client() @@ -57,4 +50,3 @@ mqttc.connect("iot.eclipse.org", 1883, 60) mqttc.subscribe("$SYS/#", 0) mqttc.loop_forever() - diff --git a/examples/client_sub-srv.py b/examples/client_sub-srv.py index a8fe1aa28ca4f2a3344dd5d8cd046b0c1a0a1dc8..bf20008e07af830ceacf1175a8f595fe29c56ef6 100644 --- a/examples/client_sub-srv.py +++ b/examples/client_sub-srv.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -16,20 +17,8 @@ # This shows a simple example of an MQTT subscriber using connect_srv method. -import sys -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt def on_connect(mqttc, obj, flags, rc): print("Connected to %s:%s" % (mqttc._host, mqttc._port)) diff --git a/examples/client_sub-ws.py b/examples/client_sub-ws.py index 8c1487bc6e1c0ad80c9e037dbf5c94e48a634a48..10b6979e6323784027b91395e43ca490dedb9ff9 100644 --- a/examples/client_sub-ws.py +++ b/examples/client_sub-ws.py @@ -1,12 +1,13 @@ -#!/usr/bin/python3 +#!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -16,21 +17,8 @@ # This shows a simple example of an MQTT subscriber. -import sys - -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt def on_connect(mqttc, obj, flags, rc): print("rc: "+str(rc)) @@ -61,6 +49,4 @@ mqttc.on_log = on_log mqttc.connect("test.mosquitto.org", 8080, 60) mqttc.subscribe("$SYS/broker/version", 0) - mqttc.loop_forever() - diff --git a/examples/client_sub.py b/examples/client_sub.py index c7c233ca5354a7dac45627dae9a8e06084e79955..5e6cdfe3d2e5336ef77bdcc6e7e2438e4a63b814 100644 --- a/examples/client_sub.py +++ b/examples/client_sub.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -16,36 +17,30 @@ # This shows a simple example of an MQTT subscriber. -import sys -try: - import paho.mqtt.client as mqtt -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.client" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.client as mqtt +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.client as mqtt + def on_connect(mqttc, obj, flags, rc): - print("rc: "+str(rc)) + print("rc: " + str(rc)) + def on_message(mqttc, obj, msg): - print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) + print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) + def on_publish(mqttc, obj, mid): - print("mid: "+str(mid)) + print("mid: " + str(mid)) + def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: "+str(mid)+" "+str(granted_qos)) + print("Subscribed: " + str(mid) + " " + str(granted_qos)) + def on_log(mqttc, obj, level, string): print(string) + # If you want to use a specific client id, use # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client @@ -56,10 +51,8 @@ mqttc.on_connect = on_connect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages -#mqttc.on_log = on_log +# mqttc.on_log = on_log mqttc.connect("m2m.eclipse.org", 1883, 60) mqttc.subscribe("$SYS/#", 0) - mqttc.loop_forever() - diff --git a/examples/context.py b/examples/context.py new file mode 100644 index 0000000000000000000000000000000000000000..e42e26e474fffc3f7e215f127180a733d9e2ff6d --- /dev/null +++ b/examples/context.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +# Ensure can import paho package +try: + import paho + +except ImportError: + # This part is only required to run the examples from within the examples + # directory when the module itself is not installed. + import sys + import os + import inspect + + cmd_subfolder = os.path.realpath( + os.path.abspath( + os.path.join( + os.path.split( + inspect.getfile(inspect.currentframe()) + )[0], + "..", + "src" + ) + ) + ) + if cmd_subfolder not in sys.path: + sys.path.insert(0, cmd_subfolder) + + import paho diff --git a/examples/publish_multiple.py b/examples/publish_multiple.py index 54bcd84c1b59b907fdde32ab8e07b307f63b90a6..24b9668a078e50c8819056701a466cf485da2835 100644 --- a/examples/publish_multiple.py +++ b/examples/publish_multiple.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -14,20 +15,8 @@ # This shows an example of using the publish.multiple helper function. -import sys -try: - import paho.mqtt.publish as publish -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.publish" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.publish as publish +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.publish as publish -msgs = [{'topic':"paho/test/multiple", 'payload':"multiple 1"}, ("paho/test/multiple", "multiple 2", 0, False)] +msgs = [{'topic': "paho/test/multiple", 'payload': "multiple 1"}, ("paho/test/multiple", "multiple 2", 0, False)] publish.multiple(msgs, hostname="test.mosquitto.org") diff --git a/examples/publish_single.py b/examples/publish_single.py index d127a461fcf450470f4280ef8fc315a7de24ae19..beba71f01694e078a5d1a9a34c84efec1875397f 100644 --- a/examples/publish_single.py +++ b/examples/publish_single.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -14,19 +15,7 @@ # This shows an example of using the publish.single helper function. -import sys -try: - import paho.mqtt.publish as publish -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.publish" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.publish as publish +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.publish as publish publish.single("paho/test/single", "boo", hostname="test.mosquitto.org") diff --git a/examples/publish_utf8-27.py b/examples/publish_utf8-27.py index 5499518de4c1a47f326fc07ef1970a5193cf9ef4..1835c5bebb1bb429d92199e207cd330efe708eef 100644 --- a/examples/publish_utf8-27.py +++ b/examples/publish_utf8-27.py @@ -1,13 +1,13 @@ #!/usr/bin/python -# vim: set fileencoding=utf-8 +# -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -15,21 +15,9 @@ # This shows an example of using the publish.single helper function with unicode topic and payload. -import sys -try: - import paho.mqtt.publish as publish -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.publish" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.publish as publish +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.publish as publish -t=u"paho/test/single/ô" - -publish.single(t, u"bôô", hostname="test.mosquitto.org") +topic = u"paho/test/single/ô" +payload = u"bôô" +publish.single(topic, payload, hostname="test.mosquitto.org") diff --git a/examples/subscribe_callback.py b/examples/subscribe_callback.py index 728da88ba416b96c7818f153666ce6d2683c071e..621218e83e65fb68d803638689142f1f28eb9fa2 100644 --- a/examples/subscribe_callback.py +++ b/examples/subscribe_callback.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2016 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -14,21 +15,8 @@ # This shows an example of using the subscribe.callback helper function. -import sys -try: - import paho.mqtt.subscribe as subscribe -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.subscribe" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.subscribe as subscribe - import paho.mqtt.client +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.subscribe as subscribe def print_msg(client, userdata, message): print("%s : %s" % (message.topic, message.payload)) diff --git a/examples/subscribe_simple.py b/examples/subscribe_simple.py index 5e31d1b6bdd1f5de2a3d7638428646ebd42d07f0..b6722cdf13edd9fa9e9bc7ae56b23925699cb0ab 100644 --- a/examples/subscribe_simple.py +++ b/examples/subscribe_simple.py @@ -1,12 +1,13 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright (c) 2016 Roger Light # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Eclipse Distribution License v1.0 -# which accompanies this distribution. +# which accompanies this distribution. # -# The Eclipse Distribution License is available at +# The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: @@ -14,21 +15,8 @@ # This shows an example of using the subscribe.simple helper function. -import sys -try: - import paho.mqtt.subscribe as subscribe -except ImportError: - # This part is only required to run the example from within the examples - # directory when the module itself is not installed. - # - # If you have the module installed, just use "import paho.mqtt.subscribe" - import os - import inspect - cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src"))) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - import paho.mqtt.subscribe as subscribe - import paho.mqtt.client +import context # Ensures paho is in PYTHONPATH +import paho.mqtt.subscribe as subscribe topics = ['#']