Created
April 23, 2026 14:18
-
-
Save tkuhn/c1e76618b104999182e0e322b538920f to your computer and use it in GitHub Desktop.
local HTTP server that supports PUT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import os | |
| from http.server import SimpleHTTPRequestHandler, HTTPServer | |
| class PUTHandler(SimpleHTTPRequestHandler): | |
| def do_PUT(self): | |
| path = self.translate_path(self.path) | |
| os.makedirs(os.path.dirname(path), exist_ok=True) | |
| length = int(self.headers.get("Content-Length", 0)) | |
| with open(path, "wb") as f: | |
| # Stream in chunks so large uploads don't blow up memory | |
| remaining = length | |
| while remaining > 0: | |
| chunk = self.rfile.read(min(65536, remaining)) | |
| if not chunk: | |
| break | |
| f.write(chunk) | |
| remaining -= len(chunk) | |
| self.send_response(201 if not os.path.exists(path) else 204) | |
| self.end_headers() | |
| if __name__ == "__main__": | |
| HTTPServer(("", 8000), PUTHandler).serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment