-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
83 lines (72 loc) · 2.42 KB
/
app.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
import asyncio
import http.server
import os
import socket
import socketserver
import ssl
import threading
import tkinter as tk
import sys
import argparse
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
def start_http_server(path, port):
os.chdir(path)
Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
".js": "application/javascript",
});
with socketserver.TCPServer(("", port), Handler) as httpd:
httpd.serve_forever()
def start_https_server(path, port):
os.chdir(path)
Handler = http.server.SimpleHTTPRequestHandler
Handler.extensions_map.update({
".js": "application/javascript",
});
httpd = http.server.HTTPServer(("", port), Handler)
httpd.socket = ssl.wrap_socket(httpd.socket,
certfile='../cert.pem', server_side=True)
httpd.serve_forever()
def main():
arg_parser = argparse.ArgumentParser( description='ScaffoldSketch' )
arg_parser.add_argument('--dummy', action='store_true', help='Disable auto-correct.')
arg_parser.add_argument('--load-state', type=str, help='A path to a JSON file with initial state to load.' )
args = arg_parser.parse_args()
path = "html/"
port = 4443
ipaddress = get_ip_address()
url = "https://{}:{}/".format(ipaddress, port)
print("Serving at {}".format(url))
# Start the web server
web_server = threading.Thread(name='web_server',
target=start_https_server,
args=(path, port))
web_server.setDaemon(True)
web_server.start()
def start_app_server():
asyncio.set_event_loop(asyncio.new_event_loop())
import paint_server
paint_server.run( **vars(args) )
# Start a separate app thread here.
app_server = threading.Thread(name='app_server',
target=start_app_server)
app_server.setDaemon(True)
app_server.start()
# App GUI window gets the main loop
root = tk.Tk()
root.title("App Server")
tk.Label(root, text="Serving at {}".format(url)).pack()
tk.Button(root, text="Quit", command=root.destroy).pack()
tk.mainloop()
if __name__ == '__main__':
main()