Python:CGI本地配置方法 Mac Apache

CGI本地配置 Mac,记录一下,网上的看了好多,不怎么好理解,通俗易懂的走个流程吧。

Mac环境下 Apache是支持CGI的,本人理解的意思就是说:平时我们打开Python文件,在浏览器上显示的是py文件内容,通过配置,就可以让py文件在Apache上运行,然后我们访问的时候就是程序的反馈信息。

开始配置流程:

1.找到要修改的文件(在find中,直接command+shift+G,然后粘贴路径,查找,就找到要修改的文件了)

/etc/apache2/httpd.conf   

2.修改httpd.conf,我是直接用sublime text修改的,直接将文件拖到sublime中,如下图所示:

(1)command+F 搜索mod_cgi.so,然后将前面的#删除

LoadModule cgi_module libexec/apache2/mod_cgi.so

(2)command+F 搜索AddHandler,然后将这一行,用下面这行替换

AddHandler cgi-script .cgi .pl .py .sh

(3)command+F 搜索ScriptAlias,然后添加下面一行(一般这个默认都是注释的,我们直接添加一句,也可以把他删除了)

ScriptAlias   /Documents/  /Library/WebServer/Documents/ 

添加后就是下面这样了,注意最下面第二行就是我添加的

<IfModule alias_module>
    #
    # Redirect: Allows you to tell clients about documents that used to 
    # exist in your server's namespace, but do not anymore. The client 
    # will make a new request for the document at its new location.
    # Example:
    # Redirect permanent /foo http://www.example.com/bar

    #
    # Alias: Maps web paths into filesystem paths and is used to
    # access content that does not live under the DocumentRoot.
    # Example:
    # Alias /webpath /full/filesystem/path
    #
    # If you include a trailing / on /webpath then the server will
    # require it to be present in the URL.  You will also likely
    # need to provide a <Directory> section to allow access to
    # the filesystem path.

    #
    # ScriptAlias: This controls which directories contain server scripts. 
    # ScriptAliases are essentially the same as Aliases, except that
    # documents in the target directory are treated as applications and
    # run by the server when requested rather than as documents sent to the
    # client.  The same rules about trailing "/" apply to ScriptAlias
    # directives as to Alias.
    #ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
    ScriptAlias   /Documents/  /Library/WebServer/Documents/  
</IfModule>

(4)在刚才搜索的下面有个CGI的Directory需要修改

#<Directory "/Library/WebServer/CGI-Executables">
  #  AllowOverride None
   # Options None
   # Require all granted
#</Directory>

就是这个字典,我把他直接注释了,然后用下面这个字典替换。

<Directory "/Library/WebServer/Documents">  
    AllowOverride None  
    Options +ExecCGI  
    Order allow,deny  
    Allow from all  
</Directory>  

修改完毕,保存。

4.重启Apache

sudo /usr/sbin/apachectl restart    

5.在下面路径下写一个用来运行的test.py文件

/Library/WebServer/Documents
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import cgi
 
 
def main():
    print "Content-type: text/html;charset=utf-8\n\n"
    print "你的东西配置好了,开始py"
 
 
main()

6.(注意)给test.py授权

chmod 777 
/Library/WebServer/Documents/test.py

7.搞定了,这时候在浏览器打开:

http://localhost/test.py

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值