mngeek206
mngeek206

Reputation: 5157

Python + LigHTTPd + CGI: How to configure for executing single script for entire subpath

I am writing a set of scripts for Python that I intend to use on the Web. Currently, I have a handful of test scripts working properly that use import cgi.

I want the behavior of FastCGI/WSGI basically. In this instance, I can pass all requests under a certain URL to a script. For example, suppose a user accesses the URL http://www.example.com/script/run/1?param=true. For any request under /script I want a given script, say /opt/mypython/webscript.py, which uses import cgi, to run and receive both the URL and the GET parameters. In this case, I would want, from within the script, to be able to see a parameter containing /run/1 and the dictionary of GET parameters.

For security considerations, ideally we would like to have the script outside the document root.

I also would like to be able to use the x-sendfile header from within my script. I know the FastCGI module supports this, but not sure if the standard CGI one does.

I would be willing to move to FastCGI, but I don't want to have to refactor any of my code. Also, I want to use standard CGI during development, since the only FastCGI implementation I looked into (flup) would cause scripts to be cached and thus necessitate a full reboot of the entire webserver any time code is changed. Also, flup requires you to work a bit differently with the CGI interface, and it would mean some major code rewrites.

flup does offer some nice conveniences; its environ variable provides a PATH_INFO parameter that specifies the subpath as I mentioned above. But again, the standard CGI module doesn't offer all of these conveniences, so flup would require code reworking, and it's very ineffective during development when code will be changing frequently.

So the ideal solution would be to accomplish this both using standard CGI, and also with FastCGI without requiring code reworking. So during development I could use standard CGI, with its accompanying performance hit (the fact that the python interpreter has to startup each time), and then when the code is ready for production use easily migrate it to a FastCGI based backend so that it can run at maximum performance.

I'm using the current build of lighttpd. What would I need to do to configure lighttpd for this, if it's even possible?

Upvotes: 2

Views: 2018

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

In your lighttpd configuration, create an alias which maps http://host/script to the path to your CGI script:

alias.url += ( "/script" => "/opt/mypython/webscript.py" )

Enable CGI processing for that script:

$HTTP["url"] =~ "^/script" {
    cgi.assign = ( "" => "" )
}

Create a CGI script that examines the query string and PATH_INFO environment variable:

import cgi, os, sys

print 'Content-Type: text/plain\n'

if 'PATH_INFO' in os.environ:
    print os.environ['PATH_INFO']
form = cgi.FieldStorage()
if 'param' in form:
    print form['param'].value

Finally, invoke the script via a web browser:

http://localhost/script/run/1?param=true

Result:

/run/1
true

Upvotes: 5

Related Questions