diff --git a/pier.py b/pier.py index 6f004aa..ce282f3 100755 --- a/pier.py +++ b/pier.py @@ -1,18 +1,34 @@ #!/usr/bin/env python3 +DIR = "." +HOST = "localhost" +PORT = 9090 + + +import argparse + +parser = argparse.ArgumentParser( + description="pier - Python Interpreter Executing Remotely", + formatter_class=argparse.ArgumentDefaultsHelpFormatter +) + +parser.add_argument("-d", "--dir", default=DIR, help="directory containing python scripts") +parser.add_argument("-H", "--host", default=HOST, help="host name") +parser.add_argument("-P", "--port", default=PORT, help="port number", type=int) + +clargs = parser.parse_args() + + from contextlib import redirect_stdout, redirect_stderr from functools import lru_cache from glob import glob from io import StringIO +import os import http.server import socketserver -HOST = "localhost" -PORT = 9090 - - class ScriptServer(http.server.SimpleHTTPRequestHandler): def do_GET(self): @@ -71,10 +87,23 @@ def encode_html(*msg): if __name__ == "__main__": + try: + os.chdir(clargs.dir) + except Exception as e: + tn = type(e).__name__ + msg = f"{tn}: {e}" + raise SystemExit(msg) + + host = clargs.host + port = clargs.port + socketserver.TCPServer.allow_reuse_address = True - with socketserver.ThreadingTCPServer((HOST, PORT), ScriptServer) as httpd: - print(f"serving at {HOST}:{PORT}") - httpd.serve_forever() + with socketserver.ThreadingTCPServer((host, port), ScriptServer) as httpd: + print(f"serving at {host}:{port}") + try: + httpd.serve_forever() + except KeyboardInterrupt: + print() diff --git a/example1.py b/scripts/example1.py similarity index 100% rename from example1.py rename to scripts/example1.py diff --git a/example2.py b/scripts/example2.py similarity index 100% rename from example2.py rename to scripts/example2.py