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的更多相关文章

  1. tornado.web.Application类配置及使用

    Application configuration classtornado.web.Application(handlers=None, default_host='', transforms=No ...

  2. 20-2 树莓派搭建服务器 Tornado Web服务器

    Drive.google.com/drive/folders/1ahbeoEHkjxoo4NV1wReOmpoRWbl448z- 1.Tornado简介 Tornado一款使用 Python 编写的, ...

  3. Tornado.web.Application之-settings

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

  4. Tornado WEB服务器框架 Epoll

    引言: 回想Django的部署方式 以Django为代表的python web应用部署时采用wsgi协议与服务器对接(被服务器托管),而这类服务器通常都是基于多线程的,也就是说每一个网络请求服务器都会 ...

  5. Tornado 模板(StaticFileHandler/static_path/template_path等) 笔记

    使用StaticFileHandler进行首页默认访问页面,最好将StaticFileHandler放在最后面,这样不会覆盖要匹配自定义的路径 import tornado.web import to ...

  6. Python(九)Tornado web 框架

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

  7. tornado web 框架的认识

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

  8. tornado 学习笔记9 Tornado web 框架---模板(template)功能分析

            Tornado模板系统是将模板编译成Python代码.         最基本的使用方式: t = template.Template("<html>{{ myv ...

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

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

随机推荐

  1. jdk8 StreamApi

    List<User> userList=new ArrayList<>(); userList.add(User.builder().age().name()).build() ...

  2. haproxy配置tcp负载均衡

    #---------------------------------------------------------------------# 监控页面 #---------------------- ...

  3. Sqlserver 批量数据更改

    insert into AAA select GETDATE(),name from BBB where aid=0 update a set a = b.a,b=b.b,c=b.c from t a ...

  4. easyui menu 添加hideItem/showItem 方法

    $.extend($.fn.menu.methods,{ showItem:function(jq,text){ return jq.each(function(){ var item = $(thi ...

  5. jquery 改变checkbox的值

    似乎没什么用... <script> $(document).ready(function(){ $("#comment").change(function(){ va ...

  6. 第二百一十九节,jQuery EasyUI,DateTimeBox(日期时间输入框)组件

    jQuery EasyUI,DateTimeBox(日期时间输入框)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 DateTimeBox(日期时间输入框 ...

  7. Collection 和 Collections的区别?

    Collection 和 Collections的区别? 解答:Collection是java.util下的接口,它是各种集合的父接口,继承于它的接口主要有Set 和List:Collections是 ...

  8. .NET学习笔记(1)

    把 DataSet 绑定到 Repeater 控件 Repeater 控件用于显示重复的项目列表,这些项目被限制在该控件.Repeater 控件可被绑定到数据库表.XML 文件或者其他项目列表. 获取 ...

  9. 修改原型给数组对象添加forEach

    Array.prototype.forEach = function (callback, context) { for (var i = 0; i < this.length; i++) { ...

  10. html input accept类型

    在上传文件的时候,需要限制指定的文件类型. <input type="file" accept="image/*" /> accept表示可以上传文 ...