简体   繁体   中英

Running a Python program on a web server

I have a Python script which accepts a XML file as input and then processes it and creates another file.

Now the way I have to run this program in terminal (mac) is:

ttx myfile.xml

And it does the job.

Now I am trying to install this program on a web server. I have all the necessary files installed as Modules under my Python installation.

My problem is, How can I pass a file to this Python script on a web server? Should I be using File Upload method? or urllib2? or something else?

Thanks a lot

Passing the data would be easiest with HTTP POST . As to integrating Python script w/ Apache, the way I know would be to create a simple Django app to wrap the main Python function in your script, but I believe there must be some more direct way.

有一个“ 最小的http-upload cgi-script ”-配方,可用于此目的。

You could create a simple CGI script: http://wiki.python.org/moin/CgiScripts using the python cgi module.

Either as a wrapper around your python script, or update the existing one - put a text area box for pasting the contents or otherwise you need to upload the file and pass it on to your program.

Then print the content header and the results to stdout; and they should show up in the browser.

The best interface from Python to a web server is probably WSGI . It's what Django recommends for its interface to Apache. WSGI is defined in PEP 333 .

WSGI works by passing Apache's requests to your Python code as function calls. An example from the PEP uses the following Python code for a simple application:

def simple_app(environ, start_response):
    """Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello world!\n']

On apache, install mod_wsgi (available as a package in several distros), and then put this somewhere in your Apache configuration:

WSGIScriptAlias /myapp /usr/local/www/wsgi-scripts/myapp.wsgi
<Directory /usr/local/www/wsgi-scripts>
    Order allow, deny
    Allow from all
</Directory>

One of the really nice things about WSGI is that your Python code doesn't have to live in the document tree, and therefore isn't available for download and inspection.

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