ssl.rst 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Using SSL With Eventlet
  2. ========================
  3. Eventlet makes it easy to use non-blocking SSL sockets. If you're using Python 2.7 or later, you're all set, eventlet wraps the built-in ssl module.
  4. In either case, the ``green`` modules handle SSL sockets transparently, just like their standard counterparts. As an example, :mod:`eventlet.green.urllib2` can be used to fetch https urls in as non-blocking a fashion as you please::
  5. from eventlet.green import urllib2
  6. from eventlet import spawn
  7. bodies = [spawn(urllib2.urlopen, url)
  8. for url in ("https://secondlife.com","https://google.com")]
  9. for b in bodies:
  10. print(b.wait().read())
  11. PyOpenSSL
  12. ----------
  13. :mod:`eventlet.green.OpenSSL` has exactly the same interface as pyOpenSSL_ `(docs) <http://pyopenssl.sourceforge.net/pyOpenSSL.html/>`_, and works in all versions of Python. This module is much more powerful than :func:`socket.ssl`, and may have some advantages over :mod:`ssl`, depending on your needs.
  14. For testing purpose first create self-signed certificate using following commands ::
  15. $ openssl genrsa 1024 > server.key
  16. $ openssl req -new -x509 -nodes -sha1 -days 365 -key server.key > server.cert
  17. Keep these Private key and Self-signed certificate in same directory as `server.py` and `client.py` for simplicity sake.
  18. Here's an example of a server (`server.py`) ::
  19. from eventlet.green import socket
  20. from eventlet.green.OpenSSL import SSL
  21. # insecure context, only for example purposes
  22. context = SSL.Context(SSL.SSLv23_METHOD)
  23. # Pass server's private key created
  24. context.use_privatekey_file('server.key')
  25. # Pass self-signed certificate created
  26. context.use_certificate_file('server.cert')
  27. # create underlying green socket and wrap it in ssl
  28. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. connection = SSL.Connection(context, sock)
  30. # configure as server
  31. connection.set_accept_state()
  32. connection.bind(('127.0.0.1', 8443))
  33. connection.listen(50)
  34. # accept one client connection then close up shop
  35. client_conn, addr = connection.accept()
  36. print(client_conn.read(100))
  37. client_conn.shutdown()
  38. client_conn.close()
  39. connection.close()
  40. Here's an example of a client (`client.py`) ::
  41. import socket
  42. # Create socket
  43. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  44. # Connect to server
  45. s.connect(('127.0.0.1', 8443))
  46. sslSocket = socket.ssl(s)
  47. print repr(sslSocket.server())
  48. print repr(sslSocket.issuer())
  49. sslSocket.write('Hello secure socket\n')
  50. # Close client
  51. s.close()
  52. Running example::
  53. In first terminal
  54. $ python server.py
  55. In another terminal
  56. $ python client.py
  57. .. _pyOpenSSL: https://launchpad.net/pyopenssl