zoukankan      html  css  js  c++  java
  • tornado 文档阅读笔记

    记录tornado文档中对自己有用的信息:

    A Tornado web application maps URLs or URL patterns to subclasses oftornado.web.RequestHandler. Those classes define get() or post() methods to handle HTTP GET or POST requests to that URL.


    The request handler can access the object representing the current request withself.request.The HTTPRequest object includes a number of useful attributes, including:

    • arguments - all of the GET and POST arguments
    • files - all of the uploaded files (via multipart/form-data POST requests)
    • path - the request path (everything before the ?)
    • headers - the request headers

    On every request, the following sequence of calls takes place:

    1. A new RequestHandler object is created on each request
    2. initialize() is called with keyword arguments from the Application configuration. (the initialize method is new in Tornado 1.1; in older versions subclasses would override __init__ instead). initialize should typically just save the arguments passed into member variables; it may not produce any output or call methods likesend_error.
    3. prepare() is called. This is most useful in a base class shared by all of your handler subclasses, as prepare is called no matter which HTTP method is used. prepare may produce output; if it calls finish (or send_error, etc), processing stops here.
    4. One of the HTTP methods is called: get()post()put(), etc. If the URL regular expression contains capturing groups, they are passed as arguments to this method.
    5. When the request is finished, on_finish() is called. For synchronous handlers this is immediately after get() (etc) return; for asynchronous handlers it is after the call tofinish().

    When a request handler is executed, the request is automatically finished. Since Tornado uses a non-blocking I/O style, you can override this default behavior if you want a request to remain open after the main request handler method returns using thetornado.web.asynchronous decorator.

    When you use this decorator, it is your responsibility to call self.finish() to finish the HTTP request, or the user’s browser will simply hang:

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

    Here is a real example that makes a call to the FriendFeed API using Tornado’s built-in asynchronous HTTP client:

    class MainHandler(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()

    When get() returns, the request has not finished. When the HTTP client eventually callson_response(), the request is still open, and the response is finally flushed to the client with the call to self.finish().

    For a more advanced asynchronous example, take a look at the chat example application, which implements an AJAX chat room using long polling. Users of long polling may want to override on_connection_close() to clean up after the client closes the connection (but see that method’s docstring for caveats).


  • 相关阅读:
    【原】git常见用法
    【转】EDID的简介和解析
    rsa公钥和私钥的生成
    往redis中存储数据是利用pipeline方法
    对于接口文档个的说明内容包括哪些
    blueprint的使用
    flask中如何生成迁移文件
    flask中自定义过滤器
    jsonify
    flask自定义处理错误方法
  • 原文地址:https://www.cnblogs.com/tangr206/p/3065155.html
Copyright © 2011-2022 走看看