要在Windows平台的Apache中使用Python,当然必须得先安装Apache和Python。Apache我使用的是XAMPP,而Python则随便一搜,就可以找到下载链接。由于这个解决方案要通过安装Apache模块mod_python来实现,而mod_python的当前版本3.3.1只支持Apache 2.2和Python 2.5,所以不得不先缷载已经装好的Python 3.0,重新下载安装了Python 2.5。mod_python是一个Apache模块,它可以将Python解释器嵌入到Apache服务器中(详情可以看这里)。

让Apache支持Python的过程很简单,只要3步。

  1. 下载mod_python模块安装程序(注意文件名后面Python和Apache的版本号要与自己已经安装的版本一致;文件名前面的版本号则是mod_python的,文件名示例:mod_python-3.3.1.win32-py2.5-Apache2.2.exe),然后安装,安装向导会自动找到Python路径,但可能需要我们手工指定Apache路径,安装到最后,向导还会提示你如何修改Apache配置文件(参见下一步)并给出了后续步骤的英文说明
  2. 让Apache加载mod_python模块。在Apache安装目录下找到其配置文件apache\conf\httpd.conf,打开,搜“LoadModule”,找到加载模块的地方,然后添加一条语句:LoadModule python_module modules/mod_python.so,重新启动Apache。
  3. 在htdocs目录下新建一个目录,如:“py”。进入py目录,新建一个文本文件,并命名为“.htaccess”,加入下列3条指令:
    
    AddHandler mod_python .py
    PythonHandler mptest
    PythonDebug On
    

    这里第一条指令是将所有URL末尾为.py的请求转发给mod_python处理程序,mod_python接收到请求之后再寻找适当的PythonHandler处理程序。第二条指令只定义了一个mptest处理程序。最后一条是启用Python代码调试功能,以便在代码运行出错时输出Python解释器返回的错误。

完成以上3步之后,就可以编写Python文件并进行测试了。在py目录下新建 mptest.py 文件,打开后添加如下代码:


from mod_python import apache

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

保存。打开浏览器,输入http://localhost/py/mptest.py,回车。看到“Hello World!”了吗?

实际上,由于前面只明确将mptest设置为处理程序,所以无论浏览器URL中的.py文件名是什么(如:login.py、default.py),都将被转发给mptest.py文件来处理,都会返回“Hello World!”。怎么办呢?长话短说,可以将上面第3步中的代码替换成如下所示:


AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

更多内容,参见Mod_python ManualIntroducing mod_python



朋友们的留言

  1. dismory | 08月 25th, 2010 at 20:29

    PythonHandler从具体的mptest改成mod_python.publisher后
    如果还要测试, 要把mptest.py中追加index(req)方法(或者直接把handler改成index即可), 否则缺少index(req)会返回404错误

    Reply to this comment
  2. 为之漫笔 | 08月 26th, 2010 at 00:06

    @dismory 多谢指出。

    Reply to this comment
  3. 上海家具 | 08月 31st, 2010 at 22:11

    每次过来都能发现新东西。。。吼吼。。。

    Reply to this comment
  4. ugg boots | 12月 11th, 2010 at 13:19

    小东西啊! 不过看了感觉还不是很懂的啊!

    Reply to this comment
  5. mobile crusher | 01月 6th, 2011 at 16:54

    博文很好,但更新速度有些慢哦

    Reply to this comment
  6. enky | 01月 12th, 2011 at 19:17

    说实话,讲的很差,

    Reply to this comment
  7. nhl jerseys wholesaler | 02月 13th, 2011 at 23:55

    Apache下跑Python,真没玩过的,我搜看看的

    Reply to this comment
  8. 111 | 02月 16th, 2011 at 17:57

    可以在留言中使用以下标签 :

    Reply to this comment
  9. 11111 | 02月 16th, 2011 at 17:58

    可以在留言中使用以下标签 :

    Reply to this comment
  10. 独孤逸辰 | 03月 2nd, 2011 at 18:14

    为啥我的.py直接就显示源码了?

    Reply to this comment
  11. www.smecms.com | 03月 10th, 2011 at 11:10

    跟楼上的一样,我这也是显示源码

    Reply to this comment
  12. air jordan shoes | 03月 14th, 2011 at 16:28

    楼主辛苦了,谢谢分享。。。。。。

    Reply to this comment
  13. scarbean | 06月 20th, 2011 at 21:01

    为啥我的.py直接就显示源码了?

    Reply to this comment
  14. Cheb Hasni | 09月 15th, 2011 at 23:02

    I really enjoy looking at on this site, it contains excellent posts . “Beauty in things exist in the mind which contemplates them.” by David Hume.

    Reply to this comment
  15. motels in hershey pa | 09月 16th, 2011 at 00:14

    Awesome job on this post.

    Reply to this comment
  16. effective presentation skills | 09月 16th, 2011 at 00:59

    After examining a couple of of the weblog posts on your web site now, and I actually like your means of blogging. I bookmarked it to my bookmark web site listing and might be checking back soon. Pls check out my site as effectively and let me know what you think.

    Reply to this comment
  17. zildjian zbt | 09月 16th, 2011 at 01:01

    Thanks for the great info.

    Reply to this comment
  18. patio gazebos | 09月 16th, 2011 at 01:03

    I like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon.

    Reply to this comment
  19. lancaster insurance | 09月 16th, 2011 at 01:31

    That was clever. I’ll be stopping back.

    Reply to this comment
  20. lace tablecloth | 09月 16th, 2011 at 01:34

    There are actually quite a lot of details like that to take into consideration. That is a great point to convey.

    Reply to this comment
  21. computer networking denver | 09月 16th, 2011 at 01:35

    you have got an amazing blog here! would you wish to make some invite posts on my weblog?

    Reply to this comment
  22. cabin | 09月 16th, 2011 at 01:50

    One of my favorite posts.

    Reply to this comment
  23. lyophilized | 09月 16th, 2011 at 01:51

    This really answered my problem, thank you!

    Reply to this comment
  24. pc repair columbus ohio | 09月 16th, 2011 at 01:52

    Can I just say what a relief to find someone who actually knows what theyre talking about on the internet.

    Reply to this comment
  25. restaurants lancaster pa | 09月 16th, 2011 at 01:58

    Thank you for all of the effort on this blog

    Reply to this comment
  26. furniture maker | 09月 16th, 2011 at 02:02

    you have a great blog here! would you like to make some invite posts on my blog?

    Reply to this comment
  27. amish country lancaster pa | 09月 16th, 2011 at 02:06

    One of my favorite posts.

    Reply to this comment
  28. vw york | 09月 16th, 2011 at 02:08

    After studying a couple of of the blog posts on your website now, and I truly like your means of blogging. I bookmarked it to my bookmark web site list and might be checking again soon. Pls take a look at my website as nicely and let me know what you think.

    Reply to this comment
  29. indianapolis mortgage companies | 09月 16th, 2011 at 02:26

    An attention-grabbing dialogue is worth a comment. I feel that it is best to write extra on this topic, it might not be a taboo subject however usually individuals are not sufficient to speak on such topics. To the next. Cheers

    Reply to this comment
  30. lancaster pa homes | 09月 16th, 2011 at 02:41

    One of my favorite posts.

    Reply to this comment
  31. cloud computing providers | 09月 16th, 2011 at 02:43

    Thanks for spending the time to discuss this, I really feel strongly about it and love reading more on this topic. If possible, as you turn into an expert, would you mind updating your weblog with more details?

    Reply to this comment
  32. Norman Dyche | 09月 16th, 2011 at 02:44

    Hello, Nice job, I’ve bookmarked this page and have a feeling I’ll be returning to it regularly.

    Reply to this comment
  33. new movers list | 09月 16th, 2011 at 02:45

    Nice post. I learn something on totally different blogs everyday. It is going to all the time be stimulating to read content from different writers and observe somewhat something from their blog.

    Reply to this comment
  34. bridesmaid dresses | 10月 24th, 2011 at 11:00

    Article is very interesting,thanks for your sharing. I will necessarily add it in the selected works and I will visit this site.

    Reply to this comment
  35. nike high dunks | 10月 29th, 2011 at 09:08

    Some sandals can also double as slippers or tods shoes sale, tods shoes sale.Some trainers may be used as running shoes or as tennis shoes;

    Reply to this comment
  36. Office 2010 | 11月 9th, 2011 at 17:27

    You may be more happy than pinces,if you will be more virtuous.

    更加合适的国际化

    Reply to this comment

我来说两句儿

可以在留言中使用以下标签 :<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree