-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
99 lines (79 loc) · 2.23 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""Run from the parent directory."""
try:
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
except:
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer import TCPServer
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
import json
import os
import imp
import argparse
import sys
if sys.platform == 'cygwin':
os.environ.setdefault('BROWSER', 'cygstart')
import webbrowser
def import_module(module_name):
path = os.path.join(os.path.dirname(__file__), module_name + '.py')
return imp.load_source(module_name, path)
class Handler(SimpleHTTPRequestHandler):
def address_string(self):
"""
Faster than resolving the address every time (it's always localhost).
"""
return self.client_address[0]
def do_POST(self):
"""
Given json, run a command. The path is ignored.
"""
#print ('"POST {}"'.format(self.path))
length = int(self.headers.getheader('content-length'))
data = self.rfile.read(length)
# stupid hack to get past filler
boundary = data.split()[0]
data = data.split(boundary)[1]
data = data[data.find('\r\n\r\n')+4:]
result = {}
try:
parsed_data = json.loads(data)
print (data.strip())
commands = import_module('commands')
result_data = commands.main(parsed_data)
if result_data is not None:
result['data'] = result_data
self.send_response(200)
except FileNotFoundError as e:
print (e)
result['error'] = str(e)
self.send_response(404)
except Exception as e:
print (e)
result['error'] = str(e)
self.send_response(500)
self.send_header("Content-type", "application/json")
self.end_headers()
response = json.dumps(result)
try:
response = bytes(response, 'utf-8')
except:
response = response.encode('utf-8')
self.wfile.write(response)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('port', nargs='?', type=int, default='8000')
args = ap.parse_args()
port = args.port
try:
httpd = TCPServer(("", port), Handler)
url = 'http://127.0.0.1:{}/crowdmap'.format(port)
print ("Open this url in your browser: {}".format(url))
webbrowser.open_new(url)
httpd.serve_forever()
except KeyboardInterrupt:
print ('port {} closed'.format(port))
if __name__ == '__main__':
main()