-
-
Notifications
You must be signed in to change notification settings - Fork 540
Cannot connect to FastAPI-based websocket server using the library while postman. #1624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you enable debug logs (https://websockets.readthedocs.io/en/stable/howto/debugging.html) so we can see what happens on the wire? |
Ideally, do the same with Postman, so you can compare what postman sends with what websockets sends and figure out why your server accepts the former and not the latter. |
Thanks for help! INFO: Started server process [17820]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
DEBUG: = connection is CONNECTING
DEBUG: < GET /agent/kimi HTTP/1.1
DEBUG: < sec-websocket-version: 13
DEBUG: < sec-websocket-key: ikoJKF3/r+AlwoaSyTxaYg==
DEBUG: < connection: Upgrade
DEBUG: < upgrade: websocket
DEBUG: < sec-websocket-extensions: permessage-deflate; client_max_window_bits
DEBUG: < host: 127.0.0.1:8000
INFO: ('127.0.0.1', 59421) - "WebSocket /agent/kimi" [accepted]
DEBUG: > HTTP/1.1 101 Switching Protocols
DEBUG: > Upgrade: websocket
DEBUG: > Connection: Upgrade
DEBUG: > Sec-WebSocket-Accept: LFU0D2gegsyWqp5NGONXcgqDDSw=
DEBUG: > Sec-WebSocket-Extensions: permessage-deflate
DEBUG: > date: Tue, 29 Apr 2025 14:35:31 GMT
DEBUG: > server: uvicorn
INFO: connection open
DEBUG: = connection is OPEN When connecting using Python code, it seems that the server did not receive any data, as there is no output of log on the server side. The log of client is as follow. 2025-04-29 22:39:08,337 Using proactor: IocpProactor
2025-04-29 22:39:08,340 = connection is CONNECTING
2025-04-29 22:39:08,341 > GET /agent/kimi HTTP/1.1
2025-04-29 22:39:08,341 > Host: 127.0.0.1:8000
2025-04-29 22:39:08,341 > Upgrade: websocket
2025-04-29 22:39:08,341 > Connection: Upgrade
2025-04-29 22:39:08,341 > Sec-WebSocket-Key: lV6JnmW4N+Np6go3I7xL1Q==
2025-04-29 22:39:08,341 > Sec-WebSocket-Version: 13
2025-04-29 22:39:08,341 > Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
2025-04-29 22:39:08,342 > User-Agent: Python/3.10 websockets/15.0.1
2025-04-29 22:39:10,610 > EOF
2025-04-29 22:39:10,611 < EOF
2025-04-29 22:39:10,611 = connection is CLOSED
2025-04-29 22:39:10,612 x half-closing TCP connection
Traceback (most recent call last):
File "C:\Users\ping\miniconda3\envs\wiu\lib\site-packages\websockets\http11.py", line 241, in parse
status_line = yield from parse_line(read_line)
File "C:\Users\ping\miniconda3\envs\wiu\lib\site-packages\websockets\http11.py", line 309, in parse_line
line = yield from read_line(MAX_LINE_LENGTH)
File "C:\Users\ping\miniconda3\envs\wiu\lib\site-packages\websockets\streams.py", line 46, in read_line
raise EOFError(f"stream ends after {p} bytes, before end of line")
EOFError: stream ends after 0 bytes, before end of line
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\***\miniconda3\envs\wiu\lib\site-packages\websockets\client.py", line 301, in parse
response = yield from Response.parse(
File "C:\Users\***\miniconda3\envs\wiu\lib\site-packages\websockets\http11.py", line 243, in parse
raise EOFError("connection closed while reading HTTP status line") from exc
EOFError: connection closed while reading HTTP status line
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\code\who-is-undercover\test_demo.py", line 25, in <module>
asyncio.run(client())
File "C:\Users\***\miniconda3\envs\wiu\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\***\miniconda3\envs\wiu\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "D:\code\who-is-undercover\test_demo.py", line 15, in client
async with aconnect("ws://127.0.0.1:8000/agent/kimi") as websocket:
File "C:\Users\***\miniconda3\envs\wiu\lib\site-packages\websockets\asyncio\client.py", line 587, in __aenter__
return await self
File "C:\Users\***\miniconda3\envs\wiu\lib\site-packages\websockets\asyncio\client.py", line 543, in __await_impl__
await self.connection.handshake(
File "C:\Users\***\miniconda3\envs\wiu\lib\site-packages\websockets\asyncio\client.py", line 114, in handshake
raise self.protocol.handshake_exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP response |
In addition, I am working on Windows10 in a conda environment. The installed packages are:
|
websockets and postman send the same headers so I don't think we have a problem there.
More likely the problem is that your server doesn't actually receive the request from websockets? I'm sorry, I don't really know how to help you debug this remotely :-( |
I create a server using FastAPI and websockets, which looks like
I try to connect the server using code below.
But it fails with outputs as follow.
I can connect to the server using postman and a client writen in vue, so I found it quite strange to me.
Please help with the issue~
The text was updated successfully, but these errors were encountered: