-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (31 loc) · 1 KB
/
server.py
File metadata and controls
36 lines (31 loc) · 1 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
#!/usr/bin/env python3
import http.server
import socket
import sys
PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
def get_local_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 "unknown"
class Handler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
super().end_headers()
def log_message(self, *args):
pass
local_ip = get_local_ip()
print(f"Learn Python! is running.")
print(f" Local: http://localhost:{PORT}")
print(f" Network: http://{local_ip}:{PORT}")
print(f"\nPress Ctrl+C to stop.\n")
with http.server.HTTPServer(("0.0.0.0", PORT), Handler) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")