-
-
Save ubiquitousthey/193e0f898b79183f67200b3f10d4f728 to your computer and use it in GitHub Desktop.
A simple echo server to inspect http web requests
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 | |
# Reflects the requests from HTTP methods GET, POST, PUT, and DELETE | |
# Written by Nathan Hamiel (2010) | |
# Added to by Heath Robinson (2018) | |
from http.server import HTTPServer, SimpleHTTPRequestHandler, HTTPStatus | |
import argparse | |
import os | |
import hashlib | |
class RequestHandler(SimpleHTTPRequestHandler): | |
def do_GET(self): | |
request_path = self.path | |
print("\n----- Request Start ----->\n") | |
print("Request path:", request_path) | |
print("Request headers:", self.headers) | |
print("<----- Request End -----\n") | |
super().do_GET() | |
def do_POST(self): | |
request_path = self.path | |
request_headers = self.headers | |
content_length = request_headers.get('Content-Length') | |
length = int(content_length) if content_length else 0 | |
payload = self.rfile.read(length).decode('utf-8') | |
print("\n----- Request Start ----->\n") | |
print("Request path:", request_path) | |
print("Content Length:", length) | |
print("Request headers:", request_headers) | |
print("Request payload:", payload) | |
print("<----- Request End -----\n") | |
m = hashlib.md5() | |
m.update(request_path.encode('utf-8')) | |
m.update(payload.encode('utf-8')) | |
response_file = os.path.join(os.getcwd(), m.hexdigest()) | |
path = self.translate_path(request_path) | |
ctype = self.guess_type(path) | |
self.send_response(HTTPStatus.OK) | |
self.send_header("Content-type", ctype) | |
try: | |
with open(path, 'rb') as f: | |
fs = os.fstat(f.fileno()) | |
self.send_header("Content-Length", str(fs[6])) | |
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime)) | |
self.end_headers() | |
self.copyfile(f, self.wfile) | |
except OSError: | |
self.end_headers() | |
self.wfile.write(b'{}') | |
def serve(args): | |
port = args.port | |
print('Listening on localhost:%s' % port) | |
server = HTTPServer(('', port), RequestHandler) | |
server.serve_forever() | |
def calculate(args): | |
m = hashlib.md5() | |
m.update(args.path) | |
m.update(args.body) | |
response_file = os.path.join(current_path, m.hexdigest()) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-p", "--port", type=int, help="Port to run the web server on", default=8000) | |
parser.add_argument("-c", "--calculate", | |
help="Calculate hash on request for creating post response file. Include --body and --path", | |
action='store_true', default=False) | |
parser.add_argument("-b", "--body", help="Body on which to calculate hash for response file") | |
parser.add_argument("-a", "--path", help="Path on which to calculate hash for response file") | |
parser.usage = ("Creates an http-server that will echo out any GET or POST parameters\n" | |
"Run:\n\n" | |
" reflect") | |
args = parser.parse_args() | |
print(args) | |
if args.calculate: | |
calculate(args) | |
else: | |
serve(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment