Skip to main content

Using an Ad-Hoc Python Web Server to Catch HTTP Client Requests

nano serv.py
import http.server

bind_address='0.0.0.0'
port=80

class CustomRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        headers = self.headers
        for header, value in headers.items():
            print(f"{header}: {value}")

        self.send_response(418)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(b"Hello, world!")

if __name__ == "__main__":
    http.server.test(HandlerClass=CustomRequestHandler, bind=bind_address, port=port)
sudo python3 ./serv.py