javascriptpythonwebsocketportforwardingdmz

Python / JavaScript - WebSockets connection problem


I am having several errors with a WebSockets server.

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 0.0.0.0 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 time async function. I sometimes get the error

send.html:14 WebSocket connection to ws://86.205.245.32:5678/ failed: WebSocket opening handshake timed out

but rarely.

I have lots of questions that can maybe answer this post.

The Server

print("start serveur")
import time, websockets, asyncio

async def time(websocket, path):
    print("connect")
    print(websocket)
    print(await websocket.recv())
start_server = websockets.serve(time, "", 5678)
loop=asyncio.get_event_loop() 

loop.run_until_complete(start_server)


loop.run_forever()

Server's Traceback

Error in connection handler
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 674, in transfer_data
    message = yield from self.read_message()
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 742, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 815, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 884, in read_frame
    extensions=self.extensions,
  File "/usr/local/lib/python3.5/dist-packages/websockets/framing.py", line 99, in read
    data = yield from reader(2)
  File "/usr/lib/python3.5/asyncio/streams.py", line 668, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/usr/lib/python3.5/asyncio/streams.py", line 458, in _wait_for_data
    yield from self._waiter
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", 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 "/usr/local/lib/python3.5/dist-packages/websockets/server.py", line 169, in handler
    yield from self.ws_handler(self, path)
  File "webserveur.py", line 8, in time
    print(await websocket.recv())
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 434, in recv
    yield from self.ensure_open()
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", 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

The Client

<script>
    alert("hello");

    var ws = new WebSocket("ws://86.205.245.32:5678/"  );//1*
        
    ws.onopen = function (event) {
       alert("had a handshake answer that cause a connection");
       ws.send("hello");
    }

</script>

WebSockets Request

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

I want to use DMZ because my network is very strange with lots of protection, so I think that putting the server in a Demilitarize zone will help. For now, I can't tell you if it will be helpful because for now when the client execute the line 1* see on the client code it can't connect to the server. What is strange is that without DMZ 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.

What did I try

I often have this issues when I close the server during the client execution, even if it doesn't execute the block time that prints print("connect"):

Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketServerProtocol.handler() running at /usr/local/lib/python3.5/dist-packages/websockets/server.py:117> wait_for=<Future pending cb=[Task._wakeup()]>>

I also changed the client's code as follows but without success.

var ws = new WebSocket("ws://86.205.245.32:5678/"),
        messages = document.createElement('ul');

        ws.onopen = function (event) {
        alert("connect");
        ws.send("hello");
        
        }

The Hardware

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

THANKS


Solution

  • I tried to reproduce your issue, but my opinion is that your problem is caused by your network infrastructure.

    The server and the client are very basic and they work smoothly.

    Divide your problem into different phases:

    1. Use your local environment to debug the code (client and server in localhost, on your PC)
    2. Use your local network to have the first segregation between the client and the server
    3. Use some free plans on Cloud Providers to test extended networking (AWS, GCP, DigitalOcean)

    Some suggestions may be: