-
Notifications
You must be signed in to change notification settings - Fork 853
Expand file tree
/
Copy pathquote.py
More file actions
36 lines (33 loc) · 1.19 KB
/
quote.py
File metadata and controls
36 lines (33 loc) · 1.19 KB
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
#!/usr/bin/env python3
# quote_server.py
import http.server
import socketserver
import random
import json
QUOTES = [
"Be the change you wish to see. — Gandhi",
"Simplicity is the ultimate sophistication. — Leonardo da Vinci",
"Code is like humor. When you have to explain it, it’s bad. — Cory House",
"First, solve the problem. Then, write the code. — John Johnson",
]
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path in ("/", "/quote"):
q = random.choice(QUOTES)
payload = {"quote": q}
data = json.dumps(payload).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
else:
super().do_GET()
if __name__ == "__main__":
PORT = 8000
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving on http://localhost:{PORT}/quote")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("Stopping server.")