• 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).


  • 相关阅读:
    HTML中超出的内容显示为省略号
    JS-复习整理
    js-实现鼠标滑轮滚动实现换页
    CSS 控制滚动条样式
    面向对象、继承、抽象方法重载知识点整理
    面向对象知识点
    复习HTML、CSS、JS练习题
    数据库---创建函数,存储函数,触发器实例
    ajax编写购物车遇到的问题
    ajax应用
  • 原文地址:https://www.cnblogs.com/tangr206/p/3065155.html
Copyright © 2020-2023  润新知