您的位置:首页 > 运维架构 > Apache

Apache运行python cgi程序

2016-08-15 16:53 274 查看

Apache运行python cgi程序

环境

win10 x64 专业版

Apache2.4

python 2.7

Apache安装和配置

Apache服务器的安装请自行搜索。在Apache2.4中默认加载了cgi模块在httpd.conf的103行左右

LoadModule cgi_module modules/mod_cgi.so

在httpd.conf的389行附近检查cgi文件目录的访问属性,默认不需要修改:

<Directory "${SRVROOT}/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>

在httpd.conf的末尾加上如下配置,在Apache2.4的配置文件中没有这行配置,在其他版本

中可能存在该配置,请使用文件的全文搜索查看。

ScriptInterpreterSource Registry

缺少这行配置会出现500的访问错误,如下图:



python的CGI程序

python 的CGI程序就是一个python脚本文件,请参考 Python CGI

CGI程序代码:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<title>Hello</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

验证CGI程序

在浏览器中输入 http://localhost:8080/cgi-bin/hello.py 浏览得到如下结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: