Code: upi_server.py
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
import urllib
from io import BytesIO
from email.parser import BytesParser
from email.policy import default
UPLOAD_DIR = os.getcwd() # Aktuelles Verzeichnis
BANNER = r"""
_ _ _____ _____ _____ ______ ___ _ _____
| | | | __ \_ _| __ \ / __ \ \ / / \ | |_ _|
| | | | |__) || | | | | | | | \ \ /\ / /| \| | | |
| | | | ___/ | | | | | | | | |\ \/ \/ / | . ` | | |
| |__| | | _| |_| |__| | |__| | \ /\ / | |\ |_| |_
\____/|_| |_____|_____/ \____/ \/ \/ |_| \_|_____|
"""
class UPIHandler(BaseHTTPRequestHandler):
def _send_response(self, html, status=200):
self.send_response(status)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(html.encode())
def _render_page(self):
entries = os.listdir(UPLOAD_DIR)
links = '\n'.join(
f'<li><a href="/uploads/{urllib.parse.quote(f)}">{f}/</a></li>' if os.path.isdir(os.path.join(UPLOAD_DIR, f))
else f'<li><a href="/uploads/{urllib.parse.quote(f)}">{f}</a></li>'
for f in entries
)
return f"""
<html><head><title>UPIDOWNI</title>
<script>
function toggleSubmit() {{
const input = document.querySelector('input[type=file]');
document.querySelector('input[type=submit]').disabled = !input.value;
}}
</script>
</head>
<body style="background-color:black; color:lime; font-family:monospace;">
<pre>{BANNER}</pre>
<h3>Datei hochladen:</h3>
<form enctype="multipart/form-data" method="post">
<input name="file" type="file" onchange="toggleSubmit()" />
<input type="submit" value="Upload" disabled />
</form>
<h3>Verfügbare Dateien & Ordner:</h3>
<ul>{links}</ul>
</body></html>
"""
def do_GET(self):
if self.path.startswith("/uploads/"):
requested = urllib.parse.unquote(self.path[len("/uploads/" ):])
requested_path = os.path.join(UPLOAD_DIR, requested)
if os.path.exists(requested_path):
if os.path.isdir(requested_path):
entries = os.listdir(requested_path)
links = '\n'.join(
f'<li><a href="/uploads/{urllib.parse.quote(os.path.join(requested, e))}">{e}</a></li>'
for e in entries
)
html = f"""
<html><body style='background-color:black; color:lime; font-family:monospace;'>
<h3>Inhalt von {requested}/:</h3><ul>{links}</ul>
</body></html>
"""
self._send_response(html)
else:
self.send_response(200)
self.send_header('Content-Type', 'application/octet-stream')
self.send_header('Content-Disposition', f'attachment; filename="{os.path.basename(requested)}"')
self.end_headers()
with open(requested_path, 'rb') as f:
self.wfile.write(f.read())
else:
self.send_error(404, "Datei nicht gefunden")
else:
self._send_response(self._render_page())
def do_POST(self):
content_type = self.headers.get('Content-Type')
content_length = int(self.headers.get('Content-Length', 0))
if not content_type or 'multipart/form-data' not in content_type:
self.send_error(400, "Invalid content type.")
return
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 and b'name="file"' in part:
try:
headers, filedata = part.split(b'\r\n\r\n', 1)
filedata = filedata.rsplit(b'\r\n', 1)[0]
header_lines = headers.decode(errors='ignore').split("\r\n")
filename = "upload_" + os.urandom(4).hex()
for line in header_lines:
if line.lower().startswith("content-disposition"):
if 'filename="' in line:
filename = line.split('filename="')[1].split('"')[0]
filename = os.path.basename(filename)
filepath = os.path.join(UPLOAD_DIR, filename)
with open(filepath, 'wb') as f:
f.write(filedata)
except Exception as e:
print(f"[!] Fehler beim Speichern der Datei: {e}")
self._send_response(self._render_page())
if __name__ == '__main__':
PORT = 8080
print(f"UPIDOWNI läuft auf http://localhost:{PORT}")
httpd = HTTPServer(('0.0.0.0', PORT), UPIHandler)
httpd.serve_forever()Anwendung
python3 upi_server.py-
Rufe im Browser auf: http://localhost:8080
-
Lade Dateien hoch oder klicke zum Herunterladen.