Skip to content

Instantly share code, notes, and snippets.

@RajatGoyal
Created June 3, 2020 19:28
Show Gist options
  • Save RajatGoyal/b49f1bea737a2c8e6be774a63d1add72 to your computer and use it in GitHub Desktop.
Save RajatGoyal/b49f1bea737a2c8e6be774a63d1add72 to your computer and use it in GitHub Desktop.
http server to dump request ip, headers and check if javascript is executing.
# To start: python3 request_logging_server.py
# returns a html page with
# 1) is javascript working
# 2) request headers
# 3) request ip address
# scraping this page can help you, when you are debugging if the request is using proxy, cookies, and correct headers
# when accessing from the same machine the address would be 0.0.0.0:8004
import http.server
import socketserver
HTML = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html>
<head>
<title>Test website for scraping</title>
<style>
#site {
display: none;
}
</style>
<script src="http://code.jquery.com/jquery-latest.min.js "></script>
<script>
$(document).ready(function() {
$("#noJS").hide();
$("#site").show();
});
</script>
</head>
<body>
<div id="noJS">JavaScript Does not Work..</div>
<div id="site">JavaScript Works.</div>
<hr>
<div id="ip">Your IP address is: %s</div>
<hr>
<div id="headers"> Headers <br/> %s</div>
<hr>
</body>
</html>
"""
PORT = 8004
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
print('server is live at 0.0.0.0:{}'.format(PORT))
def do_GET(self):
# Sending an '200 OK' response
self.send_response(200)
# Setting the header
self.send_header("Content-type", "text/html")
# Whenever using 'send_header', you also have to call 'end_headers'
self.end_headers()
formatted_headers = ""
# Extract headers
headers = self.headers.items()
for header in headers:
formatted_headers += '{}: {} <br/>'.format(header[0], header[1])
html = HTML % (self.client_address[0], formatted_headers)
# Writing the HTML contents with UTF-8
self.wfile.write(bytes(html, "utf8"))
return
# Create an object of the above class
handler_object = MyHttpRequestHandler
my_server = socketserver.TCPServer(("", PORT), handler_object)
try:
my_server.serve_forever()
except KeyboardInterrupt:
pass
my_server.server_close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment