Skip to content

Instantly share code, notes, and snippets.

@huyng
Created February 7, 2011 17:57
Show Gist options
  • Select an option

  • Save huyng/814831 to your computer and use it in GitHub Desktop.

Select an option

Save huyng/814831 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 BaseHTTPServer 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)
print(self.headers)
print("<----- Request End -----\n")
self.send_response(200)
self.send_header("Set-Cookie", "foo=bar")
def do_POST(self):
request_path = self.path
print("\n----- Request Start ----->\n")
print(request_path)
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
print(request_headers)
print(self.rfile.read(length))
print("<----- Request End -----\n")
self.send_response(200)
do_PUT = do_POST
do_DELETE = do_GET
def main():
port = 8080
print('Listening on localhost:%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()
@kaos

kaos commented Jul 20, 2016

Copy link
Copy Markdown

Nice. A minor HTTP protocol glitch though, as there's no self.end_headers() so the response is malformed when served to picky clients (haproxy). ;)

@boergsen

Copy link
Copy Markdown

πŸ‘

@1kastner

1kastner commented Aug 15, 2017

Copy link
Copy Markdown

Thanks a lot for the work! I did two adjustments in my fork (at [0]):

  • since python 3.3 you need to call self.end_headers() explicitly
  • the getheaders semantics has changed and request_headers.get("Content-Length") seems more appropriate
  • BaseHTTPServer was replaced by http.server in newer python versions
  • My printing statements are more explicit.

[0] https://gist.github.com/1kastner/e083f9e813c0464e6a2ec8910553e632

@coverboy

Copy link
Copy Markdown

Awesome work!!

@g10guang

Copy link
Copy Markdown

In Python3.6, keep do this:

self.send_response(200)
self.end_headers()
 self.wfile.write(json.dumps(j_body).encode())

or maybe client cannot get the status code. I have met this problem, so I mark it.

@rangent

rangent commented Mar 5, 2019

Copy link
Copy Markdown

Thank you so much for providing this solution! Saved me what was likely an hour at work to figure out how to do this!

@weiqiang333

Copy link
Copy Markdown

πŸ‘

@shadiakiki1986

shadiakiki1986 commented Dec 19, 2019

Copy link
Copy Markdown

I've found the below alternatives to be of great use

@petersont

Copy link
Copy Markdown

Also saved me a lot of time, thanks for posting : )

@huyng

huyng commented Jan 23, 2020

Copy link
Copy Markdown
Author

Haha, no problem. What are you guys using this for? I posted this thing years ago and forgot about it ...

@nirajchandak

Copy link
Copy Markdown

I've found the below alternatives to be of great use

πŸ‘πŸ»

@Huweicai

Copy link
Copy Markdown

I've found the below alternatives to be of great use

Thanks~ webhook.site is pretty useful~

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