Skip to content

Instantly share code, notes, and snippets.

@devasheeshG
Created July 30, 2025 17:18
Show Gist options
  • Save devasheeshG/6dd3df3da349c92c391037b3720fb3aa to your computer and use it in GitHub Desktop.
Save devasheeshG/6dd3df3da349c92c391037b3720fb3aa to your computer and use it in GitHub Desktop.
Python HTTP Server to Print All Request Details
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from urllib.parse import parse_qs, urlparse
class RequestHandler(BaseHTTPRequestHandler):
def _handle_request(self):
# Print request line
print(f"\n{'=' * 50}")
print(f"Request received: {self.command} {self.path}")
# Parse URL and query parameters
parsed_url = urlparse(self.path)
path = parsed_url.path
query_params = parse_qs(parsed_url.query)
# Print path
print(f"\nPATH: {path}")
# Print query parameters
if query_params:
print("\nQUERY PARAMETERS:")
for param, values in query_params.items():
for value in values:
print(f" {param}: {value}")
# Print headers
print("\nHEADERS:")
for header in self.headers:
print(f" {header}: {self.headers[header]}")
# Print cookies
if 'Cookie' in self.headers:
print("\nCOOKIES:")
cookies = self.headers['Cookie'].split('; ')
for cookie in cookies:
if '=' in cookie:
name, value = cookie.split('=', 1)
print(f" {name}: {value}")
# Get and print body data
content_length = int(self.headers.get('Content-Length', 0))
if content_length > 0:
body = self.rfile.read(content_length)
print("\nRAW BODY DATA:")
try:
body_str = body.decode('utf-8')
print(body_str)
# Try to parse as JSON
try:
json_data = json.loads(body_str)
print("\nJSON DATA:")
print(json.dumps(json_data, indent=2))
except json.JSONDecodeError:
# Try to parse as form data
if self.headers.get('Content-Type') == 'application/x-www-form-urlencoded':
form_data = parse_qs(body_str)
print("\nFORM DATA:")
for key, values in form_data.items():
for value in values:
print(f" {key}: {value}")
except UnicodeDecodeError:
print(" [Binary data]")
print(f"{'=' * 50}\n")
# Send response
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write(b"Request details printed to console")
# Handle all HTTP methods
def do_GET(self):
self._handle_request()
def do_POST(self):
self._handle_request()
def do_PUT(self):
self._handle_request()
def do_DELETE(self):
self._handle_request()
def do_PATCH(self):
self._handle_request()
def do_OPTIONS(self):
self._handle_request()
def do_HEAD(self):
self._handle_request()
if __name__ == '__main__':
server_address = ('', 8000)
httpd = HTTPServer(server_address, RequestHandler)
print("HTTP server running on http://localhost:8000")
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment