tags:

views:

106

answers:

4

Hi, 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

A: 

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.

che
I tried posting using HTTP POST.I created a simple form like this:<html><body><form enctype="multipart/form-data" action="iindex.py" method="POST"><p>File: <input type="file" name="ssfilename"></p><p><input type="submit" value="Upload"></p></form></body></html>Then I created the python script:#! /usr/bin/env pythonimport sys;import cgiimport cgitb; cgitb.enable()from mod_python import apacheform = cgi.FieldStorage(keep_blank_values=1)def handler(req): fileitem = form1['ssfilename']...This is the line where I get error:>>> NameError: name 'form1' is not defined
ssdesign
This particular error is pretty simple, since you're assigning to variable `form`, but reading from variable `form1`.
che
A: 

There is a "minimal http-upload cgi-script"-recipe, which can be used for that.

Joschua
A: 

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.

gauteh
A: 

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.

Mike DeSimone