记录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:
- A new RequestHandler object is created on each request
- 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.
- 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.
- 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.
- 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).