[python]mod_pythonのpublisherとpspを使う
前回はPythonHandlerをファイル指定で使っていましたが、mod_pythonには汎用的なPythonHandlerの指定方法もあるようです。mod_python.publisherというものらしい。publisherを使うと、URLを解析して以下のようにエントリポイントが変わった(私の環境の場合)。
- http://test.localpy/ ... 403 Forbidden
- http://test.localpy/index ... 404 Not Found
- http://test.localpy/index/ ... 404 Not Found
- http://test.localpy/index.py ... /document-root/index.py内のdef index(req):関数が実行された
- http://test.localpy/index.py/ ... /document-root/index.py内のdef index(req):関数が実行された
- http://test.localpy/index.py/index ... /document-root/index.py内のdef index(req):関数が実行された
- http://test.localpy/index.py/hello ... /document-root/index.py内のdef hello(req):関数が実行された
- http://test.localpy/index/hello ... 404 Not Found
- http://test.localpy/other.py ... /document-root/other.py内のdef index(req):関数が実行された
- http://test.localpy/other.py/index ... /document-root/other.py内のdef index(req):関数が実行された
- http://test.localpy/other.py/world ... /document-root/other.py内のdef world(req):関数が実行された
- http://test.localpy/dir/another.py ... /document-root/dir/another.py内のdef index(req):関数が実行された
- http://test.localpy/dir/another.py/index ... /document-root/dir/another.py内のdef index(req):関数が実行された
- http://test.localpy/dir/another.py/asdf ... /document-root/dir/another.py内のdef asdf(req):関数が実行された
参考になるリンクを示しておきます。
お次はPSP。Play Station Portable という意味ではありません(Python for PSPというものが確かにあるらしいですが)。PSPはPython Server Pagesの略です。JSPとかASPの真似っ子ですね。ようはphpみたいにHTML内にPythonが書ける仕組みをmod_pythonが提供しています。このPSPという仕組みは、私がグーグルでいろいろ調べている感じだとあんまり流行ってなさそうな雰囲気です。じゃぁみんなPythonのWebは動やっているんだろう。。。PSPは、mod_wsgiとかではたぶん提供していないと思います。
PSPのサンプルコードとして、Python Server Pages! 略してPSP - Fioの素敵な日々という記事を参考にしました。コードをそのまま拝借して(charsetくらいはUTF-8にして)test.pspを作り、そのまま、http://test.localpy/test.psp?name=uechocoにアクセスすると、以下のようなHTML表示がされます。

これならphpっぽくWebが作れそうですね。といってもmod_pythonに依存しすぎていますが。
ちなみに、今回使ったhttpd.confのVirtualHostは以下のものです。
-
<VirtualHost *:80>
-
ServerName test.localpy
-
DocumentRoot "/Users/dev/py_works/test.localpy"
-
<Directory "/Users/dev/py_works/test.localpy">
-
Allow from all
-
AddHandler mod_python .py .psp
-
PythonHandler mod_python.publisher | .py
-
PythonHandler mod_python.psp | .psp
-
PythonDebug On
-
</Directory>
-
ErrorLog "logs/test.localpy-error_log"
-
CustomLog "logs/test.localpy-access_log" common
-
</VirtualHost>