tornado.web.Application类配置及使用
Application configuration
- class
tornado.web.Application(handlers=None, default_host='', transforms=None, **settings)[source] -
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:
application = web.Application([
(r"/", MainPageHandler),
])
http_server = httpserver.HTTPServer(application)
http_server.listen(8080)
ioloop.IOLoop.current().start()The constructor for this class takes in a list of
URLSpecobjects 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
URLSpecconstructor. (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
initializemethod. This pattern is used for theStaticFileHandlerin this example (note that aStaticFileHandlercan be installed automatically with the static_path setting described below):application = web.Application([
(r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}),
])We support virtual hosts with the
add_handlersmethod, 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_pathsetting as a keyword argument. We will serve those files from the/static/URI (this is configurable with thestatic_url_prefixsetting), and we will serve/favicon.icoand/robots.txtfrom the same directory. A custom subclass ofStaticFileHandlercan be specified with thestatic_handler_classsetting.settings-
Additional keyword arguments passed to the constructor are saved in the
settingsdictionary, 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 ofRequestHandler). Some applications also like to use thesettingsdictionary as a way to make application-specific settings available to handlers without using global variables. Settings used in Tornado are described below.一般设置:
autoreload: 如果设置True,如果文件有变化进程将自动重启, 在 Debug mode and automatic reloading(DeBug模式和自动装载的情况下自动开启).debug: 几种配置的集合, 具体查看 Debug mode and automatic reloading. 设置debug=True 相当于设置autoreload=True,compiled_template_cache=False,static_hash_cache=False,serve_traceback=True.default_handler_classanddefault_handler_args: 在页面没有找到(404错误的时候)自定义404错误页视图动作类及参数compress_response: 如果设置True, responses(响应)将被自动压缩gzip: 在Tornado 4.0被compress_response代替log_function: 这个函数用来回调RequestHandler对象的处理结果,默认主程序导入logging并配置好的话会自动记录.也可以定制Application.log_request这个方法.serve_traceback: 如果设置 true,错误页面将包含错误跟踪ui_modulesandui_methods: 配置UIModule或 UI methods配置模版可用的帮助方法. 可以是一个模块、字典或一个模块或者字典的列表. 更多细节查看 UI modules
认证和安全设置:
cookie_secret: 被RequestHandler.get_secure_cookie和set_secure_cookie用来配置cookie的标志key_version: 被set_secure_cookie用来配置cookie的标志时cookie_secret的一个keylogin_url: 如果没有用户登录这个authenticated装饰器将被重新定义到. 可以进一步重写RequestHandler.get_login_urlxsrf_cookies: If true, Cross-site request forgery protection will be enabled.xsrf_cookie_version: 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_kwargs: May be set to a dictionary of additional arguments to be passed toRequestHandler.set_cookiefor 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 thetornado.authmodule to authenticate to various APIs.
模版设置:
autoescape: 制对模板的自动转义. 可以被设置为 None 以禁止转义, 或设置为一个所有输出都该传递过数 name . 默认是 "xhtml_escape". 可以在每个模板中改变使用 {% 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; 如果False静态 urls 将重新加载静态文件static_path: 静态文件的目录static_url_prefix: 静态文件的Url前缀, 默认为"/static/".static_handler_class,static_handler_args: 可以自定义处理静态文件的动作和参数,而不是默认的tornado.web.StaticFileHandler.static_handler_args, 如果设置了,应该有一个字典被传入到动作类的initialize方法中
listen(port, address='', **kwargs)[source]-
Starts an HTTP server for this application on the given port.
This is a convenience alias for creating an
HTTPServerobject and calling its listen method. Keyword arguments not supported byHTTPServer.listenare passed to theHTTPServerconstructor. For advanced uses (e.g. multi-process mode), do not use this method; create anHTTPServerand call itsTCPServer.bind/TCPServer.startmethods directly.Note that after calling this method you still need to call
IOLoop.current().start()to start the server.Returns the
HTTPServerobject.Changed in version 4.3: Now returns the
HTTPServerobject.
add_handlers(host_pattern, host_handlers)[source]-
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.
reverse_url(name, *args)[source]-
Returns a URL path for handler named
nameThe handler must be added to the application as a named
URLSpec.Args will be substituted for capturing groups in the
URLSpecregex. They will be 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 change this behavior either subclass Application and override this method, or pass a function in the application settings dictionary as
log_function.
tornado.web.Application类配置及使用的更多相关文章
- Tornado.web.Application之-settings
应用程序配置 class tornado.web.Application(handlers:List [Union [Rule,Tuple]] = None,default_host:str = N ...
- tornado框架源码分析---Application类之debug参数
先贴上Application这个类的源码. class Application(httputil.HTTPServerConnectionDelegate): """A ...
- 如何关闭tornado.web的Application
研究热更新Python程序时,需要将已有的HTTP服务器重启. 我的HTTP服务器是用tornado.web.Application生成的,这样很简单: import tornado.web weba ...
- 【bug】【yii】配置log时,报错 Setting read-only property: yii\web\Application::log
Setting read-only property: yii\web\Application::log 配置放在了 components 外面,应该放在里面
- Python(九)Tornado web 框架
一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...
- tornado web 框架的认识
tornado 简介 1,概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的 ...
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
- 浅析tornado web框架
tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...
- Tornado web 框架
Tornado web 框架 其实很简单.深度应用 一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像we ...
随机推荐
- 分享几个实用的jquery工具函数
1.$.browser对象属性 属性列表 说明 webkit webkit相关浏览器则返回true,否则返回false,如google,傲游. mozilla mozilla相关浏览器则返回tru ...
- sqlsever连接两个不同服务器上的数据库进行查询
exec sp_addlinkedserver 'LocalSERVER' ,--别名,自己起一个 '' , 'SQLOLEDB' , 'ip' --远程服务器地址 exec sp_addlinked ...
- USB系列之八:透过ASPI执行SCSI命令
在<USB系列之七>里我们介绍了ASPI的规范,并对一系列ASPI的命令做了测试,其中的02号命令是执行SCSI命令,我们专门在这篇文章中介绍,在<USB系列七>中,我们已经了 ...
- 对$NOMOD51的理解
很多朋友在看asm代码的时候,对下面的语句不是很了解,下面解说一下. $NOMOD51 $INCLUDE (REG932.INC) 解释:$NOMOD51,这一指令功能是使A51不识别8051的所有预 ...
- CircleImageView自定义圆形控件的使用
1.自定义圆形控件github地址: https://github.com/hdodenhof/CircleImageView 主要的类: package de.hdodenhof.circleima ...
- mac安装GNU命令行工具
mac安装GNU命令行工具 2.添加的repo tap home/dupes brew install coreutils binutils diffutils ed -- ...
- 《深入了解 Linq to SQL》之对象的标识 —— 麦叔叔呕心呖血之作
序言 很多朋友都向我提过,希望我写一下关于Linq to SQL 或者 VS 插件方面的文章.尽管市面上有很多 Linq to SQL 的书籍,但是都是介绍怎么用,缺乏深度.关于 VS 插件方面的书籍 ...
- 【转】ubuntu下putty的复制粘贴 -- 不错
原文网址:http://www.nwber.com/?p=165 今天在ubutnu下想用putty玩玩,突然发现在windows里直接点击右键的复制居然不管用了,调设置也没有用.这可麻烦了,要是手动 ...
- cf478D Red-Green Towers
D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【排序】表插入排序算法(C语言版)
排序耗时的操作主要分为两种:查找比较.记录移位. 1.表插入排序 在查找比较基础上,尽量减少记录移位步数,可以令排序操作耗时降低,表插入排序正是为减少移位次数而出现的. 在数据结构上,数据是存储在静态 ...