Tornado.web.StaticFileHandler

Source: Internet
Author: User

tornado.web.StaticFileHandler
Explanation in the source code
Class StaticFileHandler(RequestHandler):"A simple handler the can serve static content from a directory.A ' StaticFileHandler ' is configured automatically if you pass the' static_path ' keyword argument to ' application '. This handlerCan be customized with the ' Static_url_prefix ', ' Static_handler_class ',and ' Static_handler_args ' settings.To map the additional path to the handler for a static data directoryYou 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 theLocal root directory of the content to be served.Note that a capture group in the regex was required to parse the value forThe ' path ' argument to the Get () method (different than the constructorargument above); See ' Urlspec ' for details.To serve a file like "index.html" automatically when a directory isRequested, set ' Static_handler_args=dict (default_filename= "index.html") 'In your application settings, or add "Default_filename" as an initializerArgument for your ' StaticFileHandler '.To maximize the effectiveness of browser caching, this class supportsversioned URLs (by default using the argument "? v="). If a versionis given, we instruct the browser-to-cache this file indefinitely.' Make_static_url ' (also available as ' requesthandler.static_url ') canbe used to construct a versioned URL.This handler was intended primarily for use in development and light-dutyFile serving; For heavy traffic It'll be more efficientA dedicated static file server (such as Nginx or Apache). We SupportThe HTTP ' accept-ranges ' mechanism to return partial content (becauseSome browsers require this functionality to being present to seek inHTML5 audio or video).**subclassing notes**This class was designed to being extensible by subclassing, but becauseOf the the the-the-generated with class methods rather thanInstance methods, the inheritance patterns is somewhat unusual.Be sure to use the "@classmethod" decorator when overriding aClass method. Instance methods may use the attributes ' Self.path '' Self.absolute_path ', and ' self.modified '.Subclasses should only override methods discussed.Overriding other methods is error-prone. overriding"Staticfilehandler.get" is particularly problematic due to theTight coupling with "Compute_etag" and other methods.To change the the-the-generated (e.g. to match the behaviorof 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_cont Ent ', ' 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.  """

Two ways to render the front and back end

    • Template rendering, rendering using the template language

    • Front-end separation, before and after the development of the API interface defined, using AJAX to call these interfaces, can be developed at the same time, the backend just return the specified data

tornado.web.StaticFileHandlerIs the tornado used to provide static resource files handler

import Oscurrent_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: Used to provide html the root path of the file, and in this directory look for the value of the file name extracted with the regular expression in the URL

    • Default_filename: Used to specify the file that is provided by default when the file is not specified in the Access route

    • Static_path: Provides a location for static files

After the above information is configured, the project should be able to pull up

Tornado.web.StaticFileHandler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.