tornado.web.StaticFileHandler
tornado.web.StaticFileHandler
源代码中的解释
class StaticFileHandler(RequestHandler):
"""A simple handler that can serve static content from a directory.
A `StaticFileHandler` is configured automatically if you pass the
``static_path`` keyword argument to `Application`. This handler
can be customized with the ``static_url_prefix``, ``static_handler_class``,
and ``static_handler_args`` settings.
To map an additional path to this handler for a static data directory
you would add a line to your application like::
application = web.Application([
(r"/content/(.*)", web.StaticFileHandler, {"path": "/var/www"}),
])
The handler constructor requires a ``path`` argument, which specifies the
local root directory of the content to be served.
Note that a capture group in the regex is required to parse the value for
the ``path`` argument to the get() method (different than the constructor
argument above); see `URLSpec` for details.
To serve a file like ``index.html`` automatically when a directory is
requested, set ``static_handler_args=dict(default_filename="index.html")``
in your application settings, or add ``default_filename`` as an initializer
argument for your ``StaticFileHandler``.
To maximize the effectiveness of browser caching, this class supports
versioned urls (by default using the argument ``?v=``). If a version
is given, we instruct the browser to cache this file indefinitely.
`make_static_url` (also available as `RequestHandler.static_url`) can
be used to construct a versioned url.
This handler is intended primarily for use in development and light-duty
file serving; for heavy traffic it will be more efficient to use
a dedicated static file server (such as nginx or Apache). We support
the HTTP ``Accept-Ranges`` mechanism to return partial content (because
some browsers require this functionality to be present to seek in
HTML5 audio or video).
**Subclassing notes**
This class is designed to be extensible by subclassing, but because
of the way static urls are generated with class methods rather than
instance methods, the inheritance patterns are somewhat unusual.
Be sure to use the ``@classmethod`` decorator when overriding a
class method. Instance methods may use the attributes ``self.path``
``self.absolute_path``, and ``self.modified``.
Subclasses should only override methods discussed in this section;
overriding other methods is error-prone. Overriding
``StaticFileHandler.get`` is particularly problematic due to the
tight coupling with ``compute_etag`` and other methods.
To change the way static urls are generated (e.g. to match the behavior
of another server or CDN), override `make_static_url`, `parse_url_path`,
`get_cache_time`, and/or `get_version`.
To replace all interaction with the filesystem (e.g. to serve
static content from a database), override `get_content`,
`get_content_size`, `get_modified_time`, `get_absolute_path`, and
`validate_absolute_path`.
.. versionchanged:: 3.1
Many of the methods for subclasses were added in Tornado 3.1.
"""
关于前后端的两种渲染方式
模板渲染, 使用模板语言进行渲染
前后端分离, 前后端开发前定义好api接口, 使用ajax调用这些接口, 就能同时开发, 后端只管返回规定的数据
tornado.web.StaticFileHandler
是tornado用来提供静态资源文件的handler
import os
current_path = os.path.dirname(__file__)
app = tornado.web.Application(
[
(r'^/(.*?)$', StaticFileHandler, {"path":os.path.join(current_path, "templates"), "default_filename":"index.html"}),
],
static_path=os.path.join(current_path, "statics"),
)
path : 用来提供
html
文件的根路径, 并在此目录中寻找在url中用正则表达式提取的文件名的值default_filename : 用来指定访问路由中未指明文件时, 默认提供的文件
static_path: 提供静态文件的位置
把以上信息配置好后, 项目应该就能拉起来了
tornado.web.StaticFileHandler的更多相关文章
- tornado.web.Application类配置及使用
Application configuration classtornado.web.Application(handlers=None, default_host='', transforms=No ...
- 20-2 树莓派搭建服务器 Tornado Web服务器
Drive.google.com/drive/folders/1ahbeoEHkjxoo4NV1wReOmpoRWbl448z- 1.Tornado简介 Tornado一款使用 Python 编写的, ...
- Tornado.web.Application之-settings
应用程序配置 class tornado.web.Application(handlers:List [Union [Rule,Tuple]] = None,default_host:str = N ...
- Tornado WEB服务器框架 Epoll
引言: 回想Django的部署方式 以Django为代表的python web应用部署时采用wsgi协议与服务器对接(被服务器托管),而这类服务器通常都是基于多线程的,也就是说每一个网络请求服务器都会 ...
- Tornado 模板(StaticFileHandler/static_path/template_path等) 笔记
使用StaticFileHandler进行首页默认访问页面,最好将StaticFileHandler放在最后面,这样不会覆盖要匹配自定义的路径 import tornado.web import to ...
- Python(九)Tornado web 框架
一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...
- tornado web 框架的认识
tornado 简介 1,概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Python 的 ...
- tornado 学习笔记9 Tornado web 框架---模板(template)功能分析
Tornado模板系统是将模板编译成Python代码. 最基本的使用方式: t = template.Template("<html>{{ myv ...
- tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tornado的后端和ajax的 ...
随机推荐
- gcc,一个神奇的编译器
gcc是“GNU Compiler Collection”的缩写,从字面意思可以知道它是一个编译器集.gcc不止可以编译器c语言,还能用于c++,java,object-C等语言程序.但是在这里,我们 ...
- hdu 1042 N! java大数及判断文件末尾
N! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submi ...
- linux 查找文件并移动
find . -name '10-*.dat' -exec mv {} ../ \; 这里: => -exec mv {} /mnt/mp3 \; - 运行mv命令. => {} ...
- linux学习笔记22---命令diff和diff3
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...
- HTML标签及属性大全
HTML标签及属性大全 基本结构标签: <HTML>,表示该文件为HTML文件 <HEAD>,包含文件的标题,使用的脚本,样式定义等 <TITLE>---</ ...
- Vector类与Enumeration接口
Vector类用于保存一组对象,由于java不支持动态数组,Vector可以用于实现跟动态数组差不多的功能.如果要将一组对象存放在某种数据结构中,但是不能确定对象的个数时,Vector是一个不错的选择 ...
- source insight Confirm by typing ‘yes' below"、"has been changed outside of the editor. Do you want to reload the file?"、“
阅读内核代码习惯和喜欢使用source insight.如果能在source insight上修改内核代码,同时又不需要把修改的内核代码再拷贝到虚拟ubuntu上去那就方便了.于是想通过用samba与 ...
- 房间WIFI信号不好怎么办?——无线路由桥接(WDS)
背景 1.无线路由在客厅,房间的WIFI信号很差,只有1-2格,虽说是100M的网速,但是从客厅到房间要经过3道墙!电脑的无线接收功能一般都很一般,网速不好理所当然. 2.弄一根网线从客厅连接起来很傻 ...
- Volley 源码分析
Volley 源码分析 图片分析 要说源码分析,我们得先看一下官方的配图: 从这张图中我们可以了解到 volley 工作流程: 1.请求加入优先队列 2.从缓存调度器中查看是否存在该请求,如果有(没有 ...
- 数据结构 + 算法 -> 收集
董的博客:数据机构与算法合集 背包问题应用(2011-08-26) 数据结构之红黑树(2011-08-20) 素数判定算法(2011-06-26) 算法之图搜索算法(一)(2011-06-22) 算法 ...