-
-
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
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
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
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()