redirect stdout/stderr to browser
This commit is contained in:
18
pier.py
18
pier.py
@ -1,6 +1,9 @@
|
||||
#!/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)
|
||||
|
Reference in New Issue
Block a user