Using SSL With Eventlet

Eventlet makes it easy to use non-blocking SSL sockets. If you’re using Python 3.7 or later, you’re all set, eventlet wraps the built-in ssl module.

In either case, the green modules handle SSL sockets transparently, just like their standard counterparts. As an example, can be used to fetch https urls in as non-blocking a fashion as you please:

from import urlopen
from eventlet import spawn
bodies = [spawn(urlopen, url)
     for url in ("","")]
for b in bodies:

PyOpenSSL has exactly the same interface as pyOpenSSL (docs), and works in all versions of Python. This module is much more powerful than socket.ssl(), and may have some advantages over ssl, depending on your needs.

For testing purpose first create self-signed certificate using following commands

$ openssl genrsa 1024 > server.key
$ openssl req -new -x509 -nodes -sha1 -days 365 -key server.key > server.cert

Keep these Private key and Self-signed certificate in same directory as and for simplicity sake.

Here’s an example of a server (

from import socket
from import SSL

# insecure context, only for example purposes
context = SSL.Context(SSL.SSLv23_METHOD)
# Pass server's private key created
# Pass self-signed certificate created

# create underlying green socket and wrap it in ssl
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection = SSL.Connection(context, sock)

# configure as server
connection.bind(('', 8443))

# accept one client connection then close up shop
client_conn, addr = connection.accept()

Here’s an example of a client (

import socket
# Create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server
s.connect(('', 8443))
sslSocket = socket.ssl(s)
print repr(sslSocket.server())
print repr(sslSocket.issuer())
sslSocket.write('Hello secure socket\n')
# Close client

Running example:

In first terminal

$ python

In another terminal

$ python