Last active
February 5, 2026 19:55
-
-
Save sughodke/e19cdfc75fa9129ee8c752293c694ccc to your computer and use it in GitHub Desktop.
Simple web server that will pretty print all requests coming to it
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
| #!/usr/bin/env python3 | |
| import json | |
| from http.server import BaseHTTPRequestHandler, HTTPServer | |
| # ANSI colors | |
| RESET, BOLD, BLUE, CYAN, YELLOW = "\033[0m", "\033[1m", "\033[34m", "\033[36m", "\033[33m" | |
| def pretty_json(data): | |
| try: | |
| return json.dumps(json.loads(data), indent=2) | |
| except: | |
| return data | |
| class Handler(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| self._handle() | |
| do_POST = do_PUT = do_PATCH = do_DELETE = do_OPTIONS = do_HEAD = do_GET | |
| def _handle(self): | |
| # Read body | |
| length = int(self.headers.get("Content-Length", 0)) | |
| body = self.rfile.read(length).decode(errors="replace") if length else None | |
| # Print request | |
| print(f"\n{BOLD}{BLUE}→ {self.command} {self.path}{RESET}") | |
| for k, v in self.headers.items(): | |
| print(f"{CYAN}{k}:{RESET} {v}") | |
| if body: | |
| print(f"\n{YELLOW}{pretty_json(body)}{RESET}") | |
| # Send response | |
| self.send_response(204) | |
| self.end_headers() | |
| def log_message(self, *_): | |
| pass | |
| if __name__ == "__main__": | |
| print("Listening on http://0.0.0.0:8000") | |
| HTTPServer(("0.0.0.0", 8000), Handler).serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment