在bash中,配置lighttpd以根据特定URL调用本地python脚本的最简单方法是什么?
题
在bash中,配置lighttpd调用本地python脚本的最简单方法是什么,同时传递URL中包含的任何查询字符串或名称 - 值对作为本地python应用程序要解析的命令行选项?
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
以下是lighttpd安装和配置脚本的示例。
#!/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
解决方案
嗯,首先我不会搞乱安装脚本,但运行一次然后编辑生成的lighttpd配置文件(在你的情况下是webconsole.conf)。
然后,您需要为CGI注册Python脚本,就像在安装脚本中为Perl所做的那样。你可以添加一行
cgi.assign = ( ".py" => "/usr/bin/python" )
在相应的.pl行下,这将使Python成为/ web-console / path的另一个CGI选项(如果要在任何路径中将.py注册为CGI,请查找lighttpd文档)
然后,您的Python CGI脚本app1.py,app2.py,...必须遵守到CGI规范,如果我记得correclty将URL参数作为环境变量传递。所以你不能简单地使用sys.argv。我确信有一个Python模块可以为你进行参数提取。 (在Perl中,Lincoln Stein的CGI模块既可以使用env也可以使用命令行args,但我不确定Python是什么。)
不隶属于 StackOverflow