Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Form server
<!DOCTYPE html>
<html>
<body>
<form action="/" method="post" enctype="multipart/form-data">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname"><br>
File:<br>
<input type="file" name="file"><br>
<input type="submit">
</form>
</body>
</html>
from aiohttp import web
async def render_form(request):
with open('form-data.html') as f:
return web.Response(text=f.read(), content_type='text/html')
async def print_form(request):
text = await request.text()
headers = "\n".join(f"{name}: {val}" for name, val in request.headers.items())
body = f"{headers}\r\n------------------------\r\n{text}"
return web.Response(text=body, content_type='text/plain')
async def init():
app = web.Application()
app.add_routes([web.get('/', render_form)])
app.add_routes([web.post('/', print_form)])
return app
web.run_app(init())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.