Using SSL With Evy

Evy makes it easy to use non-blocking SSL sockets. If you’re using Python 2.6 or later, you’re all set, evy wraps the built-in ssl module. If on Python 2.5 or 2.4, you have to install pyOpenSSL to use evy.

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

from evy.patched import urllib2
from import spawn
bodies = [spawn(urllib2.urlopen, url)
     for url in ("","")]
for b in bodies:
    print b.wait().read()

With Python 2.6

To use ssl sockets directly in Python 2.6, use evy.patched.ssl, which is a non-blocking wrapper around the standard Python ssl module, and which has the same interface. See the standard documentation for instructions on use.


evy.patched.OpenSSL 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.

Here’s an example of a server:

from evy.patched import socket
from evy.patched.OpenSSL import SSL

# insecure context, only for example purposes
context = SSL.Context(SSL.SSLv23_METHOD)
context.set_verify(SSL.VERIFY_NONE, lambda *x: True))

# 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(('', 80443))

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

Table Of Contents

Related Topics

This Page