Created
March 29, 2021 06:21
-
-
Save abingham/54d646dcf5c5c5d8ab85b337378a55ff to your computer and use it in GitHub Desktop.
Example of serving a PNG from aiohttp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from aiohttp import web | |
from aiohttp.web_response import Response | |
async def index(request): | |
# This just demonstrates how the HTML can reference another endpoint in this server. | |
return Response( | |
text='<html><body><img src="/image"></body></html>', | |
content_type="text/html", | |
) | |
async def image(request): | |
# The data for this PNG could come from a file (like this), be generated programatically, etc. | |
data = Path("some_image.png").read_bytes() | |
return Response(body=data, content_type="image/png") | |
app = web.Application() | |
app.router.add_get("/", index) | |
app.router.add_get("/image", image) | |
web.run_app(app, port=8001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment