28

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

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Max Khrichtchatyi
  • 507
  • 1
  • 7
  • 14

1 Answers1

50

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()
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @63ek, Did you check your certfile is correct? I tested the code myself, but certfile with `.pem` extension. – falsetru Aug 19 '13 at 06:18
  • 10
    Tornado 2.x and up provides a convenience method in Application to create the HTTPServer instance for you, so you can just say application.listen(443,ssl_options= etc ) . You can even have the *same* application listening on *both* ports (80 *and* 443) just by putting *two* calls to application.listen before starting the IOLoop, although if you want to be able to act differently depending on which port a request came in on then you'll need a different application for each port. – Silas S. Brown Jun 07 '17 at 15:09
  • 1
    @SilasS.Brown, Thank you for the information. How about post a separated answer? Commenting here notifies me only (does not notify OP unless you explicitly mentioning OP's username). Posting answer will benefit more people. – falsetru Jun 07 '17 at 15:12
  • 1
    Thanks, the question was asked 4 years ago so I expect OP has moved on since then; just wanted to add a note in case anyone else finds this via search, and my comment was a minor change to your answer so I didn't think it worth creating a 'competing' answer, people can just see your answer + that comment – Silas S. Brown Jun 08 '17 at 16:40
  • @SilasS.Brown, i am looking forward to try application.listen(443,ssl_options= etc ). but it's not working. could you please provide good references. – Ravi Anand Oct 11 '17 at 15:58
  • 1
    @RaviAnand It works for me in [Web Adjuster](http://people.ds.cam.ac.uk/ssb22/adjuster/). If it's not working for you, maybe post a new question of the "why isn't this code working" type, and show your code? – Silas S. Brown Oct 12 '17 at 21:39