-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
94 lines (82 loc) · 3.67 KB
/
Copy pathserver.py
File metadata and controls
94 lines (82 loc) · 3.67 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import http.server
import socketserver
import json
import os
PORT = 8000
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/api/products':
self.path = '/database/products.json'
elif self.path == '/api/settings':
self.path = '/database/settings.json'
elif self.path == '/api/account':
self.path = '/database/account.json'
elif self.path == '/api/timeline':
self.path = '/database/timeline.json'
return super().do_GET()
def do_POST(self):
if self.path == '/api/products' or self.path == '/api/save':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
with open('database/products.json', 'wb') as f:
f.write(post_data)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(b'{"status":"success"}')
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(f'{{"error":"{str(e)}"}}'.encode())
elif self.path == '/api/settings':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
with open('database/settings.json', 'wb') as f:
f.write(post_data)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(b'{"status":"success"}')
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(f'{{"error":"{str(e)}"}}'.encode())
elif self.path == '/api/account':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
with open('database/account.json', 'wb') as f:
f.write(post_data)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(b'{"status":"success"}')
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(f'{{"error":"{str(e)}"}}'.encode())
elif self.path == '/api/timeline':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
with open('database/timeline.json', 'wb') as f:
f.write(post_data)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(b'{"status":"success"}')
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(f'{{"error":"{str(e)}"}}'.encode())
else:
self.send_response(404)
self.end_headers()
if __name__ == "__main__":
# Allow restarting quickly by setting allow_reuse_address
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving at port {PORT} with API enabled")
httpd.serve_forever()