Reputation: 2821
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
Upvotes: 0
Views: 1693
Reputation: 1483
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.
Upvotes: 0
Reputation: 36203
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:
Option +ExecCGI
for the folder you want. Again, see the docs I linked to.Is that what you were looking for with your question, or did you want to actually invoke the python script with os.system()
?
Upvotes: 0
Reputation: 68902
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.
Upvotes: 1
Reputation: 2609
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
Upvotes: 0
Reputation: 1497
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.
Upvotes: 0