forked from adafruit/Adafruit_CircuitPython_HTTPServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver_form_data.py
66 lines (53 loc) · 1.83 KB
/
httpserver_form_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# SPDX-FileCopyrightText: 2023 Michał Pokusa
#
# SPDX-License-Identifier: Unlicense
import socketpool
import wifi
from adafruit_httpserver import Server, Request, Response, GET, POST
pool = socketpool.SocketPool(wifi.radio)
server = Server(pool, debug=True)
FORM_HTML_TEMPLATE = """
<html lang="en">
<head>
<title>Form with {enctype} enctype</title>
</head>
<body>
<a href="/form?enctype=application/x-www-form-urlencoded">
<button>Load <strong>application/x-www-form-urlencoded</strong> form</button>
</a><br />
<a href="/form?enctype=multipart/form-data">
<button>Load <strong>multipart/form-data</strong> form</button>
</a><br />
<a href="/form?enctype=text/plain">
<button>Load <strong>text/plain</strong> form</button>
</a><br />
<h2>Form with {enctype} enctype</h2>
<form action="/form" method="post" enctype="{enctype}">
<input type="text" name="something" placeholder="Type something...">
<input type="submit" value="Submit">
</form>
{submitted_value}
</body>
</html>
"""
@server.route("/form", [GET, POST])
def form(request: Request):
"""
Serve a form with the given enctype, and display back the submitted value.
"""
enctype = request.query_params.get("enctype", "text/plain")
if request.method == POST:
posted_value = request.form_data.get("something")
return Response(
request,
FORM_HTML_TEMPLATE.format(
enctype=enctype,
submitted_value=(
f"<h3>Enctype: {enctype}</h3>\n<h3>Submitted form value: {posted_value}</h3>"
if request.method == POST
else ""
),
),
content_type="text/html",
)
server.serve_forever(str(wifi.radio.ipv4_address))