How to create HTTPS tornado server

j0shu4b0y picture j0shu4b0y · Aug 19, 2013 · Viewed 26.3k times · Source

Please help me to create HTTPS tornado server My current code Python3 doesn't work

import os, socket, ssl, pprint, tornado.ioloop, tornado.web, tornado.httpserver
from tornado.tcpserver import TCPServer

class getToken(tornado.web.RequestHandler):
    def get(self):
        self.write("hello")

application = tornado.web.Application([
    (r'/', getToken),
])

# implementation for SSL
http_server = tornado.httpserver.HTTPServer(application)

TCPServer(ssl_options={
    "certfile": os.path.join("/var/pyTest/keys/", "ca.csr"),
    "keyfile": os.path.join("/var/pyTest/keys/", "ca.key"),
})

if __name__ == '__main__':
    #http_server.listen(8888)
    http_server = TCPServer()
    http_server.listen(443)
    tornado.ioloop.IOLoop.instance().start()

HTTPS is very important for me, please help

Answer

falsetru picture falsetru · Aug 19, 2013

No need to use TCPServer.

Try following:

import tornado.httpserver
import tornado.ioloop
import tornado.web

class getToken(tornado.web.RequestHandler):
    def get(self):
        self.write("hello")

application = tornado.web.Application([
    (r'/', getToken),
])

if __name__ == '__main__':
    http_server = tornado.httpserver.HTTPServer(application, ssl_options={
        "certfile": "/var/pyTest/keys/ca.csr",
        "keyfile": "/var/pyTest/keys/ca.key",
    })
    http_server.listen(443)
    tornado.ioloop.IOLoop.instance().start()