Skip to content

Instantly share code, notes, and snippets.

@gsauthof
Last active September 1, 2023 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsauthof/276a6ca1bd28231dce20f60159f1f569 to your computer and use it in GitHub Desktop.
Save gsauthof/276a6ca1bd28231dce20f60159f1f569 to your computer and use it in GitHub Desktop.
Listen for and dump HTTP requests to STDOUT
#!/usr/bin/env python3
# SPDX-FileCopyrightText: © 2022 Georg Sauthoff <mail@gms.tf>
# SPDX-License-Identifier: BSL-1.0
import argparse
import http.server
import json
import sys
class Dumper(http.server.BaseHTTPRequestHandler):
def do_GET(self, method='GET'):
print(f'\n{method} {self.path}\n{self.headers}')
self.send_response(200)
self.end_headers()
def do_DELETE(self):
return self.do_GET('DELETE')
def do_POST(self, method='POST'):
n = int(self.headers.get('content-length', 0))
body = self.rfile.read(n)
print(f'\n{method} {self.path}\n{self.headers}{body}\n')
if self.headers.get('content-type') == 'application/json':
d = json.loads(body)
print(json.dumps(d, indent=4, sort_keys=True))
print()
self.send_response(200)
self.end_headers()
def do_PUT(self):
return self.do_POST('PUT')
def log_message(self, format, *args):
pass
def main():
p = argparse.ArgumentParser(description='Dump HTTP requests to stdout')
p.add_argument('address', help='bind address')
p.add_argument('port', type=int, help='bind port')
xs = p.parse_args();
s = http.server.HTTPServer((xs.address, xs.port), Dumper)
s.serve_forever()
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment