forked from noxrepo/pox
/
control.py
77 lines (60 loc) · 2.08 KB
/
control.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
__author__ = 'Eugene Chemeritskiy'
import multiprocessing
import BaseHTTPServer
import urlparse
import json
import os
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.path = self.path.split('?')[0][1:]
if not self.path:
self.path = 'index.html'
base = self.server.web_gui.base
filename = os.path.join(base, self.path)
if not os.path.exists(filename):
filename = os.path.join(base, 'dummy.html')
with open(filename, "r") as f:
content = f.read()
self.wfile.write(content)
def do_POST(self):
query_string = self.rfile.read(int(self.headers['Content-Length']))
query = json.loads(query_string)
print("sending query", str(query))
web_gui = self.server.web_gui
web_gui.ctl_conn[1].send(query)
reply = web_gui.srv_conn[0].recv()
print("got reply", str(reply))
r_data = json.dumps(reply)
self.send_response(200)
self.send_header("Content-type", "text")
self.send_header("Content-length", str(len(r_data)))
self.end_headers()
self.wfile.write(r_data)
def start_server(web_gui):
httpd = BaseHTTPServer.HTTPServer((web_gui.host, web_gui.port), Handler)
httpd.web_gui = web_gui
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
class WebGUI(object):
def __init__(self, host, port, base):
self.base = os.path.abspath(base)
self.host = host
self.port = port
def start(self):
self.ctl_conn = multiprocessing.Pipe(False)
self.srv_conn = multiprocessing.Pipe(False)
subprocess = multiprocessing.Process(target=start_server, args=(self,))
subprocess.start()
if __name__ == "__main__":
HOST = '127.0.0.1'
PORT = 8080
BASE = './html'
# start WebGUI inside of the parent process
# HTTP server should answer GET requests
web_gui = WebGUI(HOST, PORT, BASE)
start_server(web_gui)