Skip to content

Instantly share code, notes, and snippets.

@asvetlov
Created January 20, 2020 10:29
Show Gist options
  • Save asvetlov/50a900791d02ee41d0a7cfd9ed49ea20 to your computer and use it in GitHub Desktop.
Save asvetlov/50a900791d02ee41d0a7cfd9ed49ea20 to your computer and use it in GitHub Desktop.
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