Commit 700eb1e7 authored by augustin_s's avatar augustin_s 🐍
Browse files

redirect stdout/stderr to browser

parent 38923f5b
#!/usr/bin/env python3
from glob import glob
from io import StringIO
from contextlib import redirect_stdout, redirect_stderr
import http.server
import socketserver
......@@ -22,23 +25,30 @@ class ScriptServer(http.server.SimpleHTTPRequestHandler):
self.send_result(f"{scr} does not exist. choose from:\n", printable_scripts)
return
with open(scr) as f:
exec(f.read())
sio = StringIO()
with redirect_stdout(sio), redirect_stderr(sio):
with open(scr) as f:
exec(f.read())
res = sio.getvalue()
self.send_result(scr)
msg = f"{scr} output:\n"
msg += "_" * 80
msg += f"\n{res}"
self.send_result(msg)
def send_result(self, *msg):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
msg = encode_html(msg)
msg = encode_html(*msg)
self.wfile.write(msg)
print(msg)
def encode_html(*msg):
msg = (str(i) for i in msg)
msg = " ".join(msg)
msg = msg.split("\n")
msg = "<br>".join(msg)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment