mirror of
https://github.com/jimeh/dotfiles.git
synced 2026-02-19 09:06:44 +00:00
Add http-echo-server binary
It prints complete GET and POST requests to STDOUT. Sometimes useful as a dirty hack way to debug what HTTP requests are being made.
This commit is contained in:
59
bin/http-echo-server
Executable file
59
bin/http-echo-server
Executable file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/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 = (
|
||||||
|
"usage: serv.py [-p PORT]\n" \
|
||||||
|
"\n" \
|
||||||
|
"Creates a http-server that will echo out any GET or POST parameters."
|
||||||
|
)
|
||||||
|
parser.add_option("-p", "--port", help="Port to bind to", type="int",
|
||||||
|
default=8000)
|
||||||
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
|
main(options.port)
|
||||||
Reference in New Issue
Block a user