-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·65 lines (55 loc) · 2.22 KB
/
server.py
File metadata and controls
executable file
·65 lines (55 loc) · 2.22 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
"""
Simple HTTP server with directory listing API
Usage: python3 server.py
"""
import os
import json
from http.server import HTTPServer, SimpleHTTPRequestHandler
class DirectoryListingHandler(SimpleHTTPRequestHandler):
def end_headers(self):
# Enable CORS
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET')
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
super().end_headers()
def do_GET(self):
# API endpoint untuk list files
if self.path.startswith('/api/files/'):
folder = self.path.replace('/api/files/', '').strip('/')
self.serve_file_list(folder)
else:
# Serve static files
super().do_GET()
def serve_file_list(self, folder):
try:
folder_path = os.path.join(os.getcwd(), folder)
if not os.path.exists(folder_path):
self.send_error(404, f"Folder not found: {folder}")
return
# Get all files in folder
files = []
for filename in os.listdir(folder_path):
filepath = os.path.join(folder_path, filename)
if os.path.isfile(filepath):
files.append({
'name': filename,
'size': os.path.getsize(filepath)
})
# Sort by name
files.sort(key=lambda x: x['name'])
# Send JSON response
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(files).encode())
except Exception as e:
self.send_error(500, f"Error: {str(e)}")
if __name__ == '__main__':
PORT = 8000
print(f"🚀 Server running at http://localhost:{PORT}")
print(f"📁 API: http://localhost:{PORT}/api/files/content")
print(f"📁 API: http://localhost:{PORT}/api/files/docs")
print(f"\nPress Ctrl+C to stop")
server = HTTPServer(('', PORT), DirectoryListingHandler)
server.serve_forever()