windows+phpstudy(apache) 以cgi方式运行python


Apache配置

在httpd.conf中查找DocumentRoot: 

+ExecCGI 支持cgi

DocumentRoot  "F:\phpStud\PHPTutorial\WWW"
<Directory />
    Options +Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

 

让apache识别py文件为cgi程序:

AddHandler cgi-script .cgi .pl .py

 

指定目录下执行cgi程序

ScriptAlias /cgi-bin/ "F:/phpStud/PHPTutorial/Apache/cgi-bin/"

 

重启 apache, from.py 文件内容

#!D:\python\python.exe  #python 运行程序位置
#coding=utf-8
print ("Content-type:text/html")
print ()                             # 空行,告诉服务器结束头部
print ('<html>')
print ('<head>')
print ('<meta charset="utf-8">')
print ('<title>Hello Word - 我的第一个 CGI 程序!</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! 我是来自菜鸟教程的第一CGI程序</h2>')
print ('</body>')
print ('</html>')

 

运行文件存放位置

F:\phpStud\PHPTutorial\Apache\cgi-bin\from.py

 

浏览器访问地址:

http://localhost/cgi-bin/from.py

 

cgi 运行文件注意要点

第一:#! 前面不能有空格,后面紧跟解释程序;

第二,python等解释程序的目录是否正确;

第三,作为http协议的要求,一定要输出http headers;

第四,在存在http headers的前提下,一定要在headers后面打印一个空行,否则服务器会报错;

第五,把错误的程序在python的idle中执行一下,验证正确性;

最后,实在搞不定的情况下,查看apache的logs文件夹下的error.log文件,来确定问题。

 

 

相关文章:windows+phpstudy(apache) 以cgi方式支持python

Windows 配置Apache+CGI


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM