Skip to content

Instantly share code, notes, and snippets.

@sefgit
Forked from traut/static-http-receiver.py
Created December 25, 2023 01:33
Show Gist options
  • Select an option

  • Save sefgit/40a4d09312d85837a965d4a017164120 to your computer and use it in GitHub Desktop.

Select an option

Save sefgit/40a4d09312d85837a965d4a017164120 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()
@sefgit

sefgit commented Dec 25, 2023

Copy link
Copy Markdown
Author

from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl

httpd = HTTPServer(('localhost', 4443), SimpleHTTPRequestHandler)

httpd.socket = ssl.wrap_socket(httpd.socket,
keyfile="/path/to/key.pem",
certfile='/path/to/cert.pem',
server_side=True)

httpd.serve_forever()

@sefgit

sefgit commented Dec 25, 2023

Copy link
Copy Markdown
Author

@sefgit

sefgit commented Dec 25, 2023

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment