added commandline args incl. script dir selection; handle ctrl+c
This commit is contained in:
43
pier.py
43
pier.py
@ -1,18 +1,34 @@
|
|||||||
#!/usr/bin/env python3
|
#!/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 contextlib import redirect_stdout, redirect_stderr
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from glob import glob
|
from glob import glob
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
|
import os
|
||||||
import http.server
|
import http.server
|
||||||
import socketserver
|
import socketserver
|
||||||
|
|
||||||
|
|
||||||
HOST = "localhost"
|
|
||||||
PORT = 9090
|
|
||||||
|
|
||||||
|
|
||||||
class ScriptServer(http.server.SimpleHTTPRequestHandler):
|
class ScriptServer(http.server.SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
@ -71,10 +87,23 @@ def encode_html(*msg):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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
|
socketserver.TCPServer.allow_reuse_address = True
|
||||||
with socketserver.ThreadingTCPServer((HOST, PORT), ScriptServer) as httpd:
|
with socketserver.ThreadingTCPServer((host, port), ScriptServer) as httpd:
|
||||||
print(f"serving at {HOST}:{PORT}")
|
print(f"serving at {host}:{port}")
|
||||||
httpd.serve_forever()
|
try:
|
||||||
|
httpd.serve_forever()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user