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