diff --git a/pier.py b/pier.py index 7ac0387..54c0d61 100755 --- a/pier.py +++ b/pier.py @@ -25,11 +25,7 @@ class ScriptServer(http.server.SimpleHTTPRequestHandler): self.send_result(f"{scr} does not exist. choose from:\n", printable_scripts) return - sio = StringIO() - with redirect_stdout(sio), redirect_stderr(sio): - with open(scr) as f: - exec(f.read()) - res = sio.getvalue() + res = run_script(scr) msg = f"{scr} output:\n" msg += "_" * 80 @@ -46,6 +42,12 @@ class ScriptServer(http.server.SimpleHTTPRequestHandler): print(msg) +def run_script(fn): + sio = StringIO() + with redirect_stdout(sio), redirect_stderr(sio): + with open(fn) as f: + exec(f.read()) + return sio.getvalue() def encode_html(*msg): msg = (str(i) for i in msg)