#!/usr/bin/env python import sys, os from email.parser import BytesParser from email.policy import default print("Content-Type: text/html; charset=utf-8") print("") CONTENT_LENGTH = os.environ.get("CONTENT_LENGTH") CONTENT_TYPE = os.environ.get("CONTENT_TYPE") if CONTENT_TYPE is None or CONTENT_LENGTH is None: exit() length = int(CONTENT_LENGTH) raw = b"Content-Type: " + CONTENT_TYPE.encode() + b"\r\n\r\n" + sys.stdin.buffer.read(length) msg = BytesParser(policy=default).parsebytes(raw) for part in msg.iter_parts(): name = part.get_filename() if name is None: continue with open(os.path.join("uploads", os.path.basename(name)), "wb") as f: f.write(part.get_payload(decode=True))