简体   繁体   中英

Executing Python program on web

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

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.

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.

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

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() ?

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, eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM