InvalidHandshake between Arduino WebSocket Server and Python Client

Hi everyone,

I currently have a WebSocket Server constructed using the mWebSockets.h library on a ATmega328P Arduino Uno with a W5500 Ethernet Shield. This is designed to send a message to a client running in python every time the server is queried by the client. The code for the server is shown below:

 #include <WebSocketServer.h>
using namespace net;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[]{0xA8, 0x61, 0x0A, 0xAE, 0x69, 0x13};
IPAddress ip(198, 162, 1, 177);
IPAddress gateway(0,0,0,0);
IPAddress DNSserver(0,0,0,0);
IPAddress subnet(255,255,255,0);

constexpr uint16_t port = 80;
WebSocketServer wss{port};

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");

  // start the server
  Serial.print("server is at ");

  wss.onConnection([](WebSocket & ws) {
    const auto protocol = ws.getProtocol(); 
    if (protocol) {
      Serial.print(F("Client protocol: "));

    ws.onMessage([](WebSocket & ws, const WebSocket::DataType dataType,
    const char *message, uint16_t length) {
      switch (dataType) {
        case WebSocket::DataType::TEXT:
          Serial.print(F("Received: "));
        case WebSocket::DataType::BINARY:
          Serial.println(F("Received binary data"));

      ws.send(dataType, message, length);

    ws.onClose([](WebSocket &, const WebSocket::CloseCode, const char *,
    uint16_t) {

    Serial.print(F("New client: "));

    const char message[] {"Hello from Arduino server!"};
    ws.send(WebSocket::DataType::TEXT, message, strlen(message));


void loop() {

The python client code is shown below:

import asyncio
import websockets
import socket
import time
import datetime
import struct

starttime = time.time() # start value for timed data acquisition

# setup socket 1
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.sendall(b'GET / HTTP/1.1 400')
if True:
def acquire():
    data = s1.recv(10000)
while True:
   time.sleep(1.0 - ((time.time() - starttime) % 1.0)) # continue every  (2.0 seconds)


Currently, it seems like a connection can be made between the two but a handshake cannot be established:

Traceback (most recent call last):
  File "", line 11, in <module>
    asyncio.get_event_loop().run_until_complete(test()) # run until test() is finished
  File "/usr/lib64/python3.6/asyncio/", line 488, in run_until_complete
    return future.result()
  File "", line 5, in test
    async with websockets.connect( "ws://") as websocket:
  File "/usr/lib64/python3.6/site-packages/websockets/legacy/", line 604, in __aenter__
    return await self
  File "/usr/lib64/python3.6/site-packages/websockets/legacy/", line 634, in __await_impl__
  File "/usr/lib64/python3.6/site-packages/websockets/legacy/", line 397, in handshake
    response_headers, available_subprotocols
  File "/usr/lib64/python3.6/site-packages/websockets/legacy/", line 302, in process_subprotocol
    raise InvalidHandshake("no subprotocols supported")
websockets.exceptions.InvalidHandshake: no subprotocols supported

Given that I am very new to socket communication, I am unsure how to solve this issue as I thought both the websockets library in python and the mWebSockets.h library in Arduino use the RFC6455 protocol. How do I resolve this issue?

The error message above is not from the Python code above!

The Python code is not a Websocket client.

What else did you change before posting?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.