Last active
November 3, 2015 05:09
-
-
Save toshsan/8f6f073acce4dd30d62c to your computer and use it in GitHub Desktop.
dumpwebserver.py to dump http headers and body. run python dumpwebserver.py 8080
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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
from urlparse import parse_qs | |
from cgi import parse_header, parse_multipart | |
from SocketServer import ForkingMixIn | |
class ForkingHTTPServer(ForkingMixIn, HTTPServer): | |
def finish_request(self, request, client_address): | |
request.settimeout(30) | |
# "super" can not be used because BaseServer is not created from object | |
HTTPServer.finish_request(self, request, client_address) | |
class Handler(BaseHTTPRequestHandler): | |
def _set_headers(self): | |
self.send_response(200) | |
self.send_header('Content-type', 'text/html') | |
self.end_headers() | |
def parse_POST(self): | |
ctype, pdict = parse_header(self.headers.get('content-type', '')) | |
length = int(self.headers.get('content-length', '-1')) | |
if ctype == 'multipart/form-data': | |
postvars = parse_multipart(self.rfile, pdict) | |
elif ctype == 'application/x-www-form-urlencoded': | |
postvars = parse_qs( | |
self.rfile.read(length), | |
keep_blank_values=1) | |
else: | |
postvars = self.rfile.read(length) | |
return postvars | |
def do_POST(self): | |
self._set_headers() | |
print(self.headers) | |
print(self.parse_POST()) | |
def do_GET(self): | |
print(self.headers) | |
self._set_headers() | |
def do_HEAD(self): | |
print(self.headers) | |
self._set_headers() | |
def run(server_class=ForkingHTTPServer, handler_class=Handler, port=80): | |
try: | |
server_address = ('', port) | |
httpd = server_class(server_address, handler_class) | |
print 'Starting httpd...' | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
httpd.socket.close() | |
if __name__ == "__main__": | |
from sys import argv | |
if len(argv) == 2: | |
run(port=int(argv[1])) | |
else: | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment