-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·90 lines (76 loc) · 2.99 KB
/
server.py
File metadata and controls
executable file
·90 lines (76 loc) · 2.99 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
90
#!/usr/bin/env python3
"""
Simple HTTP server for TryOn website
Run this script to serve the website locally
"""
import http.server
import socketserver
import os
import webbrowser
from pathlib import Path
# Configuration
PORT = 8000
HOST = 'localhost'
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
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', 'Content-Type')
super().end_headers()
def guess_type(self, path):
"""Override to set correct MIME types"""
result = super().guess_type(path)
if isinstance(result, tuple):
mimetype, encoding = result
else:
mimetype = result
encoding = None
if path.endswith('.css'):
return 'text/css'
elif path.endswith('.js'):
return 'application/javascript'
return mimetype
def main():
# Change to the script directory
script_dir = Path(__file__).parent
os.chdir(script_dir)
# Check if required files exist
required_files = ['index.html', 'styles.css', 'script.js']
missing_files = [f for f in required_files if not Path(f).exists()]
if missing_files:
print(f"❌ Missing required files: {', '.join(missing_files)}")
return
print(f"🚀 Starting TryOn website server...")
print(f"📁 Serving from: {script_dir}")
print(f"🌐 Server running at: http://{HOST}:{PORT}")
print(f"📱 Website: http://{HOST}:{PORT}/index.html")
print("\n" + "="*50)
print("TryOn - Virtual Fashion Try-On App")
print("="*50)
try:
with socketserver.TCPServer((HOST, PORT), CustomHTTPRequestHandler) as httpd:
print(f"✅ Server started successfully on port {PORT}")
print(f"🔗 Open your browser and go to: http://{HOST}:{PORT}")
print("\nPress Ctrl+C to stop the server")
print("-" * 50)
# Try to open browser automatically
try:
webbrowser.open(f'http://{HOST}:{PORT}')
print("🌐 Browser opened automatically")
except Exception as e:
print(f"⚠️ Could not open browser automatically: {e}")
print(f" Please manually open: http://{HOST}:{PORT}")
print("-" * 50)
httpd.serve_forever()
except KeyboardInterrupt:
print("\n\n🛑 Server stopped by user")
except OSError as e:
if e.errno == 48: # Address already in use
print(f"❌ Port {PORT} is already in use. Please try a different port or stop the existing server.")
else:
print(f"❌ Error starting server: {e}")
except Exception as e:
print(f"❌ Unexpected error: {e}")
if __name__ == "__main__":
main()