Web框架之Tornado
阿新 • • 發佈:2019-03-11
bsp 安裝 get class list www rom nbsp gif
安裝:
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/‘, }
Web框架之Tornado