-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
33 lines (28 loc) · 851 Bytes
/
server.py
File metadata and controls
33 lines (28 loc) · 851 Bytes
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
import http.server
import socketserver
import socket
PORT = 8080
def get_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('10.255.255.255', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
LOCAL_IP = get_ip()
class MyHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
super().end_headers()
with socketserver.TCPServer(("", PORT), MyHandler) as httpd:
print(f"http://{LOCAL_IP}:{PORT}")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server.")
httpd.shutdown()