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

import http.server
import socketserver

PORT = 8000

handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), handler) as httpd:
print("Server started at localhost:" + str(PORT))
httpd.serve_forever()

@sefgit

sefgit commented Dec 25, 2023

Copy link
Copy Markdown
Author

import http.server
import socketserver

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = 'mywebpage.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)

Create an object of the above class

handler_object = MyHttpRequestHandler

PORT = 8000
my_server = socketserver.TCPServer(("", PORT), handler_object)

Star the server

my_server.serve_forever()

@sefgit

sefgit commented Dec 25, 2023

Copy link
Copy Markdown
Author

import http.server
import socketserver
from urllib.parse import urlparse
from urllib.parse import parse_qs

class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# Sending an '200 OK' response
self.send_response(200)

    # Setting the header
    self.send_header("Content-type", "text/html")

    # Whenever using 'send_header', you also have to call 'end_headers'
    self.end_headers()

    # Extract query param
    name = 'World'
    query_components = parse_qs(urlparse(self.path).query)
    if 'name' in query_components:
        name = query_components["name"][0]

    # Some custom HTML code, possibly generated by another function
    html = f"<html><head></head><body><h1>Hello {name}!</h1></body></html>"

    # Writing the HTML contents with UTF-8
    self.wfile.write(bytes(html, "utf8"))

    return

Create an object of the above class

handler_object = MyHttpRequestHandler

PORT = 8000
my_server = socketserver.TCPServer(("", PORT), handler_object)

Star the server

my_server.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