[Twisted-Python] Setting up a HTTPS-server

Matthew R. Scott twisted at goldenspud.com
Tue Jun 24 11:29:11 EDT 2003


On Tue 2003-06-24 09:38, Thomas Weholt \( PRIVAT \) wrote:
> Can anybody tell me how or if Twisted supports HTTPS? I've used M2Crypto in
> the past ad saw that the latest release also supports secure FTP. I'm not
> clear on the status of the current FTP-implementation in Twisted, so if
> anybody would like to shed some light on the current status of HTTPS and
> secure FTP that would be great.


The package I needed to install to get SSL working in twisted was pyOpenSSL.  
With Debian unstable I used the package 'python2.2-pyopenssl' 

I was able to set up a HTTPS server running alongside an HTTP server by using 
some hints in twisted/doc/examples/echoserv_ssl.py

Basically what it boiled down to for my use was:

[ ... ]

class ServerContextFactory:

    def getContext(self):
        """Create an SSL context.

        Similar to twisted's echoserv_ssl example, except the private key
        and certificate are in separate files."""
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_privatekey_file('serverkey.pem')
        ctx.use_certificate_file('servercert.pem')
        return ctx

[ ... ]

site = twisted.web.server.Site(...)
application.listenTCP(8080, site)
application.listenSSL(8443, site, ServerContextFactory())

[ ... ]

To play around with a dummy private key and certificate I used the following 
commands:

openssl genrsa -out serverkey.pem 2048
openssl req -new -x509 -key serverkey.pem -out servercert.pem -days 1095

Have fun :)

-- 
Matthew R. Scott
OMAjA / http://www.omaja.com/





More information about the Twisted-Python mailing list