---
title: "利用apache配置纯Python运行环境"
author: "skyfen"
date: "2011-04-20T15:45:36Z"
tags: []
categories: ["编程语言"]
slug: "apache-python"
draft: false
lastmod: "2011-04-20T15:45:36Z"
id: 1009382
url: "https://skyfen.iteye.com/blog/1009382"
---

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

利用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/"  
&lt;Directory "E:/newtest/pyweb"&gt;  
     Options Indexes FollowSymLinks MultiViews  
     AllowOverride None  
     Order allow,deny  
     allow from all  
     AddHandler mod\_python .py  
     PythonHandler mod\_python.publisher  
     PythonDebug On  
&lt;/Directory&gt;

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/"  
&lt;Directory "E:/newtest/pyweb"&gt;  
    Options Indexes FollowSymLinks MultiViews  
    AllowOverride None  
    Order allow,deny  
    allow from all  
    AddHandler mod\_python .psp  
    PythonHandler mod\_python.psp  
    PythonDebug On  
&lt;/Directory&gt;

test.psp文字内容

&lt;html&gt;  
&lt;body&gt;  
&lt;h1&gt;&lt;% req.write("Hello!") %&gt;&lt;/h1&gt;  
&lt;/body&gt;  
&lt;/html&gt;

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

得到的结果：Hello!

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

&lt;IfModule mod\_python.c&gt;  
        AddHandler mod\_python .py .psp  
        PythonHandler mod\_python.publisher | .py  
        PythonHandler mod\_python.psp | .psp  
&lt;/IfModule&gt;

Alias /py/ "E:/newtest/pyweb/"  
&lt;Directory "E:/newtest/pyweb"&gt;  
    Options Indexes FollowSymLinks MultiViews  
    AllowOverride None  
    Order allow,deny  
    allow from all  
    PythonDebug On  
&lt;/Directory&gt;

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

Alias /py/ "E:/newtest/pyweb/"  
&lt;Directory "E:/newtest/pyweb"&gt;  
    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  
&lt;/Directory&gt;