Application configuration
- class
-
A collection of request handlers that make up a web application.
Instances of this class are callable and can be passed directly to HTTPServer to serve the application:
The constructor for this class takes in a list ofapplication = web.Application([ (r"/", MainPageHandler), ]) http_server = httpserver.HTTPServer(application) http_server.listen(8080) ioloop.IOLoop.current().start()
URLSpec
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 regexp matches the request path. The request class can be specified as either a class object or a (fully-qualified) name.
Each tuple can contain additional elements, which correspond to the arguments to the
URLSpec
constructor. (Prior to Tornado 3.2, only tuples of two or three elements were allowed).
A dictionary may be passed as the third element of the tuple, which will be used as keyword arguments to the handler’s constructor and
method. This pattern is used for theinitialize
in this example (note that aStaticFileHandler
can be installed automatically with the static_path setting described below):StaticFileHandler
We support virtual hosts with theapplication = web.Application([ (r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}), ])
method, which takes in a host regular expression as the first argument:add_handlers
You can serve static files by sending theapplication.add_handlers(r"www\.myhost\.com", [ (r"/article/([0-9]+)", ArticleHandler), ])
setting as a keyword argument. We will serve those files from thestatic_path
URI (this is configurable with the/static/
setting), and we will servestatic_url_prefix
and/favicon.ico
from the same directory. A custom subclass of/robots.txt
can be specified with theStaticFileHandler
setting.static_handler_class
- Additional keyword arguments passed to the constructor are saved in the
dictionary, and are often referred to in documentation as “application settings”. Settings are used to customize various aspects of Tornado (although in some cases richer customization is possible by overriding methods in a subclass ofsettings
). Some applications also like to use theRequestHandler
settings
dictionary as a way to make application-specific settings available to handlers without using global variables. Settings used in Tornado are described below.
一般設定:
-
: 如果設定autoreload
,如果檔案有變化程序将自動重新開機, 在 Debug mode and automatic reloading(DeBug模式和自動裝載的情況下自動開啟).True
-
: 幾種配置的集合, 具體檢視 Debug mode and automatic reloading. 設定debug
debug=True 相當于設定
,autoreload=True
,compiled_template_cache=False
,static_hash_cache=False
.serve_traceback=True
-
anddefault_handler_class
: 在頁面沒有找到(404錯誤的時候)自定義404錯誤頁視圖動作類及參數default_handler_args
-
: 如果設定compress_response
, responses(響應)将被自動壓縮True
-
: 在Tornado 4.0被gzip
compress_response代替
-
: 這個函數用來回調log_function
對象的處理結果,預設主程式導入RequestHandler
.也可以定制logging并配置好的話會自動記錄
.Application.log_request這個方法
-
: 如果設定 true,錯誤頁面将包含錯誤跟蹤serve_traceback
-
andui_modules
: 配置ui_methods
或 UI methods配置模版可用的幫助方法. 可以是一個子產品、字典或一個子產品或者字典的清單. 更多細節檢視 UI modulesUIModule
-
: 被cookie_secret
RequestHandler.get_secure_cookie和
set_secure_cookie用來配置cookie的标志
-
: 被key_version
用來配置cookie的标志時set_secure_cookie
cookie_secret的一個key
-
: 如果沒有使用者登入這個login_url
裝飾器将被重新定義到. 可以進一步重寫authenticated
RequestHandler.get_login_url
-
: If true, Cross-site request forgery protection will be enabled.xsrf_cookies
-
: Controls the version of new XSRF cookies produced by this server. Should generally be left at the default (which will always be the highest supported version), but may be set to a lower value temporarily during version transitions. New in Tornado 3.2.2, which introduced XSRF cookie version 2.xsrf_cookie_version
-
: May be set to a dictionary of additional arguments to be passed toxsrf_cookie_kwargs
for the XSRF cookie.RequestHandler.set_cookie
-
,twitter_consumer_key
,twitter_consumer_secret
,friendfeed_consumer_key
,friendfeed_consumer_secret
,google_consumer_key
,google_consumer_secret
,facebook_api_key
: Used in thefacebook_secret
module to authenticate to various APIs.tornado.auth
-
: 制對模闆的自動轉義. 可以被設定為 None 以禁止轉義, 或設定為一個所有輸出都該傳遞過數 name . 預設是 "xhtml_escape". 可以在每個模闆中改變使用 {% autoescape %} 指令.autoescape
-
: 預設compiled_template_cache
; 如果True
False 每次請求将重新加載模版
-
: 模版檔案目錄. 可以被template_path
RequestHandler.get_template_path擷取進行重寫
-
: 配置設定一個template_loader
.如果設定了tornado.template.BaseLoader進行模版加載
template_path和
. 可以被autoescape将失效
.RequestHandler.create_template_loader進一步重寫
-
: 對于模闆中的空白處理; 詳細用法請看template_whitespace
tornado.template.filter_whitespace
-
: 預設static_hash_cache
; 如果True
靜态 urls 将重新加載靜态檔案False
-
: 靜态檔案的目錄static_path
-
: 靜态檔案的Url字首, 預設為static_url_prefix
."/static/"
-
,static_handler_class
: 可以自定義處理靜态檔案的動作和參數,而不是預設的static_handler_args
.tornado.web.StaticFileHandler
, 如果設定了,應該有一個字典被傳入到動作類的static_handler_args
方法中initialize
-
settings
-
Starts an HTTP server for this application on the given port.
This is a convenience alias for creating an
object and calling its listen method. Keyword arguments not supported byHTTPServer
are passed to theHTTPServer.listen
constructor. For advanced uses (e.g. multi-process mode), do not use this method; create anHTTPServer
and call itsHTTPServer
/TCPServer.bind
TCPServer.start
methods directly.
Note that after calling this method you still need to call
IOLoop.current().start()
to start the server.
Returns the
HTTPServer
object.
Changed in version 4.3: Now returns the
object.HTTPServer
(port, address='', **kwargs)[source]listen
-
Appends the given handlers to our handler list.
Host patterns are processed sequentially in the order they were added. All matching patterns will be considered.
(host_pattern, host_handlers)[source]add_handlers
- Returns a URL path for handler named
The handler must be added to the application as a namedname
URLSpec
.
Args will be substituted for capturing groups in the
regex. They will be converted to strings if necessary, encoded as utf8, and url-escaped.URLSpec
(name, *args)[source]reverse_url
-
Writes a completed HTTP request to the logs.
By default writes to the python root logger. To change this behavior either subclass Application and override this method, or pass a function in the application settings dictionary as
.log_function
(handler)[source]log_request
- Additional keyword arguments passed to the constructor are saved in the
tornado.web.
Application
(handlers=None, default_host='', transforms=None, **settings)[source] 轉載于:https://www.cnblogs.com/goldd/p/5824538.html