title: Python / JavaScript - WebSockets connection problem
body: <p>I am having several errors with a <strong>WebSockets</strong> server.</p>
  <p>Previously, I had a lot of problems in acknowledging the server when a connection was established. In the bind address, I have the empty string which is equal to <code>0.0.0.0</code> so I thought that the problem may come from there. For now, the client can only know that it has been connected when the server stops the <code>time</code> async function. I sometimes get the error</p>
  <p><code>send.html:14 WebSocket connection to ws://86.205.245.32:5678/ failed: WebSocket opening handshake timed out</code></p>
  <p>but rarely.</p>
  <p><em><strong>I have lots of questions that can maybe answer this post.</strong></em></p>
  <ul>
  <li><p>Can using subprotocol help? If yes, how to use it with a Python server?</p>
  </li>
  <li><p>Can using WSS help?</p>
  </li>
  <li><p>Do I need to change the bind IP of <code>0.0.0.0</code> to another?</p>
  </li>
  </ul>
  <p><em><strong>The Server</strong></em></p>
  <pre><code>print(&quot;start serveur&quot;)
  import time, websockets, asyncio
  
  async def time(websocket, path):
      print(&quot;connect&quot;)
      print(websocket)
      print(await websocket.recv())
  start_server = websockets.serve(time, &quot;&quot;, 5678)
  loop=asyncio.get_event_loop() 
  
  loop.run_until_complete(start_server)
  
  
  loop.run_forever()
  </code></pre>
  <p><em><strong>Server's Traceback</strong></em></p>
  <pre><code>Error in connection handler
  Traceback (most recent call last):
    File &quot;/usr/lib/python3.5/asyncio/tasks.py&quot;, line 241, in _step
      result = coro.throw(exc)
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/protocol.py&quot;, line 674, in transfer_data
      message = yield from self.read_message()
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/protocol.py&quot;, line 742, in read_message
      frame = yield from self.read_data_frame(max_size=self.max_size)
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/protocol.py&quot;, line 815, in read_data_frame
      frame = yield from self.read_frame(max_size)
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/protocol.py&quot;, line 884, in read_frame
      extensions=self.extensions,
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/framing.py&quot;, line 99, in read
      data = yield from reader(2)
    File &quot;/usr/lib/python3.5/asyncio/streams.py&quot;, line 668, in readexactly
      yield from self._wait_for_data('readexactly')
    File &quot;/usr/lib/python3.5/asyncio/streams.py&quot;, line 458, in _wait_for_data
      yield from self._waiter
    File &quot;/usr/lib/python3.5/asyncio/futures.py&quot;, line 380, in __iter__
      yield self  # This tells Task to wait for completion.
    File &quot;/usr/lib/python3.5/asyncio/tasks.py&quot;, line 304, in _wakeup
      future.result()
    File &quot;/usr/lib/python3.5/asyncio/futures.py&quot;, line 285, in result
      raise CancelledError
  concurrent.futures._base.CancelledError
  
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/server.py&quot;, line 169, in handler
      yield from self.ws_handler(self, path)
    File &quot;webserveur.py&quot;, line 8, in time
      print(await websocket.recv())
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/protocol.py&quot;, line 434, in recv
      yield from self.ensure_open()
    File &quot;/usr/local/lib/python3.5/dist-packages/websockets/protocol.py&quot;, line 658, in ensure_open
      ) from self.transfer_data_exc
  websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason
  </code></pre>
  <p><em><strong>The Client</strong></em></p>
  <pre><code>&lt;script&gt;
      alert(&quot;hello&quot;);
  
      var ws = new WebSocket(&quot;ws://86.205.245.32:5678/&quot;  );//1*
          
      ws.onopen = function (event) {
         alert(&quot;had a handshake answer that cause a connection&quot;);
         ws.send(&quot;hello&quot;);
      }
  
  &lt;/script&gt;
  </code></pre>
  <p><em><strong>WebSockets Request</strong></em></p>
  <pre><code>General
  Request URL: ws://86.205.245.32:5678/
  Request Headers
  Provisional headers are shown
  Accept-Encoding: gzip, deflate
  Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,fr;q=0.7
  Cache-Control: no-cache
  Connection: Upgrade
  Host: 86.205.245.32:5678
  Origin: file://
  Pragma: no-cache
  Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
  Sec-WebSocket-Key: M+CfTACesBfbhkWnFclrsA==
  Sec-WebSocket-Version: 13
  Upgrade: websocket
  User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
  </code></pre>
  <p>I want to use <em><strong>DMZ</strong></em> because my network is very strange with lots of protection, so I think that putting the server in a <em><strong>Demilitarize zone</strong></em> will help. For now, I can't tell you if it will be helpful because for now when the client execute the line <code>1* see on the client code</code> it can't connect to the server. What is strange is that without <em><strong>DMZ</strong></em> and the server on a Windows computer, it's the same but for sending data, the client can only see the data of the server when the server closes the connection.</p>
  <p><em><strong>What did I try</strong></em></p>
  <ul>
  <li>I tried to deactivate the firewall</li>
  <li>I asked questions on StackOverflow</li>
  </ul>
  <p>I often have this issues when I close the server during the client execution, even if it doesn't execute the block <code>time</code> that prints <code>print(&quot;connect&quot;)</code>:</p>
  <pre><code>Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;WebSocketServerProtocol.handler() running at /usr/local/lib/python3.5/dist-packages/websockets/server.py:117&gt; wait_for=&lt;Future pending cb=[Task._wakeup()]&gt;&gt;
  </code></pre>
  <p>I also changed the client's code as follows but without success.</p>
  <pre><code>var ws = new WebSocket(&quot;ws://86.205.245.32:5678/&quot;),
          messages = document.createElement('ul');
  
          ws.onopen = function (event) {
          alert(&quot;connect&quot;);
          ws.send(&quot;hello&quot;);
          
          }
  </code></pre>
  <p><em><strong>The Hardware</strong></em></p>
  <ul>
  <li><p>A Windows computer (client)</p>
  </li>
  <li><p>A Rasberry Pi (server)</p>
  </li>
  </ul>
  <p><em>Thanks for helping and let me know if you already had this issue and what have you done to solve it. Or if you have found errors in the code that could cause this type of problem</em></p>
  <p>THANKS</p>