Skip to content

Instantly share code, notes, and snippets.

@1kastner
Forked from huyng/reflect.py
Last active March 26, 2024 08:03
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 24 You must be signed in to fork a gist
  • Save 1kastner/e083f9e813c0464e6a2ec8910553e632 to your computer and use it in GitHub Desktop.
Save 1kastner/e083f9e813c0464e6a2ec8910553e632 to your computer and use it in GitHub Desktop.
A simple echo server to inspect http web requests
#!/usr/bin/env python
# Reflects the requests from HTTP methods GET, POST, PUT, and DELETE
# Written by Nathan Hamiel (2010)
from http.server import HTTPServer, BaseHTTPRequestHandler
from optparse import OptionParser
class RequestHandler(BaseHTTPRequestHandler):
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")
self.send_response(200)
self.send_header("Set-Cookie", "foo=bar")
self.end_headers()
def do_POST(self):
request_path = self.path
print("\n----- Request Start ----->\n")
print("Request path:", request_path)
request_headers = self.headers
content_length = request_headers.get('Content-Length')
length = int(content_length) if content_length else 0
print("Content Length:", length)
print("Request headers:", request_headers)
print("Request payload:", self.rfile.read(length))
print("<----- Request End -----\n")
self.send_response(200)
self.end_headers()
do_PUT = do_POST
do_DELETE = do_GET
def main():
port = 8080
print('Listening on 0.0.0.0:%s' % port)
server = HTTPServer(('', port), RequestHandler)
server.serve_forever()
if __name__ == "__main__":
parser = OptionParser()
parser.usage = ("Creates an http-server that will echo out any GET or POST parameters\n"
"Run:\n\n"
" reflect")
(options, args) = parser.parse_args()
main()
@mojimi
Copy link

mojimi commented Dec 27, 2017

Hey thanks for this, how can I send, for example a json message, through the do_GET?

@zopieux
Copy link

zopieux commented Jan 11, 2018

Please change Listening on localhost to Listening on all interfaces or maybe Listening on 0.0.0.0, because using an empty host on HTTPServer means all interfaces, not just localhost. This has serious security implications. I know this is just a test echo server, but it would be great not to mislead users.

@TomFaulkner
Copy link

I noticed this fails when being accessed from fetch due to CORS errors and lacking OPTIONS. I wonder if that is an easy fix?

@harold
Copy link

harold commented Aug 7, 2019

Thanks, this worked first try and helped a lot. 👍

@1kastner
Copy link
Author

1kastner commented Aug 8, 2019

Please change Listening on localhost to Listening on all interfaces or maybe Listening on 0.0.0.0, because using an empty host on HTTPServer means all interfaces, not just localhost. This has serious security implications. I know this is just a test echo server, but it would be great not to mislead users.

Good suggestions, I modified it accordingly.

@1kastner
Copy link
Author

1kastner commented Aug 8, 2019

I noticed this fails when being accessed from fetch due to CORS errors and lacking OPTIONS. I wonder if that is an easy fix?

I have not used this script for a long time, it is rather a template which can be adjusted as needed. If you have a solution, it could be great if you share that gist as well!

@ZechCodes
Copy link

Thanks for this! Came in clutch testing an API wrapper!

@ricleal
Copy link

ricleal commented Jan 21, 2024

Thanks for this!!!!

My version with some colorized output: https://gist.github.com/ricleal/72efc72d7de5e23ce98b9afb2973232a
image

@1kastner
Copy link
Author

Nice, thank you @ricleal!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment