Mod_Python在 Apache 2.2.4下的配置

一、httpd.conf配置

我的Python程序是存放在 D:/Work/pyweb下的。

OS: Windows 2003 中文版

Web服务器: Apache 2.2.4

Python环境: Ptyhon 2.5.1、、mod_python 3.3.1

我们首先需要安装python 、 apache 、mod_python

分别可以在http://www.python.org http://www.apache.org http://www.modpython.org 下载到最新的版本。

根据提示安装完成之后我们可以配置httpd.conf。

a.配置LoadModule

在httpd.conf中增加

LoadModule python_module modules/mod_python.so

b.配置目录映射和访问

#用户配置的mod_python访问目录
#映射目录D:/work/pyweb在apache中为 http://localhost/py/
Alias /py D:/work/pyweb
<Directory "D:/work/pyweb">
    Allow from all
#    设定.prog扩展名的文件时使用mod_python处理PythonHandler test
#    AddHandler mod_python .prog
#    设定使用任意文件名时使用mod_python处理PythonHandler test
    SetHandler mod_python
#    调用PythonHandler test
    PythonHandler test
#    开启mod_python的Debug模式
    PythonDebug On
#禁止浏览器从映射的目录/py/中下载py和pyc为扩展名的文件
<FilesMatch "/.(py|pyc)">
Order allow,deny
Deny from all
</FilesMatch>
</Directory>

2. 测试程序代码

# mod_python test example - test.py
from mod_python import apache
from sys import version

def writeinfo(req,name,value):
    req.write("<DT>%s</DT><DD>%s</DD>/n" % (name,value))

def handler(req):
    req.content_type = "text/html"
    if req.header_only:
        return apache.OK

    req.write("""<HTML><HEAD><TITLE>mod_python is work</TITLE>)
    </HEAD>
    <BODY>
    <H1>mod_python is working</H1>
    You have successfully configutred mod_python on your Apache system.
    Here is some information about the environment and this request:
    <P>
    <DL>
    """)

    writeinfo(req,"Client IP",req.get_remote_host(apache.REMOTE_NOLOOKUP))
    writeinfo(req,"URI",req.uri)
    writeinfo(req,"Filename",req.filename)
    writeinfo(req,"Canonical filename",req.canonical_filename)
    writeinfo(req,"Path_info",req.path_info)
    writeinfo(req,"Python version",version)

    req.write("</DL></BODY></HTML>/n")

    return apache.OK

3.运行结果

执行 http://localhost/py/abc

mod_python is working
You have successfully configutred mod_python on your Apache system. Here is some information about the environment and this request:

Client IP
127.0.0.1
URI
/py/abc
Filename
D:/work/pyweb/abc
Canonical filename
D:/work/pyweb/abc
Path_info
Python version
2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]

4.参考资料

Python网络编程基础中文版 第19章 P394-398

Apache HTTP Server 2.2 中文参考手册 http://lamp.haloso.net/Apache/ApacheMenu/mod/directives.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值