我的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。
在httpd.conf中增加
LoadModule python_module modules/mod_python.so
#用户配置的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>
# 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
执行 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)]
Python网络编程基础中文版 第19章 P394-398
Apache HTTP Server 2.2 中文参考手册 http://lamp.haloso.net/Apache/ApacheMenu/mod/directives.html