cherrypy.lib.static module

Module with helpers for serving static files.

cherrypy.lib.static._attempt(filename, content_types, debug=False)[source]
cherrypy.lib.static._make_content_disposition(disposition, file_name)[source]

Create HTTP header for downloading a file with a UTF-8 filename.

This function implements the recommendations of RFC 6266#appendix-D. See this and related answers: https://stackoverflow.com/a/8996249/2173868.

cherrypy.lib.static._serve_fileobj(fileobj, content_type, content_length, debug=False)[source]

Set response.body to the given file object, perhaps ranged.

Internal helper.

cherrypy.lib.static._setup_mimetypes()[source]

Pre-initialize global mimetype map.

cherrypy.lib.static.serve_download(path, name=None)[source]

Serve ‘path’ as an application/x-download attachment.

cherrypy.lib.static.serve_file(path, content_type=None, disposition=None, name=None, debug=False)[source]

Set status, headers, and body in order to serve the given path.

The Content-Type header will be set to the content_type arg, if provided. If not provided, the Content-Type will be guessed by the file extension of the ‘path’ argument.

If disposition is not None, the Content-Disposition header will be set to “<disposition>; filename=<name>; filename*=utf-8’’<name>” as described in RFC 6266#appendix-D. If name is None, it will be set to the basename of path. If disposition is None, no Content- Disposition header will be written.

cherrypy.lib.static.serve_fileobj(fileobj, content_type=None, disposition=None, name=None, debug=False)[source]

Set status, headers, and body in order to serve the given file object.

The Content-Type header will be set to the content_type arg, if provided.

If disposition is not None, the Content-Disposition header will be set to “<disposition>; filename=<name>; filename*=utf-8’’<name>” as described in RFC 6266#appendix-D. If name is None, ‘filename’ will not be set. If disposition is None, no Content-Disposition header will be written.

CAUTION: If the request contains a ‘Range’ header, one or more seek()s will be performed on the file object. This may cause undesired behavior if the file object is not seekable. It could also produce undesired results if the caller set the read position of the file object prior to calling serve_fileobj(), expecting that the data would be served starting from that position.

cherrypy.lib.static.staticdir(section, dir, root='', match='', content_types=None, index='', debug=False)[source]

Serve a static resource from the given (root +) dir.

match

If given, request.path_info will be searched for the given regular expression before attempting to serve static content.

content_types

If given, it should be a Python dictionary of {file-extension: content-type} pairs, where ‘file-extension’ is a string (e.g. “gif”) and ‘content-type’ is the value to write out in the Content-Type response header (e.g. “image/gif”).

index

If provided, it should be the (relative) name of a file to serve for directory requests. For example, if the dir argument is ‘/home/me’, the Request-URI is ‘myapp’, and the index arg is ‘index.html’, the file ‘/home/me/myapp/index.html’ will be sought.

cherrypy.lib.static.staticfile(filename, root=None, match='', content_types=None, debug=False)[source]

Serve a static resource from the given (root +) filename.

match

If given, request.path_info will be searched for the given regular expression before attempting to serve static content.

content_types

If given, it should be a Python dictionary of {file-extension: content-type} pairs, where ‘file-extension’ is a string (e.g. “gif”) and ‘content-type’ is the value to write out in the Content-Type response header (e.g. “image/gif”).