Discuss Scratch

ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

I made a way to run python scripts in the browser.
Make a POST request https://webpython.ninjamar.repl.co/api/runpy and send JSON data to it.
JSON data:
{
    "script":"YOURCODEHERE"
}
Response
{
    "script":XYZ
    "output":XYZ
    "timestamp":XYZ
}
Make sure the repl it running.
ToDo:
send back data better

Last edited by ninjaMAR (Feb. 24, 2021 13:53:35)

imfh
Scratcher
1000+ posts

Run python scripts from the browser

Hmm…
{"script": "import os; os.system('ls')"}

Please don't run this on an important server.
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

imfh wrote:

Hmm…
{"script": "import os; os.system('ls')"}

Please don't run this on an important server.
I run it using this so if you deleted all files I would just restore it.
Could I run the files in a sandbox?
Chiroyce
Scratcher
1000+ posts

Run python scripts from the browser

ninjaMAR wrote:

I made a way to run python scripts in the browser.
Make a POST request https://webpython.ninjamar.repl.co/api/runpy and send JSON data to it.

 ERROR: Hmmmm.... We Couldn't Reach Your Repl

Make sure your repl has a port open and is ready to receive HTTP traffic.
9gr
Scratcher
1000+ posts

Run python scripts from the browser

Do you have repl.it pro? Because you need it to keep the repl on always
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

Chiroyce wrote:

ninjaMAR wrote:

I made a way to run python scripts in the browser.
Make a POST request https://webpython.ninjamar.repl.co/api/runpy and send JSON data to it.

 ERROR: Hmmmm.... We Couldn't Reach Your Repl

Make sure your repl has a port open and is ready to receive HTTP traffic.
I need to run the repl always. I thought the page would get HTTP traffic and start up. For now just go to the page and it should run. I just used a pinger and it works

9gr wrote:

Do you have repl.it pro? Because you need it to keep the repl on always
I do not have repl..it pro

Last edited by ninjaMAR (Feb. 21, 2021 15:20:20)

Danger_script
Scratcher
20 posts

Run python scripts from the browser

You could always also use Jupiter notebook in the browser as well. Jupiter is great for people that are into machine learning in python.
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

Danger_script wrote:

You could always also use Jupiter notebook in the browser as well. Jupiter is great for people that are into machine learning in python.
How would it help me?
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

bump
imfh
Scratcher
1000+ posts

Run python scripts from the browser

ninjaMAR wrote:

imfh wrote:

Hmm…
{"script": "import os; os.system('ls')"}

Please don't run this on an important server.
I run it using this so if you deleted all files I would just restore it.
Could I run the files in a sandbox?
Ehh, maybe? https://wiki.python.org/moin/SandboxedPython

Someone could also download your scripts using something like:
import os; os.system("tar -cz . | base64")
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

imfh wrote:

ninjaMAR wrote:

imfh wrote:

Hmm…
{"script": "import os; os.system('ls')"}

Please don't run this on an important server.
I run it using this so if you deleted all files I would just restore it.
Could I run the files in a sandbox?
Ehh, maybe? https://wiki.python.org/moin/SandboxedPython

Someone could also download your scripts using something like:
import os; os.system("tar -cz . | base64")
I need a way to restrict terminal commands
Firesoldier999
Scratcher
28 posts

Run python scripts from the browser

ninjaMAR wrote:

imfh wrote:

ninjaMAR wrote:

imfh wrote:

Hmm…
{"script": "import os; os.system('ls')"}

Please don't run this on an important server.
I run it using this so if you deleted all files I would just restore it.
Could I run the files in a sandbox?
Ehh, maybe? https://wiki.python.org/moin/SandboxedPython

Someone could also download your scripts using something like:
import os; os.system("tar -cz . | base64")
I need a way to restrict terminal commands
Install proot with the terminal command
if ! which proot > /dev/null; then install-pkg proot; fi
(make sure to do this when the server starts up)
Now create a directory named “sandbox”, and run `proot -r sandbox python <filename>.py` whenever you need to run a script. Make sure to copy all of Python's dependencies (such as libc) and maybe /bin/sh into the sandbox directory.
Firesoldier999
Scratcher
28 posts

Run python scripts from the browser

Also, I can kill the running server and override it with my own:
import os
from flask import Flask
# kill running python instance
os.system('killall python')
# start new server
app = Flask(__name__)
@app.route('/')
def index():
return 'hacked'
app.run(host='0.0.0.0', port=8080)
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

Firesoldier999 wrote:

ninjaMAR wrote:

imfh wrote:

ninjaMAR wrote:

imfh wrote:

Hmm…
{"script": "import os; os.system('ls')"}

Please don't run this on an important server.
I run it using this so if you deleted all files I would just restore it.
Could I run the files in a sandbox?
Ehh, maybe? https://wiki.python.org/moin/SandboxedPython

Someone could also download your scripts using something like:
import os; os.system("tar -cz . | base64")
I need a way to restrict terminal commands
Install proot with the terminal command
if ! which proot > /dev/null; then install-pkg proot; fi
(make sure to do this when the server starts up)
Now create a directory named “sandbox”, and run `proot -r sandbox python <filename>.py` whenever you need to run a script. Make sure to copy all of Python's dependencies (such as libc) and maybe /bin/sh into the sandbox directory.
I can not get the sandbox to work
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

bump
ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

bump
Chiroyce
Scratcher
1000+ posts

Run python scripts from the browser

Ok, I maybe acting like a noob but whats wrong with this?


I am running that here https://webpython.ninjamar.repl.co/api/runpy

Last edited by Chiroyce (Feb. 24, 2021 14:14:20)

ninjaMAR
Scratcher
1000+ posts

Run python scripts from the browser

Chiroyce wrote:

Ok, I maybe acting like a noob but whats wrong with this?


I am running that here https://webpython.ninjamar.repl.co/api/runpy
You never send the request.
const data = {
    "script":"CODEHERE"
}
const url = 'https://webpython.ninjamar.repl.co/api/runpy'
fetch(url, {method: "POST", body: JSON.parse(data)});
Source

Last edited by ninjaMAR (Feb. 24, 2021 22:48:34)

Chiroyce
Scratcher
1000+ posts

Run python scripts from the browser

Is this a problem with my browser?
Firefox:



I tried with the latest version of Chrome:



Last edited by Chiroyce (Feb. 24, 2021 14:31:56)

imfh
Scratcher
1000+ posts

Run python scripts from the browser

You need to put the script in quotes. print(“Hello”) and ‘print(“Hello”)’ are two different things. print by itself is a function that I believe returns nothing, while the one inside quotes is a string which is sent to the server.

Powered by DjangoBB