-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
65 lines (53 loc) · 2.27 KB
/
server.py
File metadata and controls
65 lines (53 loc) · 2.27 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
#!/usr/bin/env python3
"""
Techfinite MVP Development Server
Simple HTTP server for local development
"""
import http.server
import socketserver
import os
import sys
from pathlib import Path
class TechfiniteHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
"""Custom request handler with CORS support and better error pages"""
def end_headers(self):
# Add CORS headers for local development
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', '*')
super().end_headers()
def do_GET(self):
# Handle root path
if self.path == '/':
self.path = '/index.html'
# Serve static files
super().do_GET()
def log_message(self, format, *args):
# Custom logging format
print(f"\033[94m[Techfinite MVP]\033[0m {self.address_string()} - {format % args}")
def run_server(port=8000):
"""Run the development server"""
# Change to the directory containing this script
os.chdir(Path(__file__).parent)
try:
with socketserver.TCPServer(("", port), TechfiniteHTTPRequestHandler) as httpd:
print(f"\n\033[92m✓ Techfinite MVP Server Started\033[0m")
print(f"\033[94m➤ Local: \033[0m http://localhost:{port}")
print(f"\033[94m➤ Network: \033[0m http://0.0.0.0:{port}")
print(f"\033[93m\n🚀 Ready to showcase Techfinite's technology solutions!\033[0m")
print(f"\033[90mPress Ctrl+C to stop the server\033[0m\n")
httpd.serve_forever()
except KeyboardInterrupt:
print(f"\n\033[91m✕ Server stopped\033[0m")
sys.exit(0)
except OSError as e:
if e.errno == 48: # Address already in use
print(f"\033[91m✗ Port {port} is already in use. Try a different port:\033[0m")
print(f"\033[94mpython3 server.py {port + 1}\033[0m")
else:
print(f"\033[91m✗ Error starting server: {e}\033[0m")
sys.exit(1)
if __name__ == "__main__":
# Get port from command line argument or use default
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
run_server(port)