Tornado learning (c)

In fact handler a lot of stress, in the comment Application class, we talk a lot.

1. First, (regexp, tornado.web.RequestHandler) in the first parameter is not an ordinary string, but a regular expression, which is why the example it took a prefix "r". This shows that a handler can match many a request. As long as they can meet the request url regexp match.

If we just

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

Change

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

Only removed a backslash, meaning it became pale. Regular expression, dot. "" Represent any one character.

Then the following URL will output the "Hello World":

1    http://127.0.0.1:8888/hello.htm
2    http://127.0.0.1:8888/hello_htm
3    http://127.0.0.1:8888/hello1htm

The following URL because they do not conform to the rules [r "/hello.htm"], it will output 404 Not Found.

1    http://127.0.0.1:8888/hello.html
2    http://127.0.0.1:8888/hello11htm

In addition, this URL will return 404 Not Found, you know why?

1    http://127.0.0.1:8888/hello?htm

2. You can define multiple handler tuple, form a list (you may have noticed, the example application parameter is a tuple list). When a user request comes, will be followed by the list handler to match, the first match found (not the most accurate of the match) will be called.

3. Each tuple can have an optional third element. This element is a dictionary object, it will transfer to the tuple Handler function as a parameter. such as:

1    application = web.Application([ 
2        (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}), 
3    ])

4. In addition to directly define constructor Application Handler outer, application objects can be invoked which add_handlers () method to complete. This function has an additional feature that supports Virtual Host function.

1    application.add_handlers(r"www\.nowamagic\.net", [(r"/article/([0-9]+)", ArticleHandler), ]) 

Above, www.nowamagic.net is a Virtual Host. Virtual Host What is it?

Conventionally, an IP corresponding to a domain name, a Web site, Virtual Host is to make possible the presence of multiple domain names on a single IP, each domain name corresponds to a different site.

To achieve this function is very simple, there are many online introduction, not wordy.

5. Static file how to do it? Such as image files, the server only thing to do is to put pictures directly back to the customer, which is the default web server behavior. We do not need handler to handle (not saying no, but suspected it superfluous). Application of the handler also provides a convenient static files.

Tornado default the "/ static /" subdirectory under the root directory of the site are viewed as a static route, visit this following file handler does not require a dynamic process, webserver automatically moves the file back to the client. It is suitable to put some pictures do not need a dynamic process, css stylesheets, music and more.

Of course, this path can make their own adjustments, specify the settings parameters Application .__ init__ function.

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

6. As you can see, settings parameter is a dictionary. There are some miscellaneous settings. Below is an example.

Copy the code
01    settings = dict( 
02        blog_title=u"nowamagic",
03        template_path=os.path.join(os.path.dirname(__file__), "templates"), 
04        static_path=os.path.join(os.path.dirname(__file__), "static"), 
05        ui_modules={"Entry1": EntryModule, "topx": TopXModule}, 
06        xsrf_cookies=True,
07        cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
08        autoescape=None,
09        debug=True,
10    )
Copy the code

settings which provide both tornado built-in options (such as static_path, cokeei_secret, debug, etc.), but also user-defined options (such as blog_title). Application will be handled by the built-in option classes themselves, custom options require the user to write their own procedures.

 

Guess you like

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