日期:2014-05-16  浏览次数:20685 次

利用apache配置纯Python运行环境

在网上摆弄了好久,终于搞清楚了这个技术问题,和大家分享一下。

利用apache配置纯Python运行环境还是要用 mod_python模块,前提你一定要安装好mod_python。

具体怎么装,网上有很多。注意配套的版本。

?

apache的httpd.conf文件中打开 mod_python.so功能

LoadModule python_module modules/mod_python.so

?

?

httpd.conf文件中加

===================================================================
python文件的运行
===================================================================
Alias /py/ "E:/newtest/pyweb/"
<Directory "E:/newtest/pyweb">
???? Options Indexes FollowSymLinks MultiViews
???? AllowOverride None
???? Order allow,deny
???? allow from all
???? AddHandler mod_python .py
???? PythonHandler mod_python.publisher
???? PythonDebug On
</Directory>

test.py文字内容

def index(req):
??? req.content_type = 'text/plain'
??? req.write("Hello World!")
??? return apache.OK

#带一个参数???
def learning(req,name):
??? req.content_type = 'text/plain'
??? if name=="":
???????? me="Wangxiaoxiao"
??? else:
???????? me=name
??? req.write("i like english,my name is %s "%me)????
??? return apache.OK

???
运行
http://192.168.1.91/py/test.py

得到的结果:Hello? World!

这个例子证明了任何py文件都可以运行先从默认方法index运行起


如果你要运行learning方法
URL方法是:
http://192.168.1.91/py/test.py/learning?name=zhangsan
运行结果:
I love english,my name is zhangsan

如果你URL是
http://192.168.1.91/py/test.py/learning
会报错。name参数没传值




======================================================================
python模板文件psp的运行
======================================================================

Alias /py/ "E:/newtest/pyweb/"
<Directory "E:/newtest/pyweb">
??? Options Indexes FollowSymLinks MultiViews
??? AllowOverride None
??? Order allow,deny
??? allow from all
??? AddHandler mod_python .psp
??? PythonHandler mod_python.psp
??? PythonDebug On
</Directory>


test.psp文字内容

<html>
<body>
<h1><% req.write("Hello!") %></h1>
</body>
</html>

运行
http://192.168.1.91/py/test.psp

得到的结果:Hello!



=================================================================
即执行py文件,又执行psp模板文件???????? 第一种方法
=================================================================

<IfModule mod_python.c>
??????? AddHandler mod_python .py .psp
??????? PythonHandler mod_python.publisher | .py
??????? PythonHandler mod_python.psp | .psp
</IfModule>

Alias /py/ "E:/newtest/pyweb/"
<Directory "E:/newtest/pyweb">
??? Options Indexes FollowSymLinks MultiViews
??? AllowOverride None
??? Order allow,deny
??? allow from all
??? PythonDebug On
</Directory>




=================================================================
即执行py文件,又执行psp模板文件???????? 第二种方法
=================================================================

Alias /py/ "E:/newtest/pyweb/"
<Directory "E:/newtest/pyweb">
??? Options Indexes FollowSymLinks MultiViews
??? AllowOverride None
??? Order allow,deny
??? allow from all
??? AddHandler mod_python .py .psp
??? PythonHandler mod_python.publisher | .py
??? PythonHandler mod_python.psp | .psp
??? PythonDebug On
</Directory>