-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTTP File Server v.08.py
More file actions
416 lines (399 loc) · 17.8 KB
/
HTTP File Server v.08.py
File metadata and controls
416 lines (399 loc) · 17.8 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from urllib.parse import unquote
import http.server
import os
import re
import socket
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
image_map = {
"/artifact.gif": "artifact.gif",
"/server.gif": "server.gif",
"/webicon.gif": "webicon.gif"
}
# Check if the path matches one of the image files
if self.path in image_map:
file_path = image_map[self.path]
if os.path.exists(file_path):
try:
with open(file_path, "rb") as f:
self.send_response(200)
self.send_header("Content-type", "image/gif")
self.end_headers()
self.wfile.write(f.read())
except Exception as e:
self.send_response(500)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(f"<html><body>Error reading image: {e}</body></html>".encode())
else:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"<html><body>Image not found</body></html>")
# List files in the root directory
elif self.path == "/list":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
# List files from the root directory (excluding certain files) and from the 'downloads' folder
files = []
directories_to_check = ["./downloads"]
for directory in directories_to_check:
if os.path.exists(directory):
files.extend([
f for f in os.listdir(directory)
if os.path.isfile(os.path.join(directory, f)) and not f.endswith(".py") and f != "artifact.gif" and f != "server.gif" and f != "webicon.gif"
])
# Generate the links for the files found
file_links = "".join(
f'<li><a href="/download/{file}">{file}</a></li>' for file in files
)
self.wfile.write(f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Index</title>
<style>
body {{
background-color: #c3c3c3;
font-family: Geneva, sans-serif;
margin: 0;
padding-bottom: 50px;
min-height: 100vh;
}}
.dialog {{
background-color: #e0e0e0;
border: 2px solid black;
padding: 20px;
width: 400px;
text-align: center;
margin: 20px auto;
}}
.dialog ul {{
list-style-type: none;
padding: 0;
}}
.dialog ul li {{
margin: 10px 0;
}}
.dialog a {{
text-decoration: none;
color: blue;
}}
.dialog a:hover {{
text-decoration: underline;
}}
.dialog h1 {{
font-size: 18px;
margin-bottom: 20px;
color: black;
}}
footer {{
text-align: center;
margin-top: 20px;
}}
</style>
</head>
<body>
<div class="dialog">
<h1>File Index</h1>
<ul>
{file_links}
</ul>
<a href="/">Back to Upload Page</a>
</div>
<footer>
<p>Created by Xenocide21 | Date: 22-11-24</p>
</footer>
</body>
</html>
""".encode())
# Handle file downloads
elif self.path.startswith("/download/"):
file_name = self.path[len("/download/"):]
# Decode URL-encoded string (e.g., '%20' becomes a space)
file_name = unquote(file_name)
# Check in the root directory and the 'downloads' folder
directories_to_check = ["./downloads"]
for directory in directories_to_check:
file_path = os.path.join(directory, file_name)
if os.path.isfile(file_path):
# Serve the file if found
self.send_response(200)
self.send_header("Content-type", "application/octet-stream")
self.send_header("Content-Disposition", f"attachment; filename={file_name}")
self.end_headers()
with open(file_path, "rb") as file:
self.wfile.write(file.read())
return
self.send_response(500)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(f"<html><body>Error downloading file: {e}</body></html>".encode())
else:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"<html><body>File not found</body></html>")
else:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTTP File Uploader</title>
<style>
body {
background-color: #c3c3c3;
font-family: Geneva, sans-serif;
margin: 0;
padding-bottom: 50px;
min-height: 100vh;
}
table {
width: 100%;
margin-top: 20px;
margin-bottom: 20px;
}
.panel {
background-color: #e0e0e0;
border: 2px solid black;
padding: 20px;
width: 240px;
height: 350px;
text-align: center;
position: relative; /* Allow absolute positioning of the icon */
}
.sidebar {
width: 300px;
}
.sidebar h2 {
font-size: 16px;
color: black;
margin-bottom: 10px;
}
.sidebar ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.sidebar ul li {
margin: 5px 0;
background-color: #c3c3c3;
border: 2px solid black;
}
.sidebar ul li:hover {
background-color: #d3d3d3;
}
.sidebar ul li a {
text-decoration: none;
color: #0066cc; /* Link color */
display: block; /* Ensure the whole area is clickable */
padding: 5px 10px; /* Add some padding for the hover effect */
}
.sidebar ul li a:hover {
text-decoration: underline;
color: #0044aa; /* Hover color */
background-color: #d3d3d3; /* Darker grey background on hover */
}
.dialog h1 {
font-size: 18px;
margin-bottom: 20px;
color: black;
}
.dialog form {
margin: 0;
}
.dialog input[type="file"] {
margin: 10px 0;
}
.dialog input[type="submit"] {
padding: 5px 10px;
background-color: #c3c3c3;
border: 2px solid black;
cursor: pointer;
}
.dialog input[type="submit"]:hover {
background-color: #d3d3d3;
}
.gif-container {
text-align: center;
margin-bottom: 20px;
}
.gif-container img {
width: 140px; /* Resize the GIF */
height: auto; /* Maintain aspect ratio */
}
footer {
clear: both;
text-align: center;
margin-top: 20px;
}
/* Styling for the icons */
.icon {
position: absolute;
top: 8px;
right: 10px;
width: 50px; /* Set to 25px as requested */
height: auto;
}
</style>
</head>
<body>
<table>
<tr>
<!-- Sidebar -->
<td class="sidebar" valign="top">
<div class="panel">
<!-- Useful Links box icon -->
<img src="/webicon.gif" class="icon" alt="Useful Links Icon">
<h2>Useful Links</h2>
<ul>
<li><a href="http://macintoshgarden.org" target="_blank">Macintosh Garden</a></li>
<li><a href="http://macintoshrepository.org" target="_blank">Macintosh Repository</a></li>
<li><a href="http://archive.org" target="_blank">Archive.org</a></li>
<li><a href="http://frogfind.com" target="_blank">Frogfind</a></li>
<li><a href="http://theoldnet.com" target="_blank">The Old Net</a></li>
<li><a href="http://retronetwork.net" target="_blank">RetroNetwork</a></li>
</ul>
</div>
</td>
<!-- Main file upload section -->
<td class="dialog" valign="top">
<div class="panel">
<!-- HTTP File Server box icon -->
<img src="/server.gif" class="icon" alt="HTTP File Server Icon">
<h1> HTTP File Server </h1>
<div class="gif-container">
<img src="/artifact.gif" alt="Artifact GIF">
</div>
<h1>Upload File</h1>
<form enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input type="submit" value="Upload File" />
</form>
<div>
<a href="/list">View All Files</a>
</div>
</div>
</td>
</tr>
</table>
<footer>
<p>Created by Xenocide21 | Date: 22-11-24</p>
<p>Arbitration: <a href="https://www.vecteezy.com/free-vector/internet-icon">Internet Icon Vectors by Vecteezy</a> <a href="https://www.vecteezy.com/free-vector/network-server">Network Server Vectors by Vecteezy</a> </p>
</footer>
</body>
</html>
""")
def do_POST(self):
# Handle file uploads
content_length = int(self.headers['Content-Length'])
content_type = self.headers['Content-Type']
# Ensure the multipart form-data is detected
if "multipart/form-data" in content_type:
boundary = content_type.split("boundary=")[1].encode()
body = self.rfile.read(content_length)
parts = body.split(b"--" + boundary)
for part in parts:
if b"Content-Disposition" in part:
match = re.search(b'filename="([^"]+)"', part)
if match:
filename = match.group(1).decode()
header, file_data = part.split(b"\r\n\r\n", 1)
file_data = file_data.rsplit(b"\r\n", 1)[0]
# Save file to /downloads folder
download_dir = './downloads'
if not os.path.exists(download_dir):
os.makedirs(download_dir) # Create directory if it doesn't exist
file_path = os.path.join(download_dir, filename)
with open(file_path, "wb") as f:
f.write(file_data)
# Display success page with consistent style
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload Success</title>
<style>
body {{
background-color: #c3c3c3;
font-family: Geneva, sans-serif;
margin: 0;
padding-bottom: 50px;
min-height: 100vh;
}}
.dialog {{
background-color: #e0f7e0;
border: 2px solid black;
padding: 20px;
width: 300px;
text-align: center;
margin: 20px auto;
}}
.dialog h1 {{
font-size: 18px;
margin-bottom: 20px;
color: green;
}}
.dialog p {{
color: black;
font-size: 16px;
}}
.dialog a {{
text-decoration: none;
color: blue;
}}
.dialog a:hover {{
text-decoration: underline;
}}
footer {{
text-align: center;
margin-top: 20px;
}}
</style>
</head>
<body>
<div class="dialog">
<h1>File Uploaded Successfully!</h1>
<p>File: <strong>{filename}</strong></p>
<a href="/">Back to Upload Page</a>
</div>
<footer>
<p>Created by Xenocide21 | Date: 22-11-24</p>
</footer>
</body>
</html>
""".encode())
return
self.send_response(400)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"<html><body>Error processing request</body></html>")
def get_local_ip():
"""Get the local machine's IP address for all network interfaces"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0)
try:
# Connect to a remote server and retrieve the local address used for the connection
s.connect(('10.254.254.254', 1)) # Arbitrary remote address
local_ip = s.getsockname()[0]
except Exception:
local_ip = '127.0.0.1' # Fallback to localhost if connection fails
finally:
s.close()
return local_ip
if __name__ == "__main__":
PORT = 8080
local_ip = get_local_ip() # Get the local IP address
server = http.server.HTTPServer((local_ip, PORT), SimpleHTTPRequestHandler)
print(f"Serving on http://{local_ip}:{PORT}")
server.serve_forever()