M
M
MrFavour2021-06-15 17:35:14
Python
MrFavour, 2021-06-15 17:35:14

Security websocket JS with Python?

Good afternoon!

There is a websocket server in python3 and a client in JS

The usual connection ws://localhost:8765 connects and works on the localhost, but it does not work on a VPS with HTTPS

Server example:

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile="/etc/letsencrypt/live/mysite.ru/fullchain.pem", keyfile="/etc/letsencrypt/live/mysite.ru/privkey.pem")

start_server = websockets.serve(server.ws_handler, 'localhost', 8765, ssl=ssl_context)


I also tried self-signed certificates, it doesn't work either
localhost_pem = Path(__file__ + "/../ssl/certificate.pem").resolve()
localhost_key = Path(__file__ + "/../ssl/key.pem").resolve()


Everything is simple in JS
socket = new WebSocket("wss://"+location.host+":8765");


The server starts and runs without errors, but the client does not connect
Returns code: 1006

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stefan, 2021-06-15
@MrFavour

I'm certainly not an expert, but it's logical to assume

start_server = websockets.serve(server.ws_handler, 'localhost', 8765, ssl=ssl_context)
that you cannot connect to localhost from outside, try replacing localhost with 0.0.0.0
PS: Either proxy requests to localhost using the same Nginx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question