-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
76 lines (67 loc) · 2.12 KB
/
Copy pathserver.py
File metadata and controls
76 lines (67 loc) · 2.12 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
import http.server
from urllib.parse import urlparse
import json
import sys
# Default port
PORT = 8000
# Empty JSON dictionary
j_dict = {}
# Get server listing port number or use default port number
try:
PORT = int(sys.argv[1])
except Exception as e:
print('Error while reading port number.', e)
print('Use default port number:', PORT)
# Read saved JSON db
try:
f = open('db.json', 'r')
j_str = f.read()
f.close()
j_dict = json.loads(j_str)
except Exception as e:
print('Error while reading JSON file.', e)
print('Use empty JSON dictionary.')
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
p = urlparse(self.path).path
print(p)
if (p == "/get_json"):
self.send_response(200)
self.send_header('Content-type','application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
j_str = json.dumps(j_dict) # Dictionary to string
self.wfile.write(j_str.encode('utf-8'))
else :
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
f = open("index.html", "r")
self.wfile.write(f.read(-1).encode('utf-8'))
f.close()
# HTTP GET query string to JSON
q = urlparse(self.path).query
# print(q)
try:
r = {}
for s in q.split("&"):
# print(s)
t = s.split("=")
# print(t)
r[t[0]] = t[1]
# print(r)
j_dict.update(r)
except Exception as e:
print('Error occurred.', e)
try:
httpd = http.server.ThreadingHTTPServer(('', PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()
except Exception as e:
print('Error occurred.', e)
except KeyboardInterrupt: # Stop the server and save JSON db
httpd.shutdown()
f = open('db.json', 'w')
j_str = json.dumps(j_dict)
f.write(j_str)
f.close()