-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
30 lines (25 loc) · 976 Bytes
/
server.py
File metadata and controls
30 lines (25 loc) · 976 Bytes
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
#!/usr/bin/env python3
import http.server, json, os, webbrowser
from pathlib import Path
DATA = Path(__file__).parent / "data.json"
PORT = 5555
class H(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/data":
body = DATA.read_bytes() if DATA.exists() else b'{"tasks":[]}'
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(body)
else:
super().do_GET()
def do_POST(self):
if self.path == "/data":
length = int(self.headers["Content-Length"])
DATA.write_bytes(self.rfile.read(length))
self.send_response(204)
self.end_headers()
def log_message(self, *_): pass # silence request logs
print(f"tt running at http://localhost:{PORT}")
webbrowser.open(f"http://localhost:{PORT}")
http.server.HTTPServer(("", PORT), H).serve_forever()