Skip to content

Instantly share code, notes, and snippets.

@jeffbrl
Created April 12, 2024 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffbrl/292c494dabb641c1d049259b6690d404 to your computer and use it in GitHub Desktop.
Save jeffbrl/292c494dabb641c1d049259b6690d404 to your computer and use it in GitHub Desktop.
Simple python webserver with HTTP header printing
import sys
from http.server import SimpleHTTPRequestHandler, HTTPServer
DEFAULT_PORT=8080
class RequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
print(self.headers)
SimpleHTTPRequestHandler.do_GET(self)
if __name__ == "__main__":
port = DEFAULT_PORT
if len(sys.argv) > 1:
try:
port = int(sys.argv[1])
except ValueError:
print(f"{sys.argv[1]} is not a valid port")
sys.exit(0)
httpd = HTTPServer(('', port), RequestHandler)
print(f"Serving at port {port}")
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment