C
C
chrome1232020-08-22 10:56:44
Python
chrome123, 2020-08-22 10:56:44

How to set up WSS in Python?

Wem hello.

To create a WebSokcet server, I used . such a library

Recently, the question arose about how to encrypt the connection to the web socket

on the library website in the WSS section.

WSS is to WS what HTTPS is to HTTP: the connection is encrypted using Transport Layer Security (TLS), often referred to as Secure Sockets Layer (SSL). WSS requires TLS certificates like HTTPS.

Here's how to adapt the server example to provide secure connections. See the documentation for the SSL module to configure the context securely.

That is, in fact, I can use the same certificates as for encrypting an HTTP connection.

The domain name registrar presented me with such keys

certificate_ca.crt Root certificate
certificate.csr CSR request
certificate.crt Certificate

I can't figure out how to connect them

ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain("/chain.crt")

start_server = websockets.serve(counter, "localhost", 6666, ssl=ssl_context)


ssl_context.load_cert_chain("chain.crt")
    ssl.SSLError: [SSL] PEM lib (_ssl.c:4012)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
chrome123, 2020-08-22
@chrome123

Figured out pem can be created from a certificate and a private key. Everything works great.
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----

C
cython, 2020-08-22
@cython

There is an example in the documentation that uses a certificate in pem format. Also try using os.path or pathlib to specify the path to the certificate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question