tags:

views:

194

answers:

7

Problem: to run one.py from a server.

Error

When I try to do it in Mac, I get errors:

$python http://cs.edu.com/u/user/TEST/one.py                       ~ 
/Library/Frameworks/Python.framework/Versions/2.5/Resources/Python.app/Contents/MacOS/Python: can't open file 'http://cs.edu.com/u/user/TEST/one.py': [Errno 2] No such file or directory

one.py is like:

print 1

When I do it in Ubuntu, I get "file is not found".

Question: How can I run Python code from a server?

+3  A: 

So far as I know, the standard Python shell doesn't know how to execute remote scripts. Try using curl or wget to retrieve the script and run it from the local copy.

$ wget http://cs.edu.com/u/user/TEST/one.py
$ python one.py

UPDATE: Based on the question referenced in the comment to this answer, you need to execute one.py based on incoming HTTP requests from end users. The simplest solution is probably CGI, but depending on what else you need to do, a more robust solution might involve a framework of some sort. They each have there strengths and weaknesses, so you should probably consider your requirements carefully before jumping in.

Hank Gay
+1  A: 
wget http://cs.edu.com/u/user/TEST/one.py
python one.py
fortran
+3  A: 

You can't do this. If you have SSH access to the server you can then run the python script located on the server using your SSH connection. If you want to write websites in python google python web frameworks for examples of how to set up and run websites with Python.

Jared
you mean like django?
Masi
+1  A: 

You can mount the remote servers directory with some sort of file networking, like NFS or something. That way it becomes local.

But a better idea is that you explain why you are trying to do this, so we can solve the real usecase. There is most likely tons of better solutions, depending on what the real problem is.

Lennart Regebro
I try to solve my problem here: http://stackoverflow.com/questions/1122609/javascript-for-a-form-on-the-server-side. I want to do it with Python.
Masi
+1  A: 

The python interpreter doesn't know how to read from a URL. The file needs to be local.

However, if you are trying to get the server to execute the python code, you can use mod_python or various kinds of CGI.

You can't do what you are trying to do the way you are trying to do it.

Christopher
You mean I need a thing called "framework" Jared is speaking about.
Masi
Well, it doesn't need to be a "framework", but you do need to setup the server to execute python code on the server side. Frameworks can make things easier if they have libraries to do things you want to do. On the other hand, you might have very simple requirements.
Christopher
+1  A: 

Maybe something like this?

python -c "import urllib; eval(urllib.urlopen(\"http://cs.edu.com/u/user/TEST/one.py").read())"
mp
I like that answer. Probably not what the user really wants but it does directly answer the question.
Bryan Oakley
It's urlopen, not ulropen >P It's a typo, sorry...
mp
Also, you can use this to create your own script that you can later on use to run scripts from another server, or maybe embed it into another script...
mp
New error: http://pastebin.com/m7750adc7
Masi
It is because the file has only a line "print serve1". I cannot understand the meaning of the error.
Masi
The error is that the print statement is trying to print a variable that does not exist. Try the following:print "server1"
mp
Or maybe because you have python v3, the print statement has been replaced by the print function if I remember correctly... So use print("server1") instead.
mp
It's a syntax error, so it's Python 3. Python 3 is not ready for production.
Lennart Regebro
I tested it on python 2.5 and 2.52.
Masi
Try to use exec instead of eval. Also, there can be a problem if the line ending character/s on the server and locally are different. In that case, you can make a search/replace in the retrieved file contents like this: http://pastebin.com/m3f758e73
mp
The same error with both methods: http://pastebin.com/m58b110f3
Masi
You need to use \\n and \\r in case you are running it from shell. \ is a special character, if you want it to be included into the string, you have to put two of them to get one into the string.
mp
+1  A: 

OK, now when you explained, here is a new answer.

You run that script with

 python one.py

It's a server side-script. It's run on the server. It's also located on the server. Why you try to access it via http is beyond me. Run it from the file system.

Although, you should probably look into running Grok or Django or something. This way you'll just end up writing your own Python web framework, you may just as well use one that exists instead. ;)

Lennart Regebro