Skip to content

Commit d499b17

Browse files
Day 25 - A Python Web App without a Framework
1 parent cab3a2e commit d499b17

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

tutorial-reference/Day 25/404.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<title>Pure Python Web App</title>
6+
</head>
7+
<body>
8+
404. Page not found. {path}
9+
</body>
10+
11+
</html>

tutorial-reference/Day 25/Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
8+
[packages]
9+
gunicorn = "*"
10+
11+
[requires]
12+
python_version = "3.8"

tutorial-reference/Day 25/Pipfile.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<title>Pure Python Web App</title>
6+
</head>
7+
<body>
8+
<h1>Contact Us</h1>
9+
</body>
10+
11+
</html>

tutorial-reference/Day 25/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<title>Pure Python Web App</title>
6+
</head>
7+
<body>
8+
Hello World
9+
</body>
10+
11+
</html>

tutorial-reference/Day 25/server.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# gunicorn
2+
3+
def render_template(template_name='index.html', context={}):
4+
html_str = ""
5+
with open(template_name, 'r') as f:
6+
html_str = f.read()
7+
html_str = html_str.format(**context)
8+
return html_str
9+
10+
def home(environ):
11+
return render_template(
12+
template_name='index.html',
13+
context={}
14+
)
15+
16+
def contact_us(environ):
17+
return render_template(
18+
template_name='contact.html',
19+
context={}
20+
)
21+
22+
def app(environ, start_response):
23+
path = environ.get("PATH_INFO")
24+
if path.endswith("/"):
25+
path = path[:-1]
26+
if path == "": # index / root of the web app
27+
data = home(environ)
28+
elif path == "/contact":
29+
data = contact_us(environ)
30+
else:
31+
data = render_template(template_name='404.html', context={"path": path})
32+
data = data.encode("utf-8")
33+
start_response(
34+
f"200 OK", [
35+
("Content-Type", "text/html"),
36+
("Content-Length", str(len(data)))
37+
]
38+
)
39+
return iter([data])

0 commit comments

Comments
 (0)