Replies: 1 comment
|
Static routes in aiohttp serve files from a directory, and that directory must exist at the time the route is registered, not when a request comes in: import os
from aiohttp import web
app = web.Application()
# This fails if /var/data/static doesn't exist at startup
app.router.add_static("/static", "/var/data/static")Solutions:
os.makedirs("/var/data/static", exist_ok=True)
app.router.add_static("/static", "/var/data/static")
async def serve_static(request):
path = os.path.join("/var/data/static", request.match_info["filename"])
if not os.path.exists(path):
raise web.HTTPNotFound()
return web.FileResponse(path)
app.router.add_get("/static/{filename:.*}", serve_static)
# pip install aiohttp-static
from aiohttp_static import static_resource
app.router.register_resource(static_resource("/static", "/var/data/static"))The create-server script you're using should ensure the directory exists before calling |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi, I'm having a little pet project where I want to serve data from multiple discs, including USB drives.
aiohttp/aiohttp/web_urldispatcher.py
Lines 537 to 538 in 3edc43c
I've tried to track down if that's just convenience or for security-related functionality, but that code changed a number of times, and I've lost track.
I wonder if it'd be OK to just drop that check?
All reactions