Python - paho.mqtt.client - WebSocket handshake error, connection not upgraded

1.1k views Asked by At

I'm trying to connect to an existing MQTT service (over websockets) that is not managed by me, using paho.mqtt.client.

This is my code:

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("$SYS/#")

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))


client = mqtt.Client(client_id="<MY_CLIENT_ID>", transport="websockets")
client.username_pw_set(username="<MY_USERNAME>",password="<MY_PASSWORD>")

client.on_connect = on_connect
client.on_message = on_message

client.tls_set()
client.connect("SERVER_ADDRESS", SERVER_PORT, 55)

client.loop_forever()

When I try to run it I'm getting the following exception

Traceback (most recent call last):
  File "C:\Development\CockpitHomePyScript\test.py", line 24, in <module>
    client.connect("SERVER_ADDRESS", 5222, 55)
  File "C:\Users\Ophir\AppData\Local\Programs\Python\Python310\lib\site-packages\paho\mqtt\client.py", line 914, in connect
    return self.reconnect()
  File "C:\Users\Ophir\AppData\Local\Programs\Python\Python310\lib\site-packages\paho\mqtt\client.py", line 1080, in reconnect
    sock = WebsocketWrapper(sock, self._host, self._port, self._ssl,
  File "C:\Users\Ophir\AppData\Local\Programs\Python\Python310\lib\site-packages\paho\mqtt\client.py", line 3713, in __init__
    self._do_handshake(extra_headers)
  File "C:\Users\Ophir\AppData\Local\Programs\Python\Python310\lib\site-packages\paho\mqtt\client.py", line 3766, in _do_handshake
    raise WebsocketConnectionError(
paho.mqtt.client.WebsocketConnectionError: WebSocket handshake error, connection not upgraded

When I searched online the only thing I found was to have tls_set before connect, but that doesn't seem to help...

To make sure that I'm not missing any client certificate to connect, I tried writing a client that connects to the same server with exactly the same details with .NET (using MQTT.NET library) and without setting up anything else special other than the settings above I successfully connected, consumed events, and sent messages.

What am I doing wrong in Python?

1

There are 1 answers

1
developer82 On

I found the solution, pretty stupid actually. By default paho.mqtt.client uses /mqtt as path. In my case I needed the root as path, so what I needed to do is just set it:

client.ws_set_options(path="/")

The actual response from the service was 404, but the way the library code is written is recognizing it as a handshake issue, so the actual exception is misleading.