首页 > Web开发 > 详细

Web框架之Tornado

时间:2019-03-11 11:12:32      阅读:158      评论:0      收藏:0      [点我收藏+]

 安装:

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

原文:https://www.cnblogs.com/yanxiaoge/p/10509034.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!