Skip to content

Instantly share code, notes, and snippets.

@cameronhotchkies
Created January 19, 2021 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronhotchkies/30dec9e9c2a44778c3e787b4d842207c to your computer and use it in GitHub Desktop.
Save cameronhotchkies/30dec9e9c2a44778c3e787b4d842207c to your computer and use it in GitHub Desktop.
Python script for logging POST requests to disk
from http.server import BaseHTTPRequestHandler
import socketserver
import time
PORT = 9000
class SinkHandler(BaseHTTPRequestHandler):
def do_POST(self):
incoming = self.rfile
content_length = int(self.headers['content-length'])
content = incoming.read(content_length)
timestamp = time.time_ns()
output_filename = f"{timestamp}.json"
output_file = open(output_filename, "wb")
output_file.write(content)
output_file.close()
self.send_response(200, "rcvd")
self.end_headers()
def run_collector():
with socketserver.TCPServer(("", PORT), SinkHandler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
if __name__ == '__main__':
run_collector()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment