#pip install tornado#cat web.pyimport tornado.ioloopimport tornado.webclass MainHandler(tornado.web.RequestHandler): def get(self): self.write('hello,world!')class test(tornado.web.RequestHandler): def get(self): self.write('this is testing !')application = tornado.web.Application([ # (r"",MainHandler), (r"/",MainHandler), (r"/test",test)])if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
#python web.py
http://IP:8888/
2.异步简化的tornado
#coding=utf-8__author__ = 'Administrator'import tornado.autoreloadimport tornado.httpserverimport tornado.ioloopimport tornado.webimport tornado.escapeimport tornado.optionsimport tornado.template#一下2行异步相关import tornado.httpclientimport tornado.genfrom tornado.options import define, optionsdefine("port", default=80, help="run on the given port", type=int)class myapp(tornado.web.Application): def __init__(self): handlers = [ (r"/", MainHandler), ] settings = { "cookie_secret": "bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E=", 'template_path':'templates', 'static_path' :'static', } tornado.web.Application.__init__(self, handlers, **settings)class MainHandler(tornado.web.RequestHandler): #一下2行异步装饰器 @tornado.web.asynchronous @tornado.gen.engine def get(self): text = self.get_argument("message", "来宾") self.render('index.html', ken=text) print("{'GET':'%s'}"%text) def post(self): text = self.get_argument("message") if text == "": text = "来宾" self.render('index.html', ken=text) print("info {'POST':'%s'}"%text) def put(self): text = self.get_argument("message") if text == "": text = "None" self.write("{'Put':'%s'}"% text) def delete(self): self.write("delete: " + self.get_argument("message", "None"))if __name__ == "__main__": tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(myapp()) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
3.nginx+tornado
server { listen 80; server_name tor.kkk.com; #index index.html index.htm index.php; root F:\my\www\tornado\tornado-yw; #网站目录必须添加,否则static无法访问,导致网站样式丢失 location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #access_log /web/log/tonado/access.log; #error_log /web/log/tornado/error.log;}