Tornado-StaticFileHandler参考
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-StaticFileHandler参考的更多相关文章
- Python高性能编程
一.进程池和线程池 1.串行 import time import requests url_lists = [ 'http://www.baidu.com', 'http://fanyi.baidu ...
- Python3.7 + jupyter安装(CentOS6.5)
Python3.7 + jupyter安装(CentOS6.5) 方法一(anaconda): anaconda是一个开源的Python发行版本 包含conda,python等大量的科学包以及依赖 优 ...
- 发布一个参考tornado的高性能c++网络库:libtnet
libtnet是一个用c++编写的高性能网络库,它在设计上面主要参考tornado,为服务端网络编程提供简洁而高效的接口,非常易于使用. Echo Server void onConnEvent(co ...
- Tornado 模板(StaticFileHandler/static_path/template_path等) 笔记
使用StaticFileHandler进行首页默认访问页面,最好将StaticFileHandler放在最后面,这样不会覆盖要匹配自定义的路径 import tornado.web import to ...
- tornado.web.StaticFileHandler
tornado.web.StaticFileHandler 源代码中的解释 class StaticFileHandler(RequestHandler): """A s ...
- Tornado源码分析 --- 静态文件处理模块
每个web框架都会有对静态文件的处理支持,下面对于Tornado的静态文件的处理模块的源码进行分析,以加强自己对静态文件处理的理解. 先从Tornado的主要模块 web.py 入手,可以看到在App ...
- Python Tornado框架三(源码结构)
Tornado 是由 Facebook 开源的一个服务器“套装”,适合于做 python 的 web 或者使用其本身提供的可扩展的功能,完成了不完整的 wsgi 协议,可用于做快速的 web 开发,封 ...
- tornado session
[转]tornado入门 - session cookie 和session 的区别: 1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以 ...
- tornado高效开发必备之源码详解
前言:本博文重在tornado源码剖析,相信读者读完此文能够更加深入的了解tornado的运行机制,从而更加高效的使用tornado框架. 本文参考武sir博客地址:http://www.cnblog ...
- tornado学习笔记11 Web应用中模板(Template)使用应用实践
上一篇中(Web应用中模板的工作流程分析),已经分析了模板的渲染流程,以及相关参数获取及设置原理.这篇主要讲述模板在实际应用案例. 11.1 需求 根据用户输入的两次密码,判断两次密码是否一致,并将判 ...
随机推荐
- Linux: bash script
content [toc] bash scripts equivalent bash command to rename a bash variable/command alias fire='fir ...
- maven下载jar包失败后无法再次重新下载
maven下载jar包失败后无法再次重新下载:删除maven 资源库中的 *.lastUpdated文件
- 生成一个唯一token
$token = md5(uniqid(rand(), true));
- Administrator账户密码忘了怎么办
WIN7的Administrator账户密码忘了怎么办 1.准备一个带有winpe系统的系统光盘或者U盘 2.启动电脑,按F12选择U盘或者光盘启动,进入winpe操作系统 3.找到c:\wind ...
- [leetcode-553-Optimal Division]
Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...
- 【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- 5.VBS的一些约定,提高可读性
1.变量命名约定 2.变量作用域 1)过程级,在事件中,函数或者子过程中 2)Script级,在head部分 原则,定义尽量小的作用域 3.在某个过程开头应该包括这些注释
- AJAX跨域的常见方法
由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问.比如说你的网站域名是aaa.com,想要通过AJAX请求 ...
- App的前后台数据同步
前言 在开发一个点餐软件时,app的订单数据是使用本地Sqlite数据库,在提交订单数据后,当订单数据在后台(Mysql数据库)发生变化时(如:已买单),本地数据如何改变呢? 思路 前台在查询时,将后 ...
- Java 实现FTP上传和下载
1. 目前网上开源的FTP Client主要有JFTP.FTP4.edtFtpjJ和Apache.FTPClient. 2. jftp地址:http://www.jmethods.com/ 3. ed ...