-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
89 lines (71 loc) ยท 2.43 KB
/
Copy pathserver.py
File metadata and controls
89 lines (71 loc) ยท 2.43 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
"""
CODE ARCADE - tiny zero-dependency web server for Termux.
Usage:
python server.py # serve on http://localhost:8080
python server.py 5000 # custom port
"""
import http.server
import socket
import socketserver
import os
import sys
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "public")
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=ROOT, **kwargs)
def end_headers(self):
# Dev-friendly: always fetch fresh files so edits show up on refresh.
self.send_header("Cache-Control", "no-store, no-cache, must-revalidate")
self.send_header("Service-Worker-Allowed", "/")
super().end_headers()
def log_message(self, fmt, *args):
# Quiet log: only show errors, keeps Termux readable.
code = args[1] if len(args) > 1 else ""
if str(code).startswith(("4", "5")):
sys.stderr.write(" [!] %s\n" % (fmt % args))
def lan_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return None
class Server(socketserver.TCPServer):
allow_reuse_address = True
def main():
if not os.path.isdir(ROOT):
print("ERROR: 'public' folder not found next to server.py")
sys.exit(1)
Handler.extensions_map.update({
".js": "text/javascript",
".mjs": "text/javascript",
".webmanifest": "application/manifest+json",
".svg": "image/svg+xml",
})
try:
httpd = Server(("0.0.0.0", PORT), Handler)
except OSError as e:
print("Could not bind port %d (%s)." % (PORT, e))
print("Try: python server.py %d" % (PORT + 1))
sys.exit(1)
ip = lan_ip()
print("")
print(" \033[95m*** CODE ARCADE ***\033[0m")
print(" " + "-" * 34)
print(" On this phone : \033[96mhttp://localhost:%d\033[0m" % PORT)
if ip:
print(" Same Wi-Fi : \033[96mhttp://%s:%d\033[0m" % (ip, PORT))
print(" " + "-" * 34)
print(" Stop with Ctrl+C")
print("")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n Bye! Progress is saved in your browser.\n")
httpd.server_close()
if __name__ == "__main__":
main()