在Windows平台的Apache中配置Python
2010年08月19日 Web开发, 编程技术
要在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步。
- 下载mod_python模块安装程序(注意文件名后面Python和Apache的版本号要与自己已经安装的版本一致;文件名前面的版本号则是mod_python的,文件名示例:mod_python-3.3.1.win32-py2.5-Apache2.2.exe),然后安装,安装向导会自动找到Python路径,但可能需要我们手工指定Apache路径,安装到最后,向导还会提示你如何修改Apache配置文件(参见下一步)并给出了后续步骤的英文说明。
- 让Apache加载mod_python模块。在Apache安装目录下找到其配置文件apache\conf\httpd.conf,打开,搜“LoadModule”,找到加载模块的地方,然后添加一条语句:LoadModule python_module modules/mod_python.so,重新启动Apache。
- 在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
为之漫笔(李松峰),本博客专注于Web前后端技术、移动平台开发技术、交互设计和技术翻译。声明一下,因为时常需要外出审稿,而且基本不带笔记本,所以有时可能会迟一点回复大家的留言。
PythonHandler从具体的mptest改成mod_python.publisher后
如果还要测试, 要把mptest.py中追加index(req)方法(或者直接把handler改成index即可), 否则缺少index(req)会返回404错误
@dismory 多谢指出。
每次过来都能发现新东西。。。吼吼。。。
小东西啊! 不过看了感觉还不是很懂的啊!
博文很好,但更新速度有些慢哦
说实话,讲的很差,
Apache下跑Python,真没玩过的,我搜看看的
可以在留言中使用以下标签 :
可以在留言中使用以下标签 :
为啥我的.py直接就显示源码了?
跟楼上的一样,我这也是显示源码
楼主辛苦了,谢谢分享。。。。。。
为啥我的.py直接就显示源码了?
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.
Awesome job on this post.
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.
Thanks for the great info.
I like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon.
That was clever. I’ll be stopping back.
There are actually quite a lot of details like that to take into consideration. That is a great point to convey.
you have got an amazing blog here! would you wish to make some invite posts on my weblog?
One of my favorite posts.
This really answered my problem, thank you!
Can I just say what a relief to find someone who actually knows what theyre talking about on the internet.
Thank you for all of the effort on this blog
you have a great blog here! would you like to make some invite posts on my blog?
One of my favorite posts.
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.
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
One of my favorite posts.
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?
Hello, Nice job, I’ve bookmarked this page and have a feeling I’ll be returning to it regularly.
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.
Article is very interesting,thanks for your sharing. I will necessarily add it in the selected works and I will visit this site.
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;
You may be more happy than pinces,if you will be more virtuous.
更加合适的国际化