-1

I am using a Mac.

I have a Python file on a server. Let's say it's here: http://jake.com/python_file.py. I want to make a script that I can run in Terminal by double-clicking and will run the Python file in Terminal on the local machine. Any ideas?

Would I have to use SSH to connect to the server and download the file to a temporary location on the hard drive (/tmp?) and then delete it when I'm done? But there's another problem with this. It would have to download the Python file to a location in the user's home folder because I don't think users have the necessary permissions to write to the folder /tmp or /var or something like that.

I was looking around for a solution to my problem and found this. It talks about how to execute a remote script using SSH on unix but I tried doing this with my Python file and it didn't work.

In case you didn't realize, the main reason why I am looking to do this is so that a user can run the file locally but they are unable to read/edit the actual Python file which is stored on the server.

If anyone has any ideas on how to accomplish this (whether using the ideas mentioned above or not) please let me know I'd really appreciate it!

Thanks, Jake

Community
  • 1
  • 1
Jake
  • 319
  • 1
  • 7
  • 21
  • It would be unusual for users not to have write access to `/tmp`. Also, if the user can download the script, then they can read and modify it as they like - ssh will only restrict the set of users that can do that. – mhawke Oct 22 '14 at 05:00
  • @mhawke That's ok. I don't really care if anyone reads the script I only care about the Python file. But if they don't know where the python file is then they can't read it. – Jake Oct 22 '14 at 05:05
  • By "script" I meant the remote Python file. How will you hide it's location from a determined user? – mhawke Oct 22 '14 at 05:08
  • @mhawke Well if the file has to be downloaded to the user's computer then I guess they could find it but I think it would be pretty difficult to find if it is buried within the filesystem. But if the file could be run remotely, then no matter how determined the user is they will not be able to access it. I may be completely wrong here, but wouldn't it be possible to run the Python file server-side like PHP so that the file is never actually on the user's computer just the output. – Jake Oct 22 '14 at 05:15
  • Sorry, perhaps I don't understand. Your question is (was?) how to run a python script stored on a remote server on the local machine - as you say somehow copy the python script to the local machine, then execute it. If you want to run the python script remotely, that's a completely different thing for which there are many solutions. – mhawke Oct 22 '14 at 05:17
  • @mhawke Sorry about that. I was unclear in my question. I don't know how to do it. One of the ideas I had was to use SSH to copy the script to local system. I see how there are problems with that method, but I didn't (and still don't) know whether or not the Python script can be run remotely on the server and the output returned to the user. I am new to Python so I don't know what is and isn't possible with it. If that is possible, I have no idea what to do with that. Thanks for taking the time work this out with me I really appreciate it. – Jake Oct 22 '14 at 05:23
  • There's no way of running a python script locally in the console without the user potentially being able to read the contents of that python script, just like the python interpreter does. If you want to make an HTTP endpoint that shows the output of a python script via an HTTP request (which your console app could access remotely via wget, etc), then expose the output of your script/function via an HTTP endpoint using a web framework like [flask](http://flask.pocoo.org/) – Aaron Oct 22 '14 at 05:34

2 Answers2

0

How about a python script to download and execute the code like so?

import requests 
py1 = requests.get('https://example.com/file.py').content
exec(py1, globals(), locals())

note: I'm using the requests library, but you could just as easily use the built in httplib's HTTPSConnection. It's just more verbose.

note 2: When you say you don't want the user to be able to "read/edit the actual Python file", they will be able to read it if they open the url themselves and view the content. they are just less likely to edit the file locally and mess something up. You can also deploy updates to your python script to the URL rather than having to copy them locally to every machine. This could be a security risk depending on the scope of what you are using it for.

Aaron
  • 2,341
  • 21
  • 27
  • Thanks for your answer. I installed the requests library but I am getting an error when running the Python file you provided. Here is the error: `Traceback (most recent call last): File "/Users/jake 1/Dropbox/Python/downloadpy.py", line 3, in exec(py1, globals(), locals()) File "", line 1 ^ SyntaxError: invalid syntax` – Jake Oct 22 '14 at 05:46
  • Open the URL that replaces 'https://example.com/file.py'? Is it python code, or does it start with ' – Aaron Oct 22 '14 at 05:56
0

Assuming that the remote Python script is a single file with no dependencies other than those of the Python standard library, and that a compatible version of Python is installed on the user's local machine, there are a few obvious ways to do it.

Use ssh:

#!/usr/bin/env sh
ssh user@host cat /path/to/python/script.py | python

Or use scp:

#!/usr/bin/env sh
TMPFILE=/tmp/$$
scp user@host:/path/to/python/script.py $TMPFILE
python $TMPFILE
rm $TMPFILE

The first one has the obvious advantage of not requiring any mucking about with copying files and cleaning up afterwards.

If you wanted to execute the python script on the remote server, this can also be done with ssh:

ssh user@host python /path/to/python/script.py

Standard input and output will be a terminal on the user's local machine.

mhawke
  • 84,695
  • 9
  • 117
  • 138