-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathserver.py
82 lines (59 loc) · 2.78 KB
/
server.py
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
"""
Simple HTTP server to run dynamic simulations and view results.
"""
import http.server
import socketserver
import simplejson as json
from run_simulations import run_simulation, setup_summary
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
"""
This class will handles any incoming request from the browser
"""
def do_GET(self):
if self.path == '/':
self.path = 'view.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
post_data = self.rfile.read(content_length) # <--- Gets the data itself
self.send_response(200)
self.end_headers()
try:
config = json.loads(post_data)
except ValueError as e:
config = {}
print("{}".format(config))
# Run the simulation
setup_summary('_interactive')
run_simulation('almostperfect', 'almostperfect_interactive',
config=json.dumps(config))
run_simulation('CloudyMorning', 'CloudyMorning_interactive',
config=json.dumps(config))
run_simulation('day1', 'day1_interactive',
config=json.dumps(config))
run_simulation('day2', 'day2_interactive',
config=json.dumps(config))
run_simulation('day3', 'day3_interactive',
config=json.dumps(config))
run_simulation('day1_grid_ie', 'day1_grid_ie_interactive',
grid_ie_col=2, config=json.dumps(config))
run_simulation('day2_grid_ie', 'day2_grid_ie_interactive',
grid_ie_col=2, config=json.dumps(config))
run_simulation('day3_grid_ie', 'day3_grid_ie_interactive',
grid_ie_col=2, config=json.dumps(config))
run_simulation('solar-vrms', 'solar-vrms_interactive',
voltage_col=2, config=json.dumps(config))
run_simulation('Energy_and_Power_Day_2020-03-22', 'Energy_and_Power_Day_2020-03-22_interactive',
separator=';', is_kw=True, config=json.dumps(config))
run_simulation('Energy_and_Power_Day_2020-03-31', 'Energy_and_Power_Day_2020-03-31_interactive',
separator=';', is_kw=True, config=json.dumps(config))
run_simulation('Energy_and_Power_Day_2020-04-01', 'Energy_and_Power_Day_2020-04-01_interactive',
separator=';', is_kw=True, config=json.dumps(config))
self.wfile.write("OK".encode('utf-8'))
# Create an object of the above class
handler_object = MyHttpRequestHandler
PORT = 8000
my_server = socketserver.TCPServer(("", PORT), handler_object)
# Start the server
print("Server started at localhost:" + str(PORT))
my_server.serve_forever()