Created
April 12, 2024 20:15
-
-
Save jeffbrl/292c494dabb641c1d049259b6690d404 to your computer and use it in GitHub Desktop.
Simple python webserver with HTTP header printing
This file contains hidden or 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
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