pants.web.fileserver
¶
pants.web.fileserver
implements a basic static file server for use with a
HTTPServer
or
Application
. It makes use of the appropriate
HTTP headers and the sendfile
system call, as well as the X-Sendfile
header to improve transfer performance.
Serving Static Files¶
The 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
library’s SimpleHTTPServer
. However, it performs much more efficiently
than 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.html
and index.htm
are served
as indices.
FileServer
¶
-
class
pants.web.fileserver.
FileServer
(path, blacklist=(<_sre.SRE_Pattern object>, ), defaults=('index.html', 'index.htm'))[source]¶ 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 asX-Sendfile
if theHTTPServer
instance is configured to use the Sendfile header. FileServer is also able to take advantage of thesendfile
system call to improve performance whenX-Sendfile
is not in use.Argument Default Description path The path to serve. blacklist .py
and.pyc
filesOptional. 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 403 Unauthorized
error.default index.html
,index.htm
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:
FileServer("/tmp/path").attach(app)
If you wish to listen on a path other than
/static/
, you can also use that when attaching:FileServer("/tmp/path").attach(app, "/files/")
-
attach
(app, path='/static/')[source]¶ Attach this FileServer to an
Application
, bypassing the usual route decorator to ensure the rule is configured as FileServer expects.Argument Default Description app The Application
instance to attach to.rule '/static/'
Optional. The path to serve requests from.
-