0

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?

Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • You've got two wildly different sets of answers because there are two different interpretations of what you want to do. Can you make it a little more clear? – Hank Gay Jul 14 '09 at 14:22
  • Hank Gay: I am unsure about the difference. May you clarify your observation? – Léo Léopold Hertz 준영 Jul 14 '09 at 14:32
  • 1
    The first interpretation is that there is some Python script somewhere that you want to execute. That prompted the wget answer. The second interpretation is that you have a server and you want to use a Python script to help you generate dynamic web content. That prompted the CGI and web framework answers. – Hank Gay Jul 14 '09 at 15:01

7 Answers7

4

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.

Community
  • 1
  • 1
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
3

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
  • 39,513
  • 29
  • 110
  • 145
1
wget http://cs.edu.com/u/user/TEST/one.py
python one.py
fortran
  • 74,053
  • 25
  • 135
  • 175
1

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
  • 167,292
  • 41
  • 224
  • 251
1

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
  • 8,815
  • 2
  • 32
  • 41
  • You mean I need a thing called "framework" Jared is speaking about. – Léo Léopold Hertz 준영 Jul 14 '09 at 14:19
  • 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 Jul 14 '09 at 17:22
1

Maybe something like this?

python -c "import urllib; eval(urllib.urlopen(\"http://cs.edu.com/u/user/TEST/one.py").read())"
mp.
  • 182
  • 1
  • 3
  • I like that answer. Probably not what the user really wants but it does directly answer the question. – Bryan Oakley Jul 14 '09 at 14:26
  • 1
    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. Jul 14 '09 at 14:59
  • It is because the file has only a line "print serve1". I cannot understand the meaning of the error. – Léo Léopold Hertz 준영 Jul 14 '09 at 15:05
  • The error is that the print statement is trying to print a variable that does not exist. Try the following: print "server1" – mp. Jul 14 '09 at 15:10
  • 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. Jul 14 '09 at 15:13
  • It's a syntax error, so it's Python 3. Python 3 is not ready for production. – Lennart Regebro Jul 14 '09 at 15:40
  • I tested it on python 2.5 and 2.52. – Léo Léopold Hertz 준영 Jul 14 '09 at 15:47
  • 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. Jul 14 '09 at 16:06
  • 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. Jul 16 '09 at 21:51
1

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
  • 167,292
  • 41
  • 224
  • 251