初试CGI编程--python篇


环境说明

system:

Linux luogw-pc 3.5.0-48-generic #72~precise1-Ubuntu SMP Tue Mar 11 20:09:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux


apache: (apt-get 安装方式)
Server version: Apache/2.2.22 (Ubuntu)
Server built:   Apr 17 2014 21:49:25

第一步:确认apache2服务器是否开启了CGI

查看相关资源发现默认开启着

/etc/apache2/sites-enabled/000-default 有如下内容:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

意味着把CGI程度放在/ usr/lib/cgi-bin/目录下即可,然后通过(本地)http://localhost/cgi-bin/脚本名的url去访问
注:在/usr/lin/cgi-bin下看到一个gsearch.cgi
写个demo试试 test.pl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";

oh no , 没看到熟悉的Hello, World


本能反应,脚本没有执行权限?,do it (sudo chmod +x test.pl)
try again, ok got it


第二步,尝试python版的Hello, World

test.py

#! /usr/bin/python
print "Content-type: text/html\n\n"
print "python : "
print "Hello, World."

run it 



调试:

查看 /var/log/apache2下的log文件: 如 error.log 


总结

1.ubuntu 12.04 下通过apt-get install 安装的apache2默认是开启了CGI,CGI脚本存放的路径是 /usr/lib/cgi-bin/
不需要自定义配置,可以开始简单的学习
2.访问脚本的url 形式是主机域名或IP/cgi-bin/脚本名称
3.脚本需要可执行权限


参考资料

http://apache.jz123.cn/howto/cgi.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值