A collection of request handlers that make up a Web application.
Instances of this class is callable and can be passed directly to Httpserver to serve the application:
application = web. Application ([ (r "/" ), ) http_server = httpserver. Httpserver (application) http_server listen (8080) ioloop ioloop. Current () . Start ()
The constructor for this class takes in a list URLSpec of objects or (RegExp, request_class) tuples. When we receive requests, we iterate over the list in order and instantiate an instance of the first request class whose R EGEXP matches the request path. The request class can be specified as either a class object or a (fully-qualified) name.
Each of the tuple can contain additional elements, which correspond to the arguments URLSpec . (Prior to Tornado 3.2, only tuples of or three elements were allowed).
A dictionary may be passed as the third element of the tuple, which'll be used as keyword arguments to the handler ' s Constructor And initialize method. This pattern was used for Thestaticfilehandler in This example (note that A staticfilehandler can is installed automatically with the Static_path setting described below):
Web. Application(R "/static/(. *)"Web. StaticFileHandler{"path""/var/www"}),])
We support Virtual hosts add_handlers with the method, which takes in a host regular expression as the first argument:
application. Add_handlers (r "www\.myhost\.com" [ (r "/article/([0-9]+)" articlehandler ), )
You can serve static files by sending the static_path setting as a keyword argument. We'll serve those files from /static/ the URI (this static_url_prefix was configurable with the setting), and we'll serve and /favicon.ico from the same directory. A Custom subclass of StaticFileHandler can is specified with the static_handler_class setting.
-
-
settings
-
Additional keyword arguments passed settings to the constructor is saved in the dictionary, and is often referred to in docu Mentation as "Application Settings". Settings was used to customize various aspects of Tornado (although in some cases richer customization was possible by Riding methods in a subclass of RequestHandler ). Some applications also like-to-use the dictionary as a-to-do settings application-specific settings available to handle RS without using global variables. Settings used in Tornado is described below.
General Settings:
autoreload: If the True file has a change process will automatically restart, in the debug mode and automatic reloading (in the case of debug modes and automatic loading, automatically open).
debug: A collection of several configurations, specifically view the Debug mode and automatic reloading. Settings debug=True 相当于设置 autoreload=True , compiled_template_cache=False , static_hash_cache=False , serve_traceback=True .
default_handler_classdefault_handler_argsand: on page not found (when 404 error) Custom 404 error Page View action class and parameters
compress_response: If set True , responses (response) will be automatically compressed
gzip: In Tornado 4.0 wascompress_response代替
log_function: This function is used to callback RequestHandler the object's processing result, the default main program import logging并配置好的话会自动记录 . can also be customized Application.log_request这个方法 .
serve_traceback: If set to True, the error page will contain error tracking
ui_modulesui_methodsand: A UIModule help method that is available for configuration or UI methods configuration templates. Can be a module, a dictionary, or a list of modules or dictionaries. More details View UI modules
Authentication and security settings:
cookie_secret: WasRequestHandler.get_secure_cookie和set_secure_cookie用来配置cookie的标志
key_version: set_secure_cookie when used to configure a cookie flagcookie_secret的一个key
login_url: This adorner will be redefined if no user is logged in authenticated . Can be further rewrittenRequestHandler.get_login_url
xsrf_cookies: If true, Cross-site request forgery protection would be enabled.
xsrf_cookie_version: Controls The version of new XSRF cookies produced by this server. Should generally be-left at the default (which'll always be the highest supported version), and May is set to a lower VA Lue temporarily during version transitions. New in Tornado 3.2.2, which introduced XSRF cookie version 2.
xsrf_cookie_kwargs : May is set to a dictionary of Additional arguments to be passed To Requesthandler.set_cookie for the XSRF cookie.
twitter_consumer_key,,,,,,, twitter_consumer_secret friendfeed_consumer_key friendfeed_consumer_secret google_consumer_key google_consumer_secret facebook_api_key , facebook_secret : Used in the module to authenticate to tornado.auth various APIs.
Stencil settings:
autoescape: Automatic escaping of the template. Can be set to none to prevent escaping, or set to one of all outputs that pass the several name. The default is "Xhtml_escape". You can change the use {% autoescape%} directive in each template.
compiled_template_cache: Default True ;False 每次请求将重新加载模版
template_path: Template file directory. Can beRequestHandler.get_template_path获取进行重写
template_loader: Assign one tornado.template.BaseLoader进行模版加载 . If set template_path和 autoescape将失效 . can be RequestHandler.create_template_loader进一步重写 .
template_whitespace: For blank handling in templates; For detailed usage please seetornado.template.filter_whitespace
Static File Settings:
static_hash_cache: Default True ; if False static URLs reload static files
static_path: Directory of static files
static_url_prefix: The URL prefix of the static file, which is the default "/static/" .
static_handler_class, static_handler_args : You can customize the actions and parameters that handle static files, not the default tornado.web.StaticFileHandler . static_handler_args if set, a dictionary should be passed into the method of the Action class initialize
-
-
listen (Port, address= ", **kwargs)[source]
-
Starts an HTTP server for this application on the given port.
This was a convenience alias for creating An Httpserver object and calling its listen method. Keyword arguments not supported By Httpserver.listen are passed to The httpserver constructor. For the advanced uses (e.g Multi-process mode), does not use the This method; Create Anhttpserver and Call Its tcpserver.bind /tcpserver.start methods directly.
Note that after calling this method, you still need to the start of the IOLoop.current().start() server.
Returns the HTTPServer object.
Changed in version 4.3:now returns the HTTPServer object.
-
add_handlers Span class= "Sig-paren" > ( host_pattern , host_handlers ) [source]
-
Appends the given handlers to our handler list.
Host patterns is processed sequentially in the order they were added. All matching patterns would be considered.
-
reverse_url name , *args [ SOURCE]
-
Returns a URL path for handler named name
The handler must be added to the application as a named urlspec .
Args would be substituted-capturing groups in The urlspec regex. They'll is converted to strings if necessary, encoded as UTF8, and url-escaped.
-
log_request (handler)[source]
-
Writes a completed HTTP request to the logs.
By default writes to the Python root logger. To behavior either subclass application and override this method, or pass a function in the application Settin GS dictionary as log_function .