pants.web.fileserver implements a basic static file server for use with a
Application. It makes use of the appropriate
HTTP headers and the
sendfile system call, as well as the
header to improve transfer performance.
Serving Static Files¶
pants.web.fileserver module can be invoked directly using the -m
switch of the interpreter to serve files in a similar way to the standard
SimpleHTTPServer. However, it performs much more efficiently
SimpleHTTPServer for this task.
$ python -m pants.web.fileserver
When doing this, you may use additional arguments to specify which address the
server should bind to, as well as which filenames should serve as directory
indices. By default, only
index.htm are served
FileServer(path, blacklist=(<_sre.SRE_Pattern object>, ), defaults=('index.html', 'index.htm'))¶
The FileServer is a request handling class that, as it sounds, serves files to the client using
pants.http.server.HTTPRequest.send_file(). As such, it supports caching headers, as well as
HTTPServerinstance is configured to use the Sendfile header. FileServer is also able to take advantage of the
sendfilesystem call to improve performance when
X-Sendfileis not in use.
Argument Default Description path The path to serve. blacklist
Optional. A list of regular expressions to test filenames against. If a given file matches any of the provided patterns, it will not be downloadable and instead return a
Optional. A list of default files to be displayed rather than a directory listing if they exist.
Using it is simple. It only requires a single argument: the path to serve files from. You can also supply a list of default files to check to serve rather than a file listing.
When used with an Application, the FileServer is not created in the usual way with the route decorator, but rather with a method of the FileServer itself. Example:
If you wish to listen on a path other than
/static/, you can also use that when attaching: