From 3ea7eb1376f91df739fb92258a79ded78be3c30c Mon Sep 17 00:00:00 2001 From: ache Date: Sat, 2 Dec 2023 08:10:37 +0100 Subject: Update pyhttpd to parse cli arguments --- pyhttpd.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pyhttpd.py b/pyhttpd.py index bd546a7..78bd156 100644 --- a/pyhttpd.py +++ b/pyhttpd.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 """Use this instead of `python3 -m http.server` when you need CORS""" +import argparse from http.server import HTTPServer, SimpleHTTPRequestHandler from pathlib import Path -serverHost = ('localhost', 8080) class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): @@ -57,10 +57,18 @@ class CORSRequestHandler(SimpleHTTPRequestHandler): SimpleHTTPRequestHandler.send_error(self, code, message) -httpd = HTTPServer(serverHost, CORSRequestHandler) +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Automaticaly set up a HTTP server') + parser.add_argument('-H', '--host', type=str, default='0.0.0.0', help='Host to serve on') + parser.add_argument('-p', '--port', type=int, default=8080, help='Port to serve on') + args = parser.parse_args() -# Print the server's host and port -print(f"Listening on {serverHost[0]} port {serverHost[1]}") + host, port = args.host, args.port + # Print the server's host and port + print(f"Opening http://{host}:{port}", end='') -httpd.serve_forever() + httpd = HTTPServer((host, port), CORSRequestHandler) + print(f"\rServed on http://{host}:{port}") + + httpd.serve_forever() -- cgit v1.2.3