5

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!

user1507886
  • 51
  • 1
  • 1
  • 2
  • 1
    Technically, it will be downloaded one way or the other. Can't access what you can't read. – TheZ Jul 06 '12 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. – jdi Jul 06 '12 at 21:42
  • @jdi And that server is doing what exactly? Downloading the file to disk first. – TheZ Jul 06 '12 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. – jdi Jul 06 '12 at 21:46

5 Answers5

12

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).

Dean
  • 8,632
  • 6
  • 45
  • 61
6

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.

mattwarren
  • 791
  • 8
  • 8
3

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

Community
  • 1
  • 1
karfai
  • 816
  • 1
  • 10
  • 21
3

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)"
Craig
  • 73
  • 7
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)

jdi
  • 90,542
  • 19
  • 167
  • 203