0

在 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
4

1 回答 1

3

嗯,一方面我不会弄乱安装脚本,而是运行一次,然后编辑生成的 lighttpd 配置文件(在您的情况下为 webconsole.conf)。

然后,您需要为 CGI 注册 Python 脚本,就像在安装脚本中为 Perl 所做的那样。你可以加一行

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

在相应的 .pl 行下,这将使 Python 成为 /web-console/ 路径的另一个 CGI 选项(如果您想在任何路径中将 .py 注册为 CGI,请查找 lighttpd 文档)。

然后,您的 Python CGI 脚本 app1.py、app2.py... 必须符合CGI 规范,如果我记得正确的话,它会将 URL 参数作为环境变量传递。所以你不能简单地使用 sys.argv。我确信有一个 Python 模块可以为您提取参数。(在 Perl 中,Lincoln Stein 的 CGI 模块同时支持 env 和命令行参数,但我不确定 Python 的)。

于 2009-09-10T10:35:02.197 回答