Tornado Learning (II)

We are in front of  Tornado  explained the organization's own code hello world, but no more in-depth and detailed explanation. Here we () function to start directly from the main, followed by single-step and see what tornado dry.

The following is a definition of main () function:

Copy the code
1    def main():
2        tornado.options.parse_command_line()
3        application = tornado.web.Application([
4            (r"/", MainHandler),
5        ])
6        http_server = tornado.httpserver.HTTPServer(application)
7        http_server.listen(options.port)
8        tornado.ioloop.IOLoop.instance().start()
Copy the code

Start with the first line:

1    tornado.options.parse_command_line()

This sentence is to parse command line parameters, functions are defined in tornado / options.py years. Note that there are two parse_command_line in options.py in () definition. It is a class member function OptionParser:

1    class OptionParser(object): 
2        def parse_command_line(self, args=None, final=True):

Another is a normal function, but its implementation is a direct call to achieve OptionParser.

1    def parse_command_line(args=None, final=True): 
2        return options.parse_command_line(args, final=final) 

Look in the direct realization OptionParser. Short comment, parse_command_line () sys.argv default parse the parameters. When we run helloworld.py, behind with parameters handled here. Parsing command line parameters is very simple, there is no need for special attention. Furthermore, we do not use any command-line arguments, so skip it.

Next:

1    application = tornado.web.Application([ 
2        (r"/", MainHandler), ]) 

This one is simple, but slightly larger amount of information. First, tornado.web.Application tornado is a built-in class, defined in the tornado / web.py in. Notes from a large section of it can be seen, at least in this class M II. By tornado statement, Application Handler class is a collection of some of these Handler make up a web application.

What is the handler it?

When a user visits your site, it will submit a URL, that "the data of this address to me", this URL may be a web page, such as http://abc.com /1.htm, it may be a pictures, such as http://abc.com/2.jpg. After the data is transferred to the browser, rendered, that is the way we see the website. For each such URL, and the tornado can be targeted provided a function to handle. For example, we give http://abc.com/1.htm a function called handle_1_htm () of, http: //abc.com/2.jpg to a call handle_2_jpg () function. They are responsible for the contents of the client request back to the client via http protocol. These functions, called handler.

Compared to normal static http, this handler mechanism provides more flexibility. For example, 1.htm possible according to the database every time dynamically generated content, and then returned to the user profile can each is different. Another example: might not 2.jpg such a file exists on the server, handle_2_jpg () function is actually shoot camera head from a server connected to a map, then returned to the user. With tornado development site, most of the work is written in such a  Handler .

Above this one, is to initialize an instance of the Application, the application is stored in a variable.

Application initialization parameter is very luxurious. In the description of the Application class, the words of a large segment of the parameters are talking about things.

The relatively simple example of parameters:

1    application = tornado.web.Application([(r"/", MainHandler), ])

We look __init__ function prototypes Applicatino class:

1    def __init__(self, handlers=None, default_host="", transforms=None, wsgi=False, **settings): 

Helloword.py visible only provides handlers value of this parameter. handlers is actually a list, each element (regexp, tornado.web.RequestHandler) this tuple. Recall that there is a mapping relationship between the user and the request handler, this association is defined herein.

1    (r"/", MainHandler)

This means that if a user requests a Web site root directory, call MainHandler to deal with. Earlier we direct input http://127.0.0.1:8888, in fact, the root access, we see hello world output is output by the MainHandler. Then followed by detailed analysis MainHandler.

If we do not access a URL handler definitions (such as http://127.0.0.1/1.htm) What happens?

Very simple, to tell you can not find! Then an experiment, the

1    application = tornado.web.Application([(r"/", MainHandler),])

Read:

1    application = tornado.web.Application([ (r"/hello\.htm", MainHandler), ]) 

Note that, because it is a regular expression, so we added a backslash before the dot. And then try to access http://127.0.0.1:8888, will be the 404.

In other words we turn over the URL, http: //127.0.0.1: 8888 / hello.htm, lovely "hello world" is back.

Now you should understand handler how it happens.

At the same time it reminds us, URL in the browser address bar does not necessarily mean that there is really such a file on the server. It is entirely possible that a web server virtual path mappings only. For example, just hello.htm.

Since this is an example of hello world level, so only one handler, and very simple.

 

Guess you like

Origin www.cnblogs.com/wangzhilong/p/12549455.html