安装:
pip3 install tornado 源码安装 https://pypi.python.org/packages/source/t/tornado/tornado-4.3.tar.gz
简单入手
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") application = tornado.web.Application([ (r"/index", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
import tornado.ioloop import tornado.web from tornado import httpclient from tornado.web import asynchronous from tornado import gen # import uimodules as md # import uimethods as mt class MainHandler(tornado.web.RequestHandler): @asynchronous @gen.coroutine #没测出来效果(这两个装饰器) def get(self): print(‘start get ‘) http = httpclient.AsyncHTTPClient() http.fetch("https://www.google.com/", self.callback) #利用fetch发送一个异步请求(挂起) self.write(‘end‘) def callback(self, response): print(response.body,"---") settings = { ‘template_path‘: ‘template‘, ‘static_path‘: ‘static‘, ‘static_url_prefix‘: ‘/static/‘, # ‘ui_methods‘: mt, # ‘ui_modules‘: md, } application = tornado.web.Application([ (r"/index", MainHandler), ], **settings) if __name__ == "__main__": application.listen(8009) tornado.ioloop.IOLoop.instance().start()
配置静态路径
settings = { ‘template_path‘: ‘template‘, ‘static_path‘: ‘static‘, ‘static_url_prefix‘: ‘/static/‘, }
原文:https://www.cnblogs.com/yanxiaoge/p/10509034.html