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

  1. Python高性能编程

    一.进程池和线程池 1.串行 import time import requests url_lists = [ 'http://www.baidu.com', 'http://fanyi.baidu ...

  2. Python3.7 + jupyter安装(CentOS6.5)

    Python3.7 + jupyter安装(CentOS6.5) 方法一(anaconda): anaconda是一个开源的Python发行版本 包含conda,python等大量的科学包以及依赖 优 ...

  3. 发布一个参考tornado的高性能c++网络库:libtnet

    libtnet是一个用c++编写的高性能网络库,它在设计上面主要参考tornado,为服务端网络编程提供简洁而高效的接口,非常易于使用. Echo Server void onConnEvent(co ...

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

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

  5. tornado.web.StaticFileHandler

    tornado.web.StaticFileHandler 源代码中的解释 class StaticFileHandler(RequestHandler): """A s ...

  6. Tornado源码分析 --- 静态文件处理模块

    每个web框架都会有对静态文件的处理支持,下面对于Tornado的静态文件的处理模块的源码进行分析,以加强自己对静态文件处理的理解. 先从Tornado的主要模块 web.py 入手,可以看到在App ...

  7. Python Tornado框架三(源码结构)

    Tornado 是由 Facebook 开源的一个服务器“套装”,适合于做 python 的 web 或者使用其本身提供的可扩展的功能,完成了不完整的 wsgi 协议,可用于做快速的 web 开发,封 ...

  8. tornado session

    [转]tornado入门 - session cookie 和session 的区别: 1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以 ...

  9. tornado高效开发必备之源码详解

    前言:本博文重在tornado源码剖析,相信读者读完此文能够更加深入的了解tornado的运行机制,从而更加高效的使用tornado框架. 本文参考武sir博客地址:http://www.cnblog ...

  10. tornado学习笔记11 Web应用中模板(Template)使用应用实践

    上一篇中(Web应用中模板的工作流程分析),已经分析了模板的渲染流程,以及相关参数获取及设置原理.这篇主要讲述模板在实际应用案例. 11.1 需求 根据用户输入的两次密码,判断两次密码是否一致,并将判 ...

随机推荐

  1. 在CentOS7下安装jekyll

    [root@k8smaster nodejs]# yum install gem ruby ruby-devel -y [root@k8smaster nodejs]# gem sources -l ...

  2. Linux网络服务12——NFS共享服务

    Linux网络服务12--NFS共享服务 一.NFS简介 端口号:TCP.UDP 111端口 NFS(Network File System)网络文件系统,是一种基于TCP/IP传输的网络文件系统协议 ...

  3. 针对Oracle数据库表中的数据的常见操作

    1.查询表中所有数据 select * from 表名; 例:select * from stu; 2.查询的同时修改表中数据 select * from 表名  for update; 例:sele ...

  4. Vue.js 介绍入门

    Vue.js 的目标 是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.Vue.js 是一个用于创建 Web 交互界面的库.它让你通过简单而灵活的 API 创建由数据驱动的 UI 组件. ...

  5. Selenium chrome配置代理Python版

    环境: windows 7 + Python 3.5.2 + Selenium 3.4.2 + Chrome Driver 2.29 + Chrome 58.0.3029.110 (64-bit) S ...

  6. PHP实现简单的评论与回复功能还有删除信息

    我们首先先看一下功能 上面黑色的是评论的下面红色的字体是回复的 再来看看怎么实现的 1.发布评论 <form action="pinglunchili.php" method ...

  7. 使用Homebrew安装MySQL

    安装命令: brew install mysql 安装完成之后,启动mysql: mysql.server start 发现无此命令: command not found 首先,检查是否是安装了.重新 ...

  8. Shiro固定身份验证

    Shiro基础身份验证 如果要进行shiro的日志信息读取,那么需要使用一个org.apache.shiro.util.Factory接口,在这个接口里面定义有一 取得SecuruityManager ...

  9. 对X86汇编的理解与入门

    本文描述基本的32位X86汇编语言的一个子集,其中涉及汇编语言的最核心部分,包括寄存器结构,数据表示,基本的操作指令(包括数据传送指令.逻辑计算指令.算数运算指令),以及函数的调用规则.个人认为:在理 ...

  10. 20170717_python爬虫之requests+cookie模拟登陆

    在成功登陆之前,失败了十几次.完全找不到是什么原因导致被网站判断cookie是无效的. 直到用了firefox的httpfox之后才发现cookie里还有一个ASP.NET_SessionId 这个字 ...