Pyhton配置CGI

CGI--common gateway interface 通用网关接口的意思,本文通过python的CGI来整体了解下CGI的配置和使用

CGI配置(Mac版)

  • 安装前提要装好apache,python等必要软件,本次只针对CGI的配置
进入apache2目录
/etc/apache2
编辑httpd.conf
vi httpd.conf
  • 取消注释LoadModule cgi_module libexec/apache2/mod_cgi.so,截图如下

  • 设置/Library/WebServer/CGI-Executables目录的权限

<Directory "/Library/WebServer/CGI-Executables">
    AllowOverride None
    Options ExecCGI    
    Order allow,deny
    Allow from all
</Directory>
  • 同时注释调
#<Directory />
# AllowOverride none
# Require all denied
#</Directory>
  • 取消注释AddHandler cgi-script .cgi,同时在末尾加上.py

  • 重启apache2

sudo  apachectl restart 

添加CGI python文件测试

  • 进入刚才设置的目录/Library/WebServer/CGI-Executables
    建test.py,并授权chmod 777 test.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-

print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello World - 我的第一个 CGI 程序!</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! </h2>'
print '</body>'
print '</html>'
  • 通过浏览器访问http://localhost/cgi-bin/test.py

整个python CGI配置流程结束

posted @ 2019-04-30 11:07  Levcon  阅读(163)  评论(0编辑  收藏  举报