tornado [极轻量级Web服务器框架]

    # 高可伸缩性和epoll非阻塞IO,响应快速,可处理数千并发连接,特别适用用于实时的Web服务 底层是gevent协程
    # http://www.tornadoweb.cn/documentation
    # http://old.sebug.net/paper/books/tornado/#_2

    pip install tornado

tornado 源码分析系列目录

        tornado 简介: http://www.cnblogs.com/Bozh/archive/2012/07/17/2596458.html
        tornado 网络层IOLoop: http://www.cnblogs.com/Bozh/archive/2012/07/18/2597114.html
        tornado 网络层IOLoop: http://www.cnblogs.com/Bozh/archive/2012/07/19/2598696.html
        tornado Buffer层IOStream: http://www.cnblogs.com/Bozh/archive/2012/07/20/2600520.html
        tornado HTTPServer层: http://www.cnblogs.com/Bozh/archive/2012/07/22/2603963.html
        tornado HTTPServer详解: http://www.cnblogs.com/Bozh/archive/2012/07/24/2606765.html


    get_argument()                # 方法来获取查询字符串参数,以及解析 POST 的内容
    self.request.arguments        # 所有的 GET 或 POST 的参数
    self.request.files            # 所有通过 multipart/form-data POST 请求上传的文件
    self.request.path             # 请求的路径( ? 之前的所有内容)
    self.request.headers          # 请求的开头信息
    callback                      # 执行完成后执行回调函数

    @tornado.web.asynchronous     # 非阻塞异步装饰器
    self.finish()                 # 使用非阻塞异步 必须调用 self.finish() 已完成 HTTTP 请求
    # 异步 HTTP 客户端 两种模式 默认 SimpleAsyncHTTPClient  如果要修改为 CurlAsyncHTTPClient
    AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient')

    import tornado.ioloop
    import tornado.web

    class MainHandler(tornado.web.RequestHandler):
    import tornado.ioloop
    import tornadotornado.web
    import tornado.httpclient


    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("Hello, world")

        def post(self):
            self.set_header("Content-Type", "text/plain")
            self.write("You wrote " + self.get_argument("message"))

    class Template(tornado.web.RequestHandler):
        def get(self):
            items = ["Item 1", "Item 2", "Item 3"]
            self.render("template.html", title="My title", items=items)

    class urlhttp(tornado.web.RequestHandler):
        @tornado.web.asynchronous
        def get(self):
            http = tornado.httpclient.AsyncHTTPClient()
            http.fetch("http://friendfeed-api.com/v2/feed/bret", callback=self.on_response)

        def on_response(self, response):
            if response.error: raise tornado.web.HTTPError(500)
            json = tornado.escape.json_decode(response.body)
            self.write("Fetched " + str(len(json["entries"])) + " entries "
                       "from the FriendFeed API")
            self.finish()

    class StoryHandler(tornado.web.RequestHandler):
        def get(self, story_id):
            self.write("You requested the story " + story_id)

    def make_app():
        return tornado.web.Application([
            (r"/", MainHandler),
            (r"/template", Template),
            (r"/story/([0-9]+)", StoryHandler),
        ])

    if __name__ == "__main__":
        app = make_app()
        app.listen(8888)
        tornado.ioloop.IOLoop.current().start()
文档更新时间: 2018-11-21 17:54   作者:RuM