一个简单的cgi编程例子

工作环境

  • 操作系统:Ubuntu 12.04
  • Web服务器:Apache
  • 开发语言:Python

准备工作

安装Apache

sudo apt-get install apache2

配置Apache

修改Apache配置文件(/etc/apache2/httpd.conf),添加以下内容

<Directory "/var/www/bin-cgi/">  
   AllowOverride None  
   Options ExecCGI  
   Order allow,deny  
   Allow from all  
</Directory>  
AddHandler cgi-script .py

重启Apache

sudo /etc/init.d/apache2 restart
访问http://localhost/ ,看看Apache是否成功开启


Hello World

新建路径

按照之前Apache的配置,在/var/www/下新建bin-cgi文件夹

cgi代码

在建好的文件夹下新建文件helloworld.py,里面内容如下

#!/usr/bin/python   
print "Content-type:text/html\r\n\r\n"  
print '<html>'  
print '<head>'  
print '<title>Hello World - First CGI Program</title>'  
print '</head>'  
print '<body>'  
print '<h2>Hello World! This is my first CGI program</h2>'  
print '</body>'  
print '</html>'

修改文件权限

sudo chmod 647 helloworld.py

测试

用浏览器访问http://localhost/bin-cgi/pythoncgi.py ,即可查看相关结果



文/背着龟壳的猪(简书作者)
原文链接:http://www.jianshu.com/p/8b0036f194e7
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值