Created
January 20, 2020 10:29
-
-
Save asvetlov/50a900791d02ee41d0a7cfd9ed49ea20 to your computer and use it in GitHub Desktop.
Form server
This file contains 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
<!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> |
This file contains 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 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