Skip to content

Instantly share code, notes, and snippets.

@traut
Created May 20, 2020 07:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save traut/5b2b5e21040bbac486c30081049d3b18 to your computer and use it in GitHub Desktop.
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
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