-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
189 lines (159 loc) Β· 5.54 KB
/
server.py
File metadata and controls
189 lines (159 loc) Β· 5.54 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os
import sys
import time
import threading
import subprocess
from http.server import SimpleHTTPRequestHandler, HTTPServer
FILES_DIR = os.path.join(os.getcwd(), "files")
class CustomHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=FILES_DIR, **kwargs)
def do_GET(self):
try:
# Handle the refresh button request
if self.path == "/refresh":
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(self.get_html_page("π Restarting Server..."))
threading.Thread(target=self.restart_server).start()
return
# Serve the default page
if self.path == "/":
self.send_response(200)
self.send_header("Content-type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(self.get_html_page())
return
# Serve the CSS content directly
if self.path == "/style.css":
self.send_response(200)
self.send_header("Content-type", "text/css; charset=utf-8")
self.end_headers()
self.wfile.write(self.get_css().encode('utf-8'))
return
# Call the default request handler for other GET requests
super().do_GET()
except Exception as e:
print(f"Error while processing request for {self.path}: {e}")
def get_html_page(self, message="Welcome to the File Server!"):
"""Generate HTML page content."""
html = f"""
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Server</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<div class="container">
<h1>π File Server</h1>
<p>{message}</p>
<button onclick="window.location.href='/refresh'">π Refresh Server</button>
<h2>π Available Files</h2>
<ul>
"""
# List files in the 'files' directory
for filename in os.listdir(FILES_DIR):
file_path = os.path.join(FILES_DIR, filename)
if os.path.isfile(file_path):
html += f'<li><a href="/{filename}" download>{filename}</a></li>'
html += """
</ul>
</div>
</body>
</html>
"""
return html.encode('utf-8')
def get_css(self):
"""Return the CSS as a string."""
css = """
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background-color: #f4f7fc;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
padding: 20px;
background-color: white;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
width: 80%;
max-width: 800px;
}
h1 {
font-size: 2.5em;
color: #4CAF50;
}
h2 {
color: #555;
margin-bottom: 15px;
}
p {
font-size: 1.2em;
color: #888;
margin-bottom: 30px;
}
button {
background-color: #4CAF50;
color: white;
font-size: 1.2em;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
ul {
list-style-type: none;
padding: 0;
}
ul li {
margin: 10px 0;
}
ul li a {
color: #007BFF;
text-decoration: none;
font-size: 1.1em;
}
ul li a:hover {
text-decoration: underline;
}
"""
return css
def restart_server(self):
"""Restart the server."""
print("π Restarting server...")
time.sleep(2)
python = sys.executable
script = os.path.abspath(sys.argv[0])
# Start a new process for the server
try:
subprocess.Popen([python, script])
print("New server process started successfully.")
except Exception as e:
print(f"Error restarting the server: {e}")
# Terminate the current server process
os._exit(0)
def run(server_class=HTTPServer, handler_class=CustomHandler, port=8080):
"""Start the server and handle requests."""
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f"π Server started at: http://localhost:{port}")
print(f"π Serving directory: {FILES_DIR}")
try:
httpd.serve_forever()
except Exception as e:
print(f"Server stopped due to error: {e}")
if __name__ == "__main__":
run()