-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_server.py
More file actions
51 lines (41 loc) Β· 1.39 KB
/
simple_server.py
File metadata and controls
51 lines (41 loc) Β· 1.39 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
#!/usr/bin/env python3
"""
Simple HTTP Server on port 7070
"""
import http.server
import socketserver
import os
import signal
import sys
PORT = 7070
def signal_handler(sig, frame):
print('\nπ Shutting down HTTP server...')
sys.exit(0)
def main():
# Register signal handler for graceful shutdown
signal.signal(signal.SIGINT, signal_handler)
# Change to current directory
os.chdir('.')
# Create server
Handler = http.server.SimpleHTTPRequestHandler
try:
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"π Simple HTTP Server started on port {PORT}")
print(f"π Serving files from: {os.getcwd()}")
print(f"π Access at: http://localhost:{PORT}")
print(f"π External access: http://0.0.0.0:{PORT}")
print("β οΈ Press Ctrl+C to stop the server")
print("-" * 50)
httpd.serve_forever()
except OSError as e:
if "Address already in use" in str(e):
print(f"β Port {PORT} is already in use!")
print(f"π‘ Try: lsof -ti :{PORT} | xargs kill -9")
else:
print(f"β Error starting server: {e}")
except KeyboardInterrupt:
print('\nπ Server stopped by user')
finally:
print('β
HTTP server shutdown complete')
if __name__ == '__main__':
main()