python-websockets / websockets

Library for building WebSocket servers and clients in Python

Home Page:https://websockets.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enable to connect ! invalid handshake

shay-te opened this issue · comments

Hello. Thank you in advance for reading.

I have tested it with a small NodeJS WS service, and our client can open the connection.
When switching to python-websockets, I get an error.
We are connecting to the WebSocket using AWS Network Load Balancer. (same load balancer working with node)

We are stuck on this for two weeks.
We are experiencing this error.

Thank you

[2024-05-09 06:49:28,569][websockets.server][DEBUG] - = connection is CONNECTING
[2024-05-09 06:49:28,569][websockets.server][DEBUG] - ! invalid handshake
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 74, in read_request
    request_line = await read_line(stream)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 279, in read_http_request
    path, headers = await read_request(self.reader)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 76, in read_request
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 167, in handler
    await self.handshake(
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 581, in handshake
    path, request_headers = await self.read_http_request()
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 283, in read_http_request
    raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > HTTP/1.1 400 Bad Request
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > Date: Thu, 09 May 2024 06:49:28 GMT
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > Server: Python/3.8 websockets/12.0
[2024-05-09 06:49:28,570][websockets.server][DEBUG] - > Content-Length: 77
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - > Content-Type: text/plain
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - > Connection: close
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - > [body] (77 bytes)
[2024-05-09 06:49:28,571][websockets.server][INFO] - connection rejected (400 Bad Request)
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - x closing TCP connection
[2024-05-09 06:49:28,571][websockets.server][DEBUG] - = connection is CLOSED
[2024-05-09 06:49:28,572][websockets.server][INFO] - connection closed
[2024-05-09 06:49:28,599][websockets.server][DEBUG] - = connection is CONNECTING
[2024-05-09 06:49:28,600][websockets.server][DEBUG] - ! invalid handshake
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 74, in read_request
    request_line = await read_line(stream)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 279, in read_http_request
    path, headers = await read_request(self.reader)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 76, in read_request
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 167, in handler
    await self.handshake(
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 581, in handshake
    path, request_headers = await self.read_http_request()
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 283, in read_http_request
    raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > HTTP/1.1 400 Bad Request
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Date: Thu, 09 May 2024 06:49:28 GMT
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Server: Python/3.8 websockets/12.0
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Content-Length: 77
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Content-Type: text/plain
[2024-05-09 06:49:28,601][websockets.server][DEBUG] - > Connection: close
[2024-05-09 06:49:28,602][websockets.server][DEBUG] - > [body] (77 bytes)
[2024-05-09 06:49:28,602][websockets.server][INFO] - connection rejected (400 Bad Request)
[2024-05-09 06:49:28,602][websockets.server][DEBUG] - x closing TCP connection
[2024-05-09 06:49:28,602][websockets.server][DEBUG] - = connection is CLOSED
[2024-05-09 06:49:28,603][websockets.server][INFO] - connection closed



[2024-05-09 06:49:29,799][websockets.server][DEBUG] - = connection is CONNECTING
[2024-05-09 06:49:29,800][websockets.server][DEBUG] - ! invalid handshake
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 74, in read_request
    request_line = await read_line(stream)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 200, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 279, in read_http_request
    path, headers = await read_request(self.reader)
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/http.py", line 76, in read_request
    raise EOFError("connection closed while reading HTTP request line") from exc
EOFError: connection closed while reading HTTP request line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 167, in handler
    await self.handshake(
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 581, in handshake
    path, request_headers = await self.read_http_request()
  File "/usr/local/lib/python3.8/site-packages/websockets/legacy/server.py", line 283, in read_http_request
    raise InvalidMessage("did not receive a valid HTTP request") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP request
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > HTTP/1.1 400 Bad Request
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Date: Thu, 09 May 2024 06:49:29 GMT
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Server: Python/3.8 websockets/12.0
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Content-Length: 77
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Content-Type: text/plain
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > Connection: close
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - > [body] (77 bytes)
[2024-05-09 06:49:29,801][websockets.server][INFO] - connection rejected (400 Bad Request)
[2024-05-09 06:49:29,801][websockets.server][DEBUG] - x closing TCP connection
[2024-05-09 06:49:29,802][websockets.server][DEBUG] - = connection is CLOSED
[2024-05-09 06:49:29,802][websockets.server][INFO] - connection closed

From websockets' perspective, it never receives the first line of a WebSocket request.

What is "our client" here?

From websockets' perspective, it never receives the first line of a WebSocket request.

What is "our client" here?

the client is Chrome and also React-Native

This rules out a problem with the client. Could you try connecting to a server running locally? If that works, then it means that the problem is at the level of your AWS Network Load Balancer setup (and I suspect that it's there).

Thank you for the response. It is working well when connecting with WS (not secured) directly to the IP address.
But JavaScript WS can handle the same setup.

If that's your question, one websockets server process cannot serve secure (wss://) and insecure (ws://) connections. I don't plan to add this feature either.

There's two ways to handle secure connections:

  • terminate TLS in a reverse proxy: recommended because it's usually more foolproof; AWS Network Load Balancer can do this;
  • terminate TLS in the websockets: doable but the security of the system depends on how you configure it.

Thank you. We moved the entire project to nodejs. Shame

I'm sorry it didn't work for you 🥲