Created
January 1, 2018 13:20
-
-
Save arjunmenon/137baf5de1880fe6ec8bdc5f659df3de to your computer and use it in GitHub Desktop.
Python server monitring any HTTP 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 | |
# Read requests from HTTP methods GET, POST, PUT, DELETE, NOTIFY or any other custom method | |
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_NOTIFY = do_POST | |
do_DELETE = do_GET | |
def main(): | |
port = 8081 | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment