I'm using the code below to connect to a broker via WebSocket and SSL. The broker is password protected. My code -
import paho.mqtt.client as mqtt
import ssl
import time
def on_connect(client, userdata, flags, rc):
print("connected with result code "+str(rc))
def on_publish(client, userdata, mid):
print("mid: "+str(mid))
client = mqtt.Client('device', transport="websockets")
client.on_connect = on_connect
client.on_publish = on_publish
client.username_pw_set("login", "pass")
client.ws_set_options(path="/logger")
client.tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLS, ciphers=None)
connection = False
print("Trying to connect ...")
client.connect(host="mqtt.broker.io", port=443)
connection = True
for _ in range(5):
time.sleep(1)
client.publish("some/topic", "HELLO", qos=2)
client.loop(1)
client.disconnect()
The error I'm getting -
Trying to connect ... connected with result code 0 Traceback (most recent call last): File "/home/souvik/GitLab/Embedded/demo_mqtt_auth.py", line 30, in client.loop(1) File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1120, in loop return self._loop(timeout) File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1164, in _loop rc = self.loop_read() File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1556, in loop_read rc = self._packet_read() File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 2373, in _packet_read except ConnectionError as err: NameError: global name 'ConnectionError' is not defined
sleepiz_one on hw-1929-diagnostics [$!] on (eu-west-1) took 6s ❯ /bin/python /home/souvik/GitLab/Embedded/demo_mqtt_auth.py Trying to connect ... connected with result code 0 Traceback (most recent call last): File "/home/souvik/GitLab/Embedded/demo_mqtt_auth.py", line 30, in client.loop(1) File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1120, in loop return self._loop(timeout) File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1164, in _loop rc = self.loop_read() File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1556, in loop_read rc = self._packet_read() File "/home/souvik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 2373, in _packet_read except ConnectionError as err: NameError: global name 'ConnectionError' is not defined
Not too sure where I'm getting the exception from. Even though I get the exception, I do see data on the broker, so I'm not sure what's
EDIT: I'm on paho-mqtt 1.6.1
ConnectionError
was added in Python 3.3.
You're running on Python 2.7, which has been end-of-life for 3 years.
You could have some luck downgrading your paho-mqtt
to a version before 1.6.0, as that's the first version that has this commit referring to ConnectionError
:
pip install paho-mqtt==1.5.1
or alternatively up to 1.6.1 which claims to "fix Python 2.7 compatibility":
pip install paho-mqtt==1.6.1
Really, though, you should stop using Python 2.7, since it has not been supported for a long while now.