安装环境

  1. pip install tornado

HelloWorld代码编写

  1. #! /usr/bin/python
  2. # -*- coding:utf-8 -*-
  3. import tornado.httpserver
  4. import tornado.ioloop
  5. import tornado.options
  6. import tornado.web
  7. from tornado.options import define, options
  8. define("port", default=8000, help="run on the given port", type=int)
  9. class IndexHandler(tornado.web.RequestHandler):
  10. def get(self):
  11. greeting = self.get_argument('greeting', 'Hello')
  12. self.write(greeting + ', tornado!')
  13. if __name__ == "__main__":
  14. tornado.options.parse_command_line()
  15. app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
  16. http_server = tornado.httpserver.HTTPServer(app)
  17. http_server.listen(options.port)
  18. tornado.ioloop.IOLoop.instance().start()

测试 & 验证