aiohttp-send
Send file in aiohttp
Install
Python 3.6 only now (function arguments type hint does not work in 3.5)
pip install aiohttp aiohttp-send
Options
-
max_age
Browser cache max-age in milliseconds. (defaults to0
) -
immutable
Tell the browser the resource is immutable and can be cached indefinitely. (defaults toFalse
) -
hidden
Allow transfer of hidden files. (defaults toTrue
) -
root
Root directory to restrict file access. -
index
Name of the index file to serve automatically when visiting the root location. (defaults toNone
) -
gzip
Try to serve the gzipped version of a file automatically whengzip
is supported by a client and if the requested file with.gz
extension exists. (defaults toFalse
). -
brotli
Try to serve the brotli version of a file automatically whenbrotli
is supported by a client and if the requested file with.br
extension exists. (defaults toFalse
). -
format
If notFalse
(defaults toTrue
), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both/directory
and/directory/
. -
extensions
Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults toFalse
)
Root path
Note that root
is required, defaults to ''
and will be resolved,
removing the leading /
to make the path relative and this
path must not contain "..", protecting developers from
concatenating user input. If you plan on serving files based on
user input supply a root
directory from which to serve from.
For example to serve files from ./public
:
async def index(request: web.Request):
return await send(request, request.path, root='./public', format=True)
app.add_routes([
web.get('/{tail:.*}', index),
])
Example
from aiohttp import web
from aiohttp_send import send
app = web.Application()
async def index(request):
return await send(request, 'index.html')
app.add_routes([
web.get('/', index),
])
web.run_app(app, port=8888)
This project comes from koajs/send.