title: Configuring a websocket server in python
body: <p>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)</p>
  <pre><code>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()
  </code></pre>
  <p>This chunk of code as suggested by this post: <a href="https://bytesofgigabytes.com/python/websockets-python/" rel="nofollow noreferrer">https://bytesofgigabytes.com/python/websockets-python/</a> 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:</p>
  <pre><code>OSError: [Errno 10049] error while attempting to bind on address ('--.--.---.---', 5500): the requested address is not valid in its context
  </code></pre>
  <p>I thought it was admin priverlages, ran it from an elevated command prompt, made a rule in the firewall but to no avail.</p>
  <p>I then came across this post: <a href="https://stackoverflow.com/questions/45944199/python-websockets-cant-connect-over-internet">Python Websockets can&#39;t connect over internet</a> 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:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;script src=&quot;controller.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>and</p>
  <pre><code>const socket = new WebSocket('ws://--.--.---.---:5500/');
  
  socket.addEventListener('open', function (event) {
      console.log(&quot;this is running&quot;);
      socket.send('Hello Server!');
  });
  </code></pre>
  <p>In this case the client (in chrome) says:</p>
  <pre><code>controller.js:1 WebSocket connection to 'ws://--.--.---.---:5500/' failed: 
  </code></pre>
  <p>EDIT 1: Tried this solution and didn't get it to work <a href="https://stackoverflow.com/questions/19599282/python-socket-error-errno-10049/19599379">Python, socket.error: [Errno 10049]</a></p>
  <p>EDIT 2: confirmed that there is some issue client side - the port is listening on netstat <a href="https://i.stack.imgur.com/rmJmf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rmJmf.png" alt="screen shot" /></a></p>