blog.Ring.idv.tw

Python Server Pages - 架構一個PSP環境

Python Server Pages - 架構一個PSP環境


在Web Application的開發工具中,絕大多數耳熟能詳的Server Side Programming無非就是ASP、PHP和JSP,那... Python呢?

我們是否也能使用Python來開發Web Application呢?答案是肯定的~

通常在架設LAMP(PHP)環境時~ 在安裝好Apache之後,就是要為它加上「mod_php」這個module,這樣Apache才能委任由此module來處理PHP~

OK,換句話說~ 若是要讓Apache和Python緊密地結合在一起~ 這時候我們需要的就是「mod_python」...

至於Web Framework的選擇方案... 在Java領域中不外乎有StrutsSpring,PHP也有Zend FrameworkCakePHP~ 那Python哩?也有DjangoTurboGears~ 不過我沒用過倒是真的~ XDD

所以本文就是要建構一個「Apache」結合「Python」等於「Python Server Pages(PSP)」的環境~

環境配置

.作業環境Windows XP Home Edition

.Apache 2.2

.Python 2.5

.mod_python(請下載並安裝:mod_python-3.3.1.win32-py2.5-Apache2.2.exe)

httpd.conf設定

安裝「mod_python」之後,在「Apache」的modules目錄夾之中會產生一個「mod_python.so」~ 所以我們需要修改「httpd.conf」將這個module載入進來~

.加入此行「LoadModule python_module modules/mod_python.so」

.尋找「<Directory "/some/directory/htdocs">」(/some/directory/指的是你Apache所安裝的位置),並改寫「AllowOverride」屬性為「FileInfo」,如:

AllowOverride FileInfo

HelloWorld範例 (Template System)

現在在「htdocs」目錄下建立一個「psp」的目錄夾,並寫入下述三份文件:

.htaccess

SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On

hello.py

from mod_python import apache, psp
def handler(req):
	req.content_type = 'text/html'
	template = psp.PSP(req, filename='hello.tmpl')
	template.run({'what':'world'})

hello.tmpl

<html>
	<h1>Hello, <%=what%>!</h1>
</html>

測試它吧!

最後的步驟就是開啟你的Browser,在網址列上給它填上「http://localhost/psp/hello/handler」你就能看見「Hello, world!」~ ^^v

相關資源

modpython官網

Mod_python's PSP: Python Server Pages

Web Python Tutorial

2008-08-06 15:58:00

2 comments on "Python Server Pages - 架構一個PSP環境"

  1. 1. zzt 說:

    太強大了...(拜服)

    2009-02-25 14:22:54

  2. 2. Shen 說:

    沒有啦~ 只是個簡單的Hello World分享文而已:p

    2009-02-26 17:19:15

Leave a Comment

Copyright (C) Ching-Shen Chen. All rights reserved.

::: 搜尋 :::

::: 分類 :::

::: Ads :::

::: 最新文章 :::

::: 最新回應 :::

::: 訂閱 :::

Atom feed
Atom Comment