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?

3
  • 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? Commented Jul 14, 2009 at 14:22
  • Hank Gay: I am unsure about the difference. May you clarify your observation? Commented Jul 14, 2009 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. Commented Jul 14, 2009 at 15:01

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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.

1 Comment

I try to solve my problem here: stackoverflow.com/questions/1122609/…. I want to do it with Python.
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.

2 Comments

You mean I need a thing called "framework" Jared is speaking about.
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.
1

Maybe something like this?

python -c "import urllib; eval(urllib.urlopen(\"http://cs.edu.com/u/user/TEST/one.py").read())"

9 Comments

I like that answer. Probably not what the user really wants but it does directly answer the question.
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...
It is because the file has only a line "print serve1". I cannot understand the meaning of the error.
The error is that the print statement is trying to print a variable that does not exist. Try the following: print "server1"
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.
|
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. ;)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.