MAC apache2.4如何执行python脚本

2018-07-04  本文已影响7人  虫yu
  1. 去掉前面的#
LoadModule cgi_module libexec/apache2/mod_cgi.so
  1. 修改CGI目录
#ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
ScriptAlias /catch/ "/Users/myweb/html/catch/"
  1. 修改原CGI目录的Directory
<Directory "/Users/myweb/html/catch">
    AllowOverride None
    Options +ExecCGI
    Order allow,deny
    Allow from all
</Directory>
  1. 添加尾缀
AddHandler cgi-script .cgi .py .pl .sh
  1. 保存重启apache
sudo apachectl restart
  1. 注意脚本解释器的声明
#!/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! 我是第一个CGI程序</h2>'
print '</body>'
print '</html>'

#!/usr/bin/python,申明这个脚本,需要的解释器的位置

/usr/bin/python
  1. 文件权限修改
cd /Users/myweb/html/catch 
chmod 755 hipy.py
  1. 网页运行
http://localhost/hipy
网页运行结果

参考资料:http://www.runoob.com/python/python-cgi.html

上一篇 下一篇

猜你喜欢

热点阅读