Python Simple SSL Server

Just trying to set up a simple SSL server. In the past, I have never had SSL work for me. I have a free understanding of how SSL certificates and signature are.

The code is simple

import socket, ssl

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.load_cert_chain(certfile="mycertfile") ###############

bindsocket = socket.socket()
bindsocket.bind(('', 2099))
bindsocket.listen(5)

while True:
    newsocket, fromaddr = bindsocket.accept()
    sslsoc = context.wrap_socket(newsocket, server_side=True)
    request = sslsoc.read()
    print(request)

The line there is C ### s after it does not work. I don't know what I need to do with openssl to create a PEM file that will work here.

Can someone tell me how to make this simple socket work.

By the way, this is NOT used for HTTP.

+10
source share
2 answers

this command can be used to create a self-signed certificate

openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem

openssl , ​​ , .., , cert.pem. RSA, , . :

-----BEGIN RSA PRIVATE KEY-----
 # your private key
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
 # your certificate
-----END CERTIFICATE-----

, ssl :

context.load_cert_chain(certfile="cert.pem", keyfile="cert.pem")

btw, python2 "SSLContext". , python2, pem :

newsocket, fromaddr = bindsocket.accept()
connstream = ssl.wrap_socket(newsocket,
                             server_side=True,
                             certfile="cert.pem",
                             keyfile="cert.pem",
                             ssl_version=YOUR CHOICE) 

ssl: ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv23. , ssl.PROTOCOL_SSLv23 , .

+9

In your example, you provide certfile, but not keyfile. Both are required.

0
source

All Articles