在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发布于 2009-09-10 10:35:02
首先,我不会弄乱安装脚本,但是运行一次它,然后编辑得到的lighttpd配置文件(在你的例子中是webconsole.conf)。
然后,您需要为CGI注册Python脚本,就像在安装脚本中为Perl所做的那样。您可以添加一行
cgi.assign = ( ".py" => "/usr/bin/python" )在相应的CGI行下面,这将使.pl成为/web-console/路径的另一个CGI选项(如果您想在任何路径中将.py注册为CGI,请查阅lighttpd文档)。
然后,Python CGI脚本app1.py、app2.py、...必须遵守CGI规范,如果我没记错的话,该规范将URL参数作为环境变量进行传递。所以你不能简单地使用sys.argv。我确信有一个Python模块可以为您提取参数。(在Perl中,林肯·斯坦的CGI模块可以支持env和命令行参数,但我不确定Python的)。
https://stackoverflow.com/questions/1403672
复制相似问题