Created
January 19, 2021 07:21
-
-
Save cameronhotchkies/30dec9e9c2a44778c3e787b4d842207c to your computer and use it in GitHub Desktop.
Python script for logging POST requests to disk
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 | |
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