Created
May 20, 2020 07:49
-
-
Save traut/5b2b5e21040bbac486c30081049d3b18 to your computer and use it in GitHub Desktop.
Static HTTP server in Python that saves all POST requests as files in the current directory
This file contains 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
import os | |
from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler | |
class CustomHTTPRequestHandler(SimpleHTTPRequestHandler): | |
def do_POST(self): | |
filename = os.path.basename(self.path) | |
file_length = int(self.headers['Content-Length']) | |
with open(filename, 'wb') as output_file: | |
output_file.write(self.rfile.read(file_length)) | |
self.send_response(201, 'Created') | |
self.end_headers() | |
reply_body = 'Saved "{}"\n'.format(filename) | |
self.wfile.write(reply_body.encode('utf-8')) | |
httpd = HTTPServer(('0.0.0.0', 8000), CustomHTTPRequestHandler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment