In bash, was ist der einfachste Weg, lighttpd zum Konfigurieren eines lokalen Python-Skript auf einer bestimmten URL basierend anrufen?
Frage
In der bash, was ist der einfachste Weg, lighttpd zu konfigurieren, dass eine lokale Python-Skript aufrufen, während jede Abfrage String übergeben oder Name-Wert-Paare mit der URL enthalten als Befehlszeilenoption für die lokale Python App zu analysieren?
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
Hier ist ein Beispiel lighttpd installieren und config-Skript.
#!/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
Lösung
Mh, für eine Sache würde ich nicht verwirren mit dem Skript installieren, aber es einmal ausgeführt und dann bearbeiten Sie die resultierende lighttpd-Konfigurationsdatei (webconsole.conf in Ihrem Fall).
Sie müssen dann Python-Skripte für CGI registrieren, wie für Perl im Installationsskript ausgeführt wird. Sie könnten eine Zeile hinzufügen
cgi.assign = ( ".py" => "/usr/bin/python" )
unter der .pl Linie entsprechen, die für die / Web-Konsole / path Python eine andere CGI-Option machen würde (die lighttpd docs nachschlagen, wenn Sie als CGI registrieren Py wollen in jeder Pfad) .
Dann Python CGI-Skript app1.py, app2.py, ... müssen entspricht auf die CGI-Spezifikation, die, wenn ich Correclty erinnere URL-Parameter als Umgebungsvariablen durchläuft. So können Sie nicht einfach sys.argv verwenden. Ich bin sicher, dass es ein Python-Modul, das die Parameterextraktion für Sie tut. (In Perl, Lincoln Stein CGI-Modul ist in der Lage sowohl env und Kommandozeilen-args, aber ich bin mir nicht sicher über Python).