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
74e39010
Commit
74e39010
authored
May 11, 2022
by
augustin_s
🐍
Browse files
handle scripts with arguments
parent
2d4199cc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
4 deletions
+18
-4
pier.py
pier.py
+18
-4
No files found.
pier.py
View file @
74e39010
...
...
@@ -25,14 +25,20 @@ from glob import glob
from
io
import
StringIO
import
os
import
sys
import
http.server
import
socketserver
HTML_SPACE
=
"%20"
class
ScriptServer
(
http
.
server
.
SimpleHTTPRequestHandler
):
def
do_GET
(
self
):
scr
=
self
.
path
.
strip
(
"/"
)
args
=
self
.
path
.
lstrip
(
"/"
).
split
(
HTML_SPACE
)
scr
=
args
[
0
]
if
not
scr
.
endswith
(
".py"
):
scr
+=
".py"
...
...
@@ -42,7 +48,7 @@ class ScriptServer(http.server.SimpleHTTPRequestHandler):
self
.
send_result
(
f
"
{
scr
}
does not exist. choose from:
\n
"
,
printable_scripts
)
return
res
=
run_script
(
scr
)
res
=
run_script
(
scr
,
args
)
msg
=
f
"
{
scr
}
output:
\n
"
msg
+=
"_"
*
80
...
...
@@ -60,11 +66,19 @@ class ScriptServer(http.server.SimpleHTTPRequestHandler):
def
run_script
(
fn
):
def
run_script
(
fn
,
args
):
sio
=
StringIO
()
with
redirect_stdout
(
sio
),
redirect_stderr
(
sio
):
code
=
open_script
(
fn
)
exec
(
code
)
old_sys_argv
,
sys
.
argv
=
sys
.
argv
,
args
try
:
exec
(
code
)
except
BaseException
as
e
:
# need to catch SystemExit etc. as well
print
(
"_"
*
80
)
tn
=
type
(
e
).
__name__
msg
=
f
"
{
tn
}
:
{
e
}
"
print
(
msg
,
file
=
sys
.
stderr
)
sys
.
argv
=
old_sys_argv
return
sio
.
getvalue
()
@
lru_cache
()
...
...
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