0

Can I use os.system() or subprocess.call() to execute a Python program on a webserver?

I mean can I write these functions in a .py script and run it from a web browser and expect the program to be executed?

Thanks a lot.

EDIT: Sorry for all the confusion, I am giving you more background to my problem.

The reason I am trying to do is this. I have a Python program that accepts an XML file and returns me TTF file. I run that program in terminal like this:

ttx somefile.xml 

After which it does all the work and generates a ttf file. Now when I deploy this script as a module on web server. I use a to allow user to browse and select the XML file. Then I read the file data to temporary file and then pass the file to the module script to be executed like this:

ttx.main([temp_filename]) 

Is this right way to do it? Because at this point, I don't get any error in the log or in browser. I get blank screen.

When this didn't work, I was going to try os.system or subprocess.call

1
  • your question needs improvements: The second sentence is a much better place to expand. THe first sentence looks like you misunderstand what those functions are meant for. You should be researching writing "CGI" scripts, and "web services" with Python, perhaps. Commented Jul 15, 2010 at 17:12

5 Answers 5

1

You do not use os.system or subprocess.call to execute something as a cgi process.

Maybe you should read the Python cgi tutorial here:

http://www.cs.virginia.edu/~lab2q/

If you want your cgi process to communicate with another process on your local machine, you might want to look at "REST frameworks" for Python.

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

Comments

0

So long as your server is configured to run CGI scripts (Apache's documentation for that is here, for example), yes, you can execute a python script from a webserver. Simply make sure the script is in the appropriate cgi-bin/ directory and that the file has executable permission on the server.

With regards to your comments:

  1. You can, if you really want, explicitly allow other folders on the server to run executable code. I don't know what server you're using, but on Apache this is done by setting Option +ExecCGI for the folder you want. Again, see the docs I linked to.
  2. You need to give an absolute path with respect to the server. As an example, a site I develop has the layout: /public_html/cgi-bin/ When I want to access .cgi or .py files, the url for the site is something like http://chess.narnia.homeunix.com/cgi-bin/index.cgi. You can also set up re-directs to certain files if you want.
  3. One way to pass parameters through your browser is to append them to the URL like an HTTP POST method. Here's a good example of doing that.

Is that what you were looking for with your question, or did you want to actually invoke the python script with os.system()?

4 Comments

Thanks. I am trying to do is this. I have a Python program that accepts an XML file and returns me TTF file. I run that porgram in terminal like this: ttx somefile.xml ......After which it does all the work and generates a ttf file. Now when I deploy this script as a module on web server. I use a <form> to allow user to browse and select the XML file. Then I read the file data to temporory file and then pass the file to the module script to be executed like this: ttx.main([temp_filename]) Is this right way to do it? when this didnt work, I was going to try os.system or subprocess.call
So the form executes a different python script when it is submitted? If that is the case, then yes, you can import another python script into the one that processes the form. It's difficult to know if calling ttx.main() would necessarily work without seeing your code, but you can certainly import one .py file into another (or into a .cgi that was written in python).
Yes the import is working correctly because if I send wrong number of arguments to the main function, the script throws an error. The problem is, how can I trace something inside the main function. All my efforts at doing req.write() failed....... I might seem like doing stupid things but I am new to Python....:)
You're using mod_python and inline PSP to do this? That's quite different from CGI scripting, and is actually an attempt to replace traditional CGI methods. You may want to post some of the code from your web-form, to make it easier to see exactly how you're calling this script and passing the .xml file to it.
0

Yes, I do it all the time. Import as you would do normally, stick your .py in your cgi-bin folder and make sure the server is capable of handling python.

4 Comments

I dont have the script under cgi-bin. Will it still work? Secondly, do I have to give absolute path to the python program? and lastly, how do i pass parameters to it? Thanks
but not using os.system or subprocess.call as the method to invoke the cgi, right?
@ssdesign I just noticed that bit of ambiguity in the question, too. I've amended my answer to try to help with your extra questions, but you might want to go back and try to clarify your original question.
@ssdeisgn, if the script isn't under cgi-bin, it shouldn't execute the script, it will give normal access to users (i.e., display the code). If you want to execute a second python program2, you can do that from os.system, but for program1 to execute, it has to be executed itself, which usually only happens when program1 is in cgi-bin.
0

Another option would be to simply create an application on Google's App Engine. That gives you oodles of resources and APIs for Python execution.

http://code.google.com/appengine

Comments

0

I've done it quite a bit in classic ASP on IIS 5 and above. I would have the ASP engine execute python code (instead of, e.g., vbscript (hearkening back to the old days, here)). Behind those asp pages would be python modules written in straight python that could be imported and could execute pretty much arbitrary code. As others have mentioned, the effective user needs to have execute permission on the thing you're trying to execute.

4 Comments

hi, I am trying to do so but my webpage shows me blank and the error log also does not show any error. But my script is not executed :( ..... Any idea what might be going wrong?
I mean I am trying pure Python, not using ASP
What platform (os, web server) are you using?
I am using Apache on Mac OS (snowleapord) with Python 25 and mod_python 3.3.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.