8

I was wondering is any way to execute a python script from a url (www.blahblah.com/script.py) in terminal without downloading the file to disk?

Thanks!

4
  • 1
    Technically, it will be downloaded one way or the other. Can't access what you can't read. Commented Jul 6, 2012 at 21:37
  • 1
    @TheZ: Thats not true. A server-side executing script does just that. Executes server side. What you would receive is the results of the execution. Not the original file. Commented Jul 6, 2012 at 21:42
  • @jdi And that server is doing what exactly? Downloading the file to disk first. Commented Jul 6, 2012 at 21:44
  • @TheZ: The OP is asking for the results of the executed python file as opposed to the contents of the source file to be downloaded. I think you are just misinterpreting. Its perfectly clear that the OP expects to receive results of some nature. Commented Jul 6, 2012 at 21:46

7 Answers 7

13

Do you want this to run on the client, or on the server (which would return the results)?

If you want to run it on the client, it's going to have to be downloaded one way or another. An easy way would be to download, run, delete:

$ wget blahblah.com/script.py && (python script.py; rm script.py)

If you want to run this on the server, you can use CGI as mentioned by others. Depending on what you want to do though, you may want to use a web framework instead.

For a lightweight framework check out Flask. Their documentation is excellent and I managed to get something up and running in a day (I'm fairly new to both Python and web servers).

Sign up to request clarification or add additional context in comments.

Comments

7

Here's what you're looking for:

wget -qO-  https://gist.githubusercontent.com/mattwarrenrnp/6ca5bbeb4959974fb4ac/raw/23196eba76b1f21210f530a05572e38349384e0d/print.py | python -

explanation:

-q for quiet mode hide errors from attempting to be interpreted as python

O- will direct the download to stdout

pipe that stdout to python with the '-' to tell it to execute from stdin.

WARNING: always be sure you trust the script you run like this.

Comments

5

Since you did not mention what type of shell in your terminal. I assume that you are using /bin/bash. Then here is what you are looking for:

python <(wget https://bootstrap.pypa.io/get-pip.py -q -O-)

where

-q quiet (no output).

-O- write documents to standard input

< redirect standard input

Reference: Execute bash script from URL

Comments

4

I know this is a super old thread but just wanted to say none of the other solutions really worked for us partially due to us needing to use input() and that we were hesitant about creating temporary files as there is no guarantee that the script wouldn't error. In the end we went with the following which worked for us:

python3 -c "$(wget -q -O - https://host.com/script.py)"

Comments

1

If you have installed, You can download & execute the script in one go using uv run command.

uv run https://raw.githubusercontent.com/astral-sh/uv/df45b9ac2584824309ff29a6a09421055ad730f6/scripts/uv-run-remote-script-test.py

If the script has dependencies that needs to be downloaded before running the script, You can mention it via --with option.

uv run --with rich,requests <URL_TO_PY_FILE>

This command internally download the script file into a temp directory & execute it.

Comments

0

What you are asking about is called CGI. Python has a module for it:
http://docs.python.org/library/cgi.html

But CGI is a bit dated now, because it is generally a pretty inefficient approach to serving a python application. It is preferred that you use a python web framework of some sort.

Common Gateway Interface

A web server that supports CGI can be configured to interpret a URL that it serves as a reference to a CGI script. A common convention is to have a cgi-bin/ directory at the base of the directory tree and treat all executable files within it as CGI scripts. Another popular convention is to use filename extensions; for instance, if CGI scripts are consistently given the extension .cgi, the web server can be configured to interpret all such files as CGI scripts.
In the case of HTTP PUT or POSTs, the user-submitted data is provided to the program via the standard input. The web server creates a small and efficient subset of the environment variables passed to it and adds details pertinent to the execution of the program.

How this applies to your question is that your script.py would need to first live inside of a cgi-bin or a similar location that your web server is configured to recognize the type. And secondly you would need to make use of the cgi python module to access parameters (and also conform to a request/response format)

Comments

0

You can try curl -s https://www.blahblah.com/script.py | python.

If you can do curl -s https://raw.githubusercontent.com/Killercodes/py/main/codebook/hello.py and it will dump the hello.py to console which can be given to python as input using the following.

curl -s https://raw.githubusercontent.com/Killercodes/py/main/codebook/hello.py | python

Here:

  • curl -s will dump the hello.py from github to your console
  • The pipe | operator will pipe this output to python making it execute.
  • In Bash, the pipe operator | creates a 'pipeline' between command which has a similar usage in windows also making it independent of linux/windows
  • Curl is available even in windows10 natively so this will work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.