Created
March 9, 2023 20:03
-
-
Save oskarsh/7f43a9fdd0a9d638d8634cca4b5ac824 to your computer and use it in GitHub Desktop.
This gist provides a simple http route on localhost:4000/test/ printing out the headers and the payload. Used for debugging only
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
from http.server import BaseHTTPRequestHandler, HTTPServer | |
from urllib.parse import urlparse | |
class RequestHandler(BaseHTTPRequestHandler): | |
def do_POST(self): | |
# Parse the request URL | |
parsed_path = urlparse(self.path) | |
print(parsed_path) | |
# Check if the requested URL is /test | |
if parsed_path.path == "/test/": | |
# Extract the headers from the request | |
headers = str(self.headers) | |
# Extract the input from the request | |
content_length = int(self.headers['Content-Length']) | |
post_data = self.rfile.read(content_length) | |
# Print the headers and input to the console | |
print("Headers:") | |
print(headers) | |
print("Input:") | |
print(post_data) | |
# Send a response back to the client | |
self.send_response(200) | |
self.send_header('Content-type', 'text/plain') | |
self.end_headers() | |
self.wfile.write(headers.encode()) | |
self.wfile.write(b'\r\n') | |
self.wfile.write(post_data) | |
return | |
# If the requested URL is not /test/, return a 404 response | |
self.send_response(404) | |
self.send_header('Content-type', 'text/plain') | |
self.end_headers() | |
self.wfile.write(b'404 Not Found') | |
return | |
# Start the server on port 8080 | |
server_address = ('localhost', 4000) | |
print("Server running on") | |
print(server_address) | |
httpd = HTTPServer(server_address, RequestHandler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment