-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_website.py
75 lines (66 loc) · 2.33 KB
/
run_website.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
import http.server
import socketserver
import subprocess
import socket
import os
import platform
import math
import random
import webbrowser
OS_TYPE = platform.system()
PORT = 5000
# get current directory and set it as the directory to serve
DIRECTORY = os.path.dirname(os.path.realpath(__file__)) + "/dist/"
def kill_port_unix(port):
command = f"lsof -i :{port} -t"
try:
result = subprocess.check_output(command, shell=True).decode("utf-8")
pids = result.strip().split("\n")
for pid in pids:
if (pid == "0"):
continue
subprocess.run(f"kill -9 {pid}", shell=True)
except subprocess.CalledProcessError as e:
return
def kill_port_windows(port):
command = f"netstat -ano | findstr :{port}"
try:
result = subprocess.check_output(command, shell=True).decode("utf-8").strip().split("\n")
for line in result:
pid = line.split()[-1]
if (pid == "0"):
continue
subprocess.run(f"taskkill /F /PID {pid}", shell=True)
except subprocess.CalledProcessError as e:
return
# Check if port is already in use
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port_in_use = sock.connect_ex(('127.0.0.1', PORT)) == 0
sock.close()
# If port is in use, suggest using --force option
if port_in_use:
force_port = input(f"Port {PORT} is already in use. Try to close the port and continue [Y/n]? ")
if force_port.lower() == "y":
if OS_TYPE == "Windows":
kill_port_windows(PORT)
else:
kill_port_unix(PORT)
else:
exit(1)
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def start_server(port):
try:
with socketserver.ThreadingTCPServer(("", port), Handler) as httpd:
print(f'Visit http://localhost:{port} for ViralWasm-Consensus. Please leave this window open! To stop running the site, just close the window.')
webbrowser.open(f"http://localhost:{port}/")
httpd.serve_forever()
except KeyboardInterrupt:
print("Stopping server...")
httpd.shutdown()
httpd.server_close()
try:
start_server(PORT)
except OSError as e:
start_server(6802 + math.floor(random.random() * 15))