-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_server.py
More file actions
executable file
·64 lines (52 loc) · 1.97 KB
/
test_server.py
File metadata and controls
executable file
·64 lines (52 loc) · 1.97 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
#!/usr/bin/env python
"""
Simple HTTP server for testing media downloads.
Serves files from demo_data directory.
"""
import os
import http.server
import socketserver
from pathlib import Path
PORT = 8001
DIRECTORY = 'demo_data'
class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def end_headers(self):
# Add CORS headers for cross-origin requests
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
super().end_headers()
def log_message(self, format, *args):
# Custom log format
print(f'[TEST SERVER] {self.address_string()} - {format % args}')
if __name__ == '__main__':
# Verify demo_data directory exists
if not os.path.exists(DIRECTORY):
print(f'Error: {DIRECTORY} directory not found!')
print(f'Please create it with: mkdir {DIRECTORY}')
exit(1)
# List available files
print(f'\n{"=" * 60}')
print(f'TEST SERVER - Serving files from {DIRECTORY}/')
print(f'{"=" * 60}')
print(f'\nServer running on: http://localhost:{PORT}/')
print('\nAvailable test URLs:')
demo_path = Path(DIRECTORY)
for file in sorted(demo_path.iterdir()):
if file.is_file():
file_size = file.stat().st_size
size_mb = file_size / (1024 * 1024)
url = f'http://localhost:{PORT}/{file.name}'
print(f' - {url}')
print(f' ({size_mb:.2f} MB)')
print(f'\n{"=" * 60}')
print('Press Ctrl+C to stop the server\n')
# Start server
with socketserver.TCPServer(('', PORT), CustomHTTPRequestHandler) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('\n\nShutting down test server...')
httpd.shutdown()