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