I have a Python script which accepts a XML file as input and then processes it a开发者_如何学JAVAnd 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.
There is a "minimal http-upload cgi-script"-recipe, which can be used for that.
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.
精彩评论