-
Notifications
You must be signed in to change notification settings - Fork 26
/
http_server.py
44 lines (31 loc) · 1.04 KB
/
http_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
import atexit
import http.server
import os
import socketserver
import threading
from kepler_model.util.config import model_toppath
def cleanup_task(server):
print("Shutdown server...")
server.shutdown()
def get_server(file_server_port):
Handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", file_server_port), Handler)
# Register the cleanup task to be executed on program exit
atexit.register(cleanup_task, httpd)
print("Http File Serve Serving at Port", file_server_port, " for ", model_toppath)
return httpd
def http_file_server(file_server_port):
try:
httpd = get_server(file_server_port)
# Start the server in a separate thread
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()
except Exception as err:
print(f"File server is running: {err}")
def run():
os.chdir(model_toppath)
httpd = get_server(8110)
httpd.serve_forever()
if __name__ == "__main__":
run()