Skip to content Skip to sidebar Skip to footer

Python Websockets Can't Connect Over Internet

I'm just trying to get a very basic websocket connection over internet. The code seems fine - because it works when connecting to localhost - but for some reason fails when I try t

Solution 1:

Your line:

websockets.serve(handler, host='127.0.0.1', port=6969)

provides a specific address on which the websockets server listens. Your server will only listen on that address; any requests to any other address will never be seen.

From https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_server :

The host parameter can be a string, in that case the TCP server is bound to host and port. The host parameter can also be a sequence of strings and in that case the TCP server is bound to all hosts of the sequence. If host is an empty string or None, all interfaces are assumed and a list of multiple sockets will be returned (most likely one for IPv4 and another one for IPv6).

You have bound your webserver to 127.0.0.1, which is a special address that only ever refers to the local machine. This address is also known as localhost. No other machine can ever connect to your localhost.

The solution is to provide an empty string or None (the default value). In this case, your web server will listen for requests sent to any address.

websockets.serve(handler, port=6969)

Post a Comment for "Python Websockets Can't Connect Over Internet"