Search code examples
pythonsslhttpscyclone

Does cyclone (python) support HTTPS connections and SSL?


Does cyclone (python) support HTTPS connections and SSL? If so, can you please provide an example?

I've looked through the documentation and code on the cyclone github page, and can't find any reference to SSL. But since lots of cyclone is just wrapping twisted, maybe there's something I'm missing...


Solution

  • From the README:

    cyclone is a Twisted protocol, therefore it may be used in conjunction with any other protocol implemented in Twisted.

    If Twisted supports SSL then cyclone supports it e.g.:

    #file: cyclone-ssl.py
    import cyclone.web
    
    class IndexHandler(cyclone.web.RequestHandler):
        def get(self):
            self.write("hello world")
    
    factory = cyclone.web.Application([(r"/", IndexHandler)])
    portstr = "ssl:4443:privateKey=server_key.pem:certKey=server_cert.pem"
    
    # make twisted app
    from twisted.application import service, strports
    
    application = service.Application("cyclone-ssl")
    strports.service(portstr, factory).setServiceParent(application)
    

    Run it as:

    $ twistd -ny cyclone-ssl.py
    

    The part that activates ssl is portstr. It specifies that the server serves on 4443 port and uses server_key.pem as its private key, server_cert.pem as a certificate.