Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
augustin_s
pier
Commits
700eb1e7
Commit
700eb1e7
authored
May 10, 2022
by
augustin_s
🐍
Browse files
redirect stdout/stderr to browser
parent
38923f5b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
4 deletions
+14
-4
pier.py
pier.py
+14
-4
No files found.
pier.py
View file @
700eb1e7
#!/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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment