简体   繁体   中英

How to run a python or bash script interactively on a webpage?

I am building a website and I would like to show a terminal on my webpage which runs a script (python or bash) interactively.

Something like trinket.io but I would like to use the python interpreter or the bash I have on my server, so I could install pip packages and in general control every aspect of the script.

I was thinking at something like an interactive frame which shows the terminal and what's executed in it, obv with user interaction supported.

A good example is https://create.withcode.uk/ , it's exactly what I want but I would like to host it on my own server with my own modules and ecosystem. This seems to be pretty good also on the security side.

Is there anything like that?

If I understand well you look for a mechanism, that allows you to display a terminal on a web server.

Then you want to run an interactive python script on that terminal, right.

So in the end the solution to share a terminal does not necessarily have to be written in python, right? (Though I must admit that I prefer python solutions if I find them, but sometimes being pragmatic isn't a bad idea)

You might google for http and terminal emulators.

Perhaps ttyd fits the bill. https://github.com/tsl0922/ttyd

Building on linux could be done with

sudo apt-get install build-essential cmake git libjson-c-dev libwebsockets-dev
git clone https://github.com/tsl0922/ttyd.git
cd ttyd && mkdir build && cd build
cmake ..
make && make install

Usage would be something like: ttyd -p 8888 yourpythonscript.py

and then you could connect with a web browser with http://hostip:8888

you might of course 'hide' this url behind a reverse proxy and add authentification to it or add options like --credential username:password to password protect the url.

Addendum: If you want to share multiple scripts with different people and the shareing is more a on the fly thing, then you might look at tty-share ( https://github.com/elisescu/tty-share ) and tty-server ( https://github.com/elisescu/tty-server )

tty-server can be run in a docker container. tty-share can be used to run a script on your machine on one of your terminals. It will output a url, that you can give to the person you want to share the specific session with) If you think that's interesting I might elaborate on this one

>> Insert security disclaimer here <<

Easiest most hacktastic way to do it is to create a div element where you'll store your output and an input element to enter commands. Then you can ajax POST the command to a back-end controller.

The controller would take the command and run it while capturing the output of the command and sending it back to the web page for it to render it in the div

In python I use this to capture command output:

from subprocess import Popen, STDOUT, PIPE

proc = Popen(['ls', '-l'], stdout=PIPE, stderr=STDOUT, cwd='/working/directory')
proc.wait()
return proc.stdout.read()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM