views:

395

answers:

1

In bash, what is the simplest way to configure lighttpd to call a local python script while passing any query string or name-value pairs included with the URL as a command line option for the local python app to parse?

Example:
www.myapp.com/sendtopython/app1.py?Foo=Bar
results in the following occurring on the system. 
>python app1.py Foo=Bar

www.myapp.com/sendtopython/app2.py?-h
results in the following occurring on the system. 
>python app2.py –h

Here is an example lighttpd install and config script.

#!/bin/bash
# Install and configure web console managed by lighttpd
# Suggested Amazon EC2 AMI : ami-0d729464
#
# The console installed into /opt/web-console and 
# available on the http://_the_server_dns_/web-console

set -e -x
export DEBIAN_FRONTEND=noninteractive

function die()
{
    echo -e "$@" >> /dev/console
    exit 1
}

apt-get update && apt-get upgrade -y
apt-get -y install python
apt-get -y install unzip
apt-get -y install lighttpd

# web directory defaults to /var/www. 
WEBDIR=/var/www/logs
mkdir $WEBDIR || die "Cannot create log directory."

PYTHON=`which python`
echo $?
if [ ! $? ]
then
echo "Python interpreter not installed or not found in system path!!!" >> /dev/console
echo "Exiting setup-instance..."
exit 1
fi

#Download web-console 
FILE_DOWNLOAD_URL=http://downloads.sourceforge.net/web-console/web-console_v0.2.5_beta.zip
wget $FILE_DOWNLOAD_URL -O web-console.zip || die "Error downloading file web-console.zip"

# Install the web-console
INSTALL_DIR=/opt/web-console

mkdir $INSTALL_DIR
unzip -u -d $INSTALL_DIR web-console.zip || die "Error extracting web-console.zip"
chown www-data:www-data $INSTALL_DIR

# Configure lighttpd
cat > $INSTALL_DIR/webconsole.conf <<EOF
server.modules  += ( "mod_cgi" )
alias.url       += ( "/web-console/wc.pl" => "/opt/web-console/wc.pl" )
alias.url       += ( "/web-console/" => "/opt/web-console/wc.pl" )
\$HTTP["url"] =~ "^/web-console/" {
        cgi.assign = ( ".pl" => "/usr/bin/perl" )
}
EOF

ln -s $INSTALL_DIR/webconsole.conf /etc/lighttpd/conf-enabled/
/etc/init.d/lighttpd force-reload

exit 0
+2  A: 

Mh, for one thing I wouldn't mess with the install script, but run it once and then edit the resulting lighttpd configuration file (webconsole.conf in your case).

You then need to register Python scripts for CGI, like is done for Perl in the install script. You could add a line

cgi.assign = ( ".py" => "/usr/bin/python" )

under the corresponding .pl line which would make Python another CGI option for the /web-console/ path (look up the lighttpd docs if you want to register .py as CGI in any path).

Then, your Python CGI script app1.py, app2.py, ... have to comply to the CGI spec, which if I recall correclty passes URL parameters as environment variables. So you cannot simply use sys.argv. I'm sure there is a Python module that does the parameter extraction for you. (In Perl, Lincoln Stein's CGI module is capable of both env and command line args, but I'm not sure about Python's).

ThomasH