diff options
author | ache <ache@ache.one> | 2023-12-02 08:10:37 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2023-12-02 08:10:37 +0100 |
commit | 3ea7eb1376f91df739fb92258a79ded78be3c30c (patch) | |
tree | 2de65de2ba54a593992d7885b8fb1534cfa39131 | |
parent | Update 404 stategy (diff) |
Update pyhttpd to parse cli arguments
-rw-r--r-- | pyhttpd.py | 18 |
1 files changed, 13 insertions, 5 deletions
@@ -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() |