[python]mod_pythonのpublisherとpspを使う

カテゴリ: python / author: uechoco / 2010年08月09日 23:17:03
この記事を読む時間:552くらい

前回は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は以下のものです。

TEXT:
  1. <VirtualHost *:80>
  2.     ServerName test.localpy
  3.     DocumentRoot "/Users/dev/py_works/test.localpy"
  4.     <Directory "/Users/dev/py_works/test.localpy">
  5.         Allow from all
  6.         AddHandler mod_python .py .psp
  7.         PythonHandler mod_python.publisher | .py
  8.         PythonHandler mod_python.psp | .psp
  9.         PythonDebug On
  10.     </Directory>
  11.     ErrorLog "logs/test.localpy-error_log"
  12.     CustomLog "logs/test.localpy-access_log" common
  13. </VirtualHost>


コメントはまだありません »

コメントはまだありません。

この投稿へのコメントの RSS フィード。 TrackBack URI

コメントする

Copyright © 2012 うえちょこ@ぼろぐ. WP Theme created by Web Top.