Tornado Study Notes

Source: Internet
Author: User

Tornado is a powerful and scalable Web Server written in Python. It is robust enough to handle severe network traffic and lightweight enough to be used in a large number of applications and tools.

Tornadoweb.org: a large number of examples and functional defects

Tornado version library on GitHub: Details and changes

Google group of Tornado: more specific questions

 

1. Tornado. Options: Read settings from the command line

from tornado.options import define, optionsdefine("port", default=8000, help="run on the given port", type=int)

This module specifies the port on which the application listens for HTTP requests.

Workflow: IfDefineThe setting with the same name in the statement is given in the command line, so it will become globalOptions. If you use--helpThe program will print all the options you define ('Port') andDefineFunctionHelpThe text specified in the parameter. If you do not specify a value for this option, useDefault. Tornado usageTypeThe parameter performs basic parameter type verification. An exception is thrown when an inappropriate type is given.

 

2. Tornado. Web. requesthandler: request processing function class of tornado

Built-in method:

Get_argument: Get query string parameters and parsePOSTContent

Write: Write a string as a function parameter to an HTTP response.

 

3.

app = tornado.web.Application(handlers=[    (r"/reverse/(\w+)", ReverseHandler),    (r"/wrap", WrapHandler)])

If the regular expression contains a series of extra parentheses, the matched string will be passed into the input parameter as an additional parameter according to the order in which the regular expression appears.

 

4. Tornado automatically sets the HTTP status code

404 not found

Tornado cannot match anyRequesthandlerReturns the 404 (not found) response code when the corresponding mode of the class.

400 bad request

If you callGet_argumentFunction, and no parameter for the given name is found, Tornado will automatically return a 400 (Bad request) response code.

405 method not allowed

If the incoming request usesRequesthandlerHTTP method not defined in (for example,PostRequest, but the processing function only definesGetMethod), Tornado returns a 405 (Methos not allowed) response code.

500 Internal Server Error

When a program encounters any error that cannot exit, Tornado returns the 500 (internal server error) response code. Any exceptions that are not captured in your code will also cause a 500 response code.

200 OK

If the response is successful and no other return code is set, Tornado returns a 200 (OK) response code by default.

 

5. templates: HTML files that can be embedded with Python code snippets.

6. template inheritance: Put {% extends "filename.html" %} on the top of the new template file}

7. A block statement can compress some template elements that you may want to change during expansion. For example, to use a dynamic headerblock that can write different pages, you can add the following code to the parent template main.html:

Insert the subtemplate into the overwriting content

 

Tornado Study Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.