Last active
April 28, 2017 00:39
-
-
Save ppar/29c75e557671e6ba0ff5894a824be1df to your computer and use it in GitHub Desktop.
Simple HTTP Sink server for Python 2, dumps all GET/HEAD/POST/PUT requests to stdout
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
#!/usr/bin/env python | |
# Simple HTTP Sink server for Python 2 | |
# Dumps all received GET/HEAD/POST/PUT requests to stdout, returns configurable HTTP response code | |
# Usage: | |
# python http_sink.py [listen_port] [http_result_code] | |
# Based on https://gist.github.com/bradmontgomery/2219997 | |
from __future__ import print_function | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
class S(BaseHTTPRequestHandler): | |
def _start_request(self): | |
print("------------------------------------------------------") | |
print("{} {} {}".format(self.command, self.path, self.request_version)) | |
for h in self.headers: | |
print("> {}: {}".format(h, self.headers[h])) | |
def _do_upload(self): | |
self._start_request() | |
content_length = int(self.headers['Content-Length']) | |
post_data = self.rfile.read(content_length) | |
print("{}".format(post_data)) | |
self._finish_request() | |
def _finish_request(self): | |
self.send_response(self.resp_code) | |
self.send_header('Content-type', 'text/plain') | |
self.end_headers() | |
self.wfile.write("Done\n") | |
def do_GET(self): | |
self._start_request() | |
self._finish_request() | |
def do_HEAD(self): | |
self._start_request() | |
self._finish_request() | |
def do_POST(self): | |
self._do_upload() | |
def do_PUT(self): | |
self._do_upload() | |
#def run(server_class=HTTPServer, handler_class=S, port=port, resp_code=resp_code): | |
if __name__ == "__main__": | |
from sys import argv | |
port = 80 | |
resp_code = 200 | |
if len(argv) >= 2: | |
port = int(argv[1]) | |
if len(argv) >= 3: | |
resp_code = int(argv[2]) | |
if len(argv) >= 4: | |
print("Too many aguments") | |
exit(1) | |
#run(port, resp_code) | |
S.resp_code = resp_code | |
server_address = ('', port) | |
httpd = HTTPServer(server_address, S) | |
print('Starting httpd...') | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment