-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.py
57 lines (46 loc) · 1.62 KB
/
main.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
# example script to show how uri routing and parameters work
#
# create a file called secrets.py alongside this one and add the
# following two lines to it:
#
# WIFI_SSID = "<ssid>"
# WIFI_PASSWORD = "<password>"
#
# with your wifi details instead of <ssid> and <password>.
from phew import server, connect_to_wifi
from phew.template import render_template
import secrets
connect_to_wifi(secrets.WIFI_SSID, secrets.WIFI_PASSWORD)
# basic response with status code and content type
@server.route("/basic", methods=["GET", "POST"])
def basic(request):
return "Gosh, a request", 200, "text/html"
# basic response with status code and content type
@server.route("/status-code", methods=["GET", "POST"])
def status_code(request):
return "Here, have a status code", 200, "text/html"
# url parameter and template render
@server.route("/hello/<name>", methods=["GET"])
def hello(request, name):
return await render_template("example.html", name=name)
# response with custom status code
@server.route("/are/you/a/teapot", methods=["GET"])
def teapot(request):
return "Yes", 418
# custom response object
@server.route("/response", methods=["GET"])
def response_object(request):
return server.Response("test body", status=302, content_type="text/html", headers={"Cache-Control": "max-age=3600"})
# query string example
@server.route("/random", methods=["GET"])
def random_number(request):
import random
min = int(request.query.get("min", 0))
max = int(request.query.get("max", 100))
return str(random.randint(min, max))
# catchall example
@server.catchall()
def catchall(request):
return "Not found", 404
# start the webserver
server.run()