Application configuration

classtornado.web.Application(handlers=Nonedefault_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 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 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 initialize method. This pattern is used for theStaticFileHandler in this example (note that a StaticFileHandler can 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_handlers 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 will serve those files from the /static/ URI (this is configurable with the static_url_prefix setting), and we will serve /favicon.ico and /robots.txt from the same directory. A custom subclass ofStaticFileHandler can be specified with the static_handler_class setting.

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 of RequestHandler). Some applications also like to use the 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: 如果设置 True,如果文件有变化进程将自动重启, 在 Debug mode and automatic reloading(DeBug模式和自动装载的情况下自动开启).
  • debug: 几种配置的集合, 具体查看 Debug mode and automatic reloading. 设置 debug=True 相当于设置 autoreload=True,compiled_template_cache=Falsestatic_hash_cache=Falseserve_traceback=True.
  • default_handler_class and default_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_modules and ui_methods: 配置 UIModule 或 UI methods配置模版可用的帮助方法. 可以是一个模块、字典或一个模块或者字典的列表. 更多细节查看 UI modules

认证和安全设置:

  • cookie_secret: 被 RequestHandler.get_secure_cookieset_secure_cookie用来配置cookie的标志
  • key_version: 被 set_secure_cookie 用来配置cookie的标志时cookie_secret的一个key
  • login_url: 如果没有用户登录这个 authenticated 装饰器将被重新定义到. 可以进一步重写 RequestHandler.get_login_url
  • xsrf_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_cookie for the XSRF cookie.
  • twitter_consumer_keytwitter_consumer_secretfriendfeed_consumer_key,friendfeed_consumer_secretgoogle_consumer_keygoogle_consumer_secretfacebook_api_key,facebook_secret: Used in the tornado.auth module 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_classstatic_handler_args: 可以自定义处理静态文件的动作和参数,而不是默认的 tornado.web.StaticFileHandlerstatic_handler_args, 如果设置了,应该有一个字典被传入到动作类的 initialize 方法中
listen(portaddress=''**kwargs)[source]

Starts an HTTP server for this application on the given port.

This is a convenience alias for creating an HTTPServer object and calling its listen method. Keyword arguments not supported by HTTPServer.listen are passed to the HTTPServerconstructor. For advanced uses (e.g. multi-process mode), do not use this method; create anHTTPServer and call its 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 HTTPServer object.

add_handlers(host_patternhost_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 name

The handler must be added to the application as a named URLSpec.

Args will be substituted for capturing groups in the URLSpec regex. 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类配置及使用的更多相关文章

  1. Tornado.web.Application之-settings

    应用程序配置 class  tornado.web.Application(handlers:List [Union [Rule,Tuple]] = None,default_host:str = N ...

  2. tornado框架源码分析---Application类之debug参数

    先贴上Application这个类的源码. class Application(httputil.HTTPServerConnectionDelegate): """A ...

  3. 如何关闭tornado.web的Application

    研究热更新Python程序时,需要将已有的HTTP服务器重启. 我的HTTP服务器是用tornado.web.Application生成的,这样很简单: import tornado.web weba ...

  4. 【bug】【yii】配置log时,报错 Setting read-only property: yii\web\Application::log

    Setting read-only property: yii\web\Application::log 配置放在了 components 外面,应该放在里面

  5. Python(九)Tornado web 框架

    一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...

  6. tornado web 框架的认识

    tornado 简介 1,概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的 ...

  7. tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片

    本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...

  8. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...

  9. Tornado web 框架

    Tornado web 框架 其实很简单.深度应用 一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像we ...

随机推荐

  1. Windows7上FTP服务器建立

    1. FTP服务器建立 注意:千万不能使用FTP和ftp建立用户,否则无法登陆ftp服务器. 1.1本地机器上创建一个用户 这个用户是用来登录到FTP的.我的电脑右键->管理->本地用户和 ...

  2. 高可用集群(HA)之Keeplived原理+配置过程

    原理--> 通过vrrp协议,定义虚拟路由,在多个服务节点上进行转移. 通过节点优先级,将初始虚拟路由到优先级高的节点上,checker工作进程检测到主节点出问题时,则降低此节点优先级,从而实现 ...

  3. C语言程序的结构分析

    一个C语言源程序可以由一个或多个源文件组成. 每个源文件可由一个或多个函数组成. 一个源程序不论由多少个文件组成,都有一个且只能有一个main函数,即主函数. 源程序中可以有预处理命令(include ...

  4. LINUX SSH客户端的中文乱码问题

       原因在于文件/etc/sysconfig/i18n 这个文件是系统的区域语言设置, i18n是 国际化internationalization的缩写 i和n之间正好18个字母 解释: LANG= ...

  5. Yslow-23条规则编辑

    1. 减少HTTP请求次数 合并图片.CSS.JS,改进首次访问用户等待时间.   2. 使用CDN 就近缓存==>智能路由==>负载均衡==>WSA全站动态加速   3. 避免空的 ...

  6. DropBox与Box的区别,包括直接的投资人的评价(本地Sync可能还是挺重要的)

    作者:曲凯链接:http://www.zhihu.com/question/22207220/answer/20642357来源:知乎著作权归作者所有,转载请联系作者获得授权. Box和Dropbox ...

  7. QAction系列详解

    QAction系列详解 一.QAction类详解 [详细描述] QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中.        应用程序可以通过菜单,工具栏按 ...

  8. Android原型界面设计工具

     第 1 页:原型界面制作工具Lumzy  第 2 页:在线工具Mockingbird  第 3 页:开源UI工具The Pencil Project  第 4 页:JS开发工具包Dojo  第 5 ...

  9. python使用post登陆电子科大信息门户并保存登陆后页面

    python使用post登陆电子科大信息门户并保存登陆后页面 作者:vpoet mail:vpoet_sir@163.com #coding=utf-8 import HTMLParser impor ...

  10. PHP伪静态与短链接

    如今,Web服务高速发展的时代,各式各类的门户网站,如新浪http://www.sina.com.腾讯http://www.qq.com,这些网站大家都很容易记住,因为这种名称都是有规则和含义的.如果 ...