-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb-server.py
More file actions
executable file
·66 lines (49 loc) · 1.73 KB
/
web-server.py
File metadata and controls
executable file
·66 lines (49 loc) · 1.73 KB
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
from http.server import SimpleHTTPRequestHandler, HTTPServer
import os
import socket
class HtmlFallbackHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, directory=None, **kwargs):
self.directory = directory
super().__init__(*args, directory=directory, **kwargs)
def do_GET(self):
path = self.path.split("?")[0]
# If no file extension, try .html fallback
if not os.path.splitext(path)[1]:
html_path = path.rstrip("/") + ".html"
full_path = os.path.join(self.directory, html_path.lstrip("/"))
if os.path.exists(full_path):
self.path = html_path
super().do_GET()
def get_local_ip():
"""Gets the local network IP (Wi-Fi / Hotspot)"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
except Exception:
ip = "127.0.0.1"
finally:
s.close()
return ip
# ===== Ask for project path =====
webPath = "/sdcard/.workspace/web"
project_path = webPath + input("Enter project folder path to host: " + webPath).strip()
if not os.path.isdir(project_path):
print("❌ Invalid directory path")
exit(1)
project_path = os.path.abspath(project_path)
PORT = 5000
HOST = "0.0.0.0"
handler = lambda *args, **kwargs: HtmlFallbackHandler(
*args,
directory=project_path,
**kwargs
)
server = HTTPServer((HOST, PORT), handler)
local_ip = get_local_ip()
print("\nServer running:")
print(f"• Localhost : http://127.0.0.1:{PORT}")
print(f"• Network : http://{local_ip}:{PORT}")
print(f"• Hosting : {project_path}")
print("\nOpen the Network URL on devices connected to the same Wi-Fi or hotspot.\n")
server.serve_forever()