-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
63 lines (43 loc) · 1.75 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
__author__ = 'Arnout Aertgeerts'
import sys
if sys.version_info.major == 3:
import http.server as SimpleHTTPServer
import socketserver as SocketServer
else:
import SimpleHTTPServer
import SocketServer
import os
import json
class ChartRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def __init__(self, request, client_address, server):
SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, request, client_address, server)
return
def do_POST(self):
content_len = int(self.headers.getheader('content-length', 0))
body = json.loads(self.rfile.read(content_len))
if 'options' in body:
with open(body['name'], 'w') as json_file:
json_file.write(json.dumps(body['options']))
else:
with open(body['name'], 'w') as chart_file:
chart_file.write(body['svg'])
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def log_message(self, format, *args):
pass
class ChartServer(SocketServer.TCPServer):
def __init__(self, server_address, handler_class=ChartRequestHandler):
SocketServer.TCPServer.__init__(self, server_address, handler_class)
return
def run_server():
import threading
address = ('localhost', 0) # let the kernel give us a port
server = ChartServer(address, ChartRequestHandler)
ip, port = server.server_address # find out what port we were given
print('Server running in the folder {0} at {1}:{2}'.format(os.path.abspath(os.getcwd()), ip, port))
t = threading.Thread(target=server.serve_forever)
t.setDaemon(True) # don't hang on exit
t.start()
return 'http://{0}:{1}/'.format(ip, port)
address = run_server()
def url(path):
return address + path