0

So I'm trying to configure a websocket server in python and cant quite figure out the issue since I came across a bunch of contradictory posts (Shall provide them later). Here is the code for the server: (I shall be using --.--.---.--- for the IP)

import asyncio
import websockets

HOST = --.--.---.---

async def echo(websocket, path):
    async for message in websocket:
        print(message)

asyncio.get_event_loop().run_until_complete(
    websockets.serve(echo, host=HOST, port=5500))
asyncio.get_event_loop().run_forever()

This chunk of code as suggested by this post: https://bytesofgigabytes.com/python/websockets-python/ said I must put in my IP address as a host. Now I understand that this might be my modem's IP address, but in this case I spun up an AWS instance to work things out. In this case Im getting the error:

OSError: [Errno 10049] error while attempting to bind on address ('--.--.---.---', 5500): the requested address is not valid in its context

I thought it was admin priverlages, ran it from an elevated command prompt, made a rule in the firewall but to no avail.

I then came across this post: Python Websockets can't connect over internet which says that I shouldnt put in the IP address. So I didnt do that - this time the code runs but I cant connect over a websocket client which I have on html/js. The code for them is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="controller.js"></script>
</body>
</html>

and

const socket = new WebSocket('ws://--.--.---.---:5500/');

socket.addEventListener('open', function (event) {
    console.log("this is running");
    socket.send('Hello Server!');
});

In this case the client (in chrome) says:

controller.js:1 WebSocket connection to 'ws://--.--.---.---:5500/' failed: 

EDIT 1: Tried this solution and didn't get it to work Python, socket.error: [Errno 10049]

EDIT 2: confirmed that there is some issue client side - the port is listening on netstat screen shot

1 Answer 1

0

Okay I have figured it out. The issue is not with the code, at least not entirely. The IP should be "0.0.0.0" or "" for the python listener to bind. Whats more important is that in AWS/Papespace/Linode you pick a static IP. This was very obvious but I missed it since I thought we got Static IPs by default (damn you linode for this bad habit). Moreover depending on your provider, AWS for example needs you to make ports available in their portal. (Screenshot attached)

import asyncio
import websockets

Host = "0.0.0.0"
SocketPort = 5500

async def WebSocketserver(websocket, path):
    Rxdata = await websocket.recv()
    print(f" Received data : {Rxdata}")
    await websocket.send("200")
    

start_server = websockets.serve(WebSocketserver, Host, SocketPort)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

The following AWS firewall needs to happen in addition to the internal computer firewall Screenshot of AWS firewall

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.