added commandline args incl. script dir selection; handle ctrl+c

This commit is contained in:
2022-05-10 11:22:19 +02:00
parent f615daf250
commit 37075b3494
3 changed files with 36 additions and 7 deletions

43
pier.py
View File

@ -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()