Forums

Websocket connection

Hi! I've a paid account with Pythonanywhere, and I've all files there and everything seems ok. I'm trying to create a Websocket connection from Pythonanywhere, the client is on a PC in my office to a server in another PC, but I'm unable to reach it. and here is the error message:(

Traceback (most recent call last): File "C:\venv\S_C_Connection\client.py", line 37, in <module> asyncio.run(connect_to_server()) File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\venv\S_C_Connection\client.py", line 33, in connect_to_server async with websockets.connect(ws_url) as websocket: File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\legacy\client.py", line 629, in aenter return await self ^^^^^^^^^^ File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\legacy\client.py", line 647, in await_impl_timeout return await self.await_impl() ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\legacy\client.py", line 654, in await_impl await protocol.handshake( File "C:\Users\malek\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\legacy\client.py", line 325, in handshake raise InvalidStatusCode(status_code, response_headers) websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 404

) I need help to fix the problem.

You're getting a 404 which means that the path that you're trying to access is not on the site that you're accessing. Check that you are using the correct URL.

Hi again It was working good on local server , I've this problem when I uploaded the files to pythonanywhere and change this 'ws://127.0.0.1:8000/ws/raspberry_pi/' url in my client file into 'wss://rahdr.pythonanywhere.com/ws/raspberry_pi/' so Idont know what to do now? Where is the problem now?