Search code examples
pythonpython-3.xtornado

Python 3 Tornado website


I am trying to create a simple local website using Tornado. When I run this and go to http://localhost:8888, I get the following error: This site can’t be reached localhost refused to connect. I am still a beginner with python so any help would be much appreciated. My code is shown below.

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("<!DOCTYPE html><head><title>" + "Hello world</title></head>" + "<body>Hello World</body>")

    if __name__ == "__main__":
        application = tornado.web.Application([(r"/", MainHandler),
                                               ],)
        application.listen(8888)
        tornado.ioloop.IOLOOP.instance().start()

Solution

  • IOLOOP should be IOLoop:

    tornado.ioloop.IOLoop.instance().start()
    #              ^^^^^
    


    UPDATE You need to indent correctly the code. Especially if __name__ == "__main__": part, that part should be outside of class .. definition:

    if __name__ == "__main__":  # <----
        application = tornado.web.Application([(r"/", MainHandler),
                                               ],)
        application.listen(8888)
        tornado.ioloop.IOLoop.instance().start()