#!/usr/bin/env python3 ## This script starts a Web server providing texts and files sharing function. ## See the usage with `-h` option. import os import sys import argparse import urllib.parse from http.server import HTTPServer, BaseHTTPRequestHandler from html import escape text_log = [] class SimpleHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/': self.respond(200, "text/html; charset=utf-8", self.render_main_page()) elif self.path.startswith("/files/"): filename = os.path.basename(urllib.parse.unquote(self.path[len("/files/"):])) file_path = os.path.join(self.server.directory, filename) if os.path.isfile(file_path): with open(file_path, "rb") as f: data = f.read() self.send_response(200) self.send_header("Content-Disposition", f"attachment; filename*=UTF-8''{urllib.parse.quote(filename)}") self.send_header("Content-Type", "application/octet-stream") self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data) else: self.send_error(404, "File not found") else: self.send_error(404) def do_POST(self): content_type = self.headers.get("Content-Type", "") content_length = int(self.headers.get("Content-Length", "0")) body = self.rfile.read(content_length) if self.path == "/upload" and "multipart/form-data" in content_type: boundary = content_type.split("boundary=")[-1].encode() parts = body.split(b"--" + boundary) for part in parts: if b"filename=" in part: header_data, file_data = part.split(b"\r\n\r\n", 1) file_data = file_data.rstrip(b"\r\n--") disposition = [line for line in header_data.split(b"\r\n") if b"Content-Disposition" in line][0] disposition_str = disposition.decode("utf-8", errors="replace") filename_enc = disposition_str.split("filename=")[-1].strip().strip('"') filename = os.path.basename(filename_enc) if filename: with open(os.path.join(self.server.directory, filename), "wb") as f: f.write(file_data) self.redirect("/") elif self.path == "/text": fields = urllib.parse.parse_qs(body.decode('utf-8', errors='replace')) text = fields.get("text", [""])[0] text_log.append(text) print("Received text:", text) self.redirect("/") else: self.send_error(400) def render_main_page(self): file_list_html = "".join( f"
{log_html}
        
        """
    def respond(self, status, content_type, content):
        content_bytes = content.encode("utf-8")
        self.send_response(status)
        self.send_header("Content-Type", content_type)
        self.send_header("Content-Length", str(len(content_bytes)))
        self.end_headers()
        self.wfile.write(content_bytes)
    def redirect(self, location):
        self.send_response(303)
        self.send_header("Location", location)
        self.end_headers()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--dir", default=".", help="Directory to serve files from (default: current directory)")
    parser.add_argument("--port", type=int, default=7123, help="Port to listen on (default: 7123)")
    args = parser.parse_args()
    directory = os.path.abspath(args.dir)
    if not os.path.isdir(directory):
        print("Directory does not exist:", directory)
        sys.exit(1)
    server = HTTPServer(('', args.port), SimpleHandler)
    server.directory = directory
    print(f"Serving at http://localhost:{args.port}")
    print(f"Sharing directory: {directory}")
    server.serve_forever()
if __name__ == "__main__":
    main()