#!/usr/bin/env python3
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
import shutil
import sys

class QuietHTTPRequestHandler(SimpleHTTPRequestHandler):
    def copyfile(self, source, outputfile):
        try:
            # 执行文件传输
            shutil.copyfileobj(source, outputfile)
        except (ConnectionResetError, BrokenPipeError):
            # 客户端主动断开连接，静默处理，仅打印简单提示（可选）
            # 如果想完全安静，可以把下面这行注释掉
            print(f"[-] Client {self.client_address[0]} disconnected prematurely.")
        except Exception as e:
            # 其他未知错误可选择性打印
            print(f"[!] Error sending file: {e}")

if __name__ == '__main__':
    port = 8000
    server = ThreadingHTTPServer(('0.0.0.0', port), QuietHTTPRequestHandler)
    print(f"[+] Serving HTTP on 0.0.0.0 port {port} (多线程模式，Ctrl+C 停止)...")
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("\n[+] Shutting down...")
        server.shutdown()
