一个简单的cgi编程例子

作者: 背着龟壳的猪 | 来源:发表于2015-01-11 18:47 被阅读1668次

工作环境

  • 操作系统: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 ,即可查看相关结果


后续

进一步学习请点w3cSchool

相关文章

网友评论

    本文标题:一个简单的cgi编程例子

    本文链接:https://www.haomeiwen.com/subject/kpcqxttx.html