Pythonをやってみる CGIとmod_pythonの違い

欲しい情報がどこ探しても見つからなかったので自分で検証。
PythonのHelloWorld総まとめ。
 

CGI

特徴

ファイルに実行権限を与える必要がある。
1行目は必ずヘッダーを送信する必要がある。
 

httpd.conf / .htaccess
AddHandler cgi-script .py

 

hello.py
#!/usr/local/bin/python2
# -*- coding: utf-8 -*-

def main():
	print "Content-Type: text/html; charset=utf-8"
	print
	print "Hello! CGI"

if __name__ == "__main__":
	main()

 

mod_python - handler

特徴

mod_pythonのインストールが必要。
CGIで動かすよりもめちゃくちゃ速い。(らしい)
.htaccessを置いたディレクトリ以下の*.py(存在しなくてもいい)にアクセスすると、PythonHandler+AddHandlerのファイルのhandler()が実行される。
関数名はhandler固定。
 

httpd.conf / .htaccess
LoadModule python_module modules/mod_python.so
AddHandler mod_python .py
PythonHandler hello
PythonDebug On

 

hello.py
# -*- coding: utf-8 -*-
from mod_python import apache

def handler(req):
  req.content_type = "text/html"
  req.write("Hello! Handler")
  return apache.OK

 

参考

3.3 mod_python は一体何をやっているのか
http://www.python.jp/doc/contrib/modpython/tut-what-it-do.html
 

mod_python - publisherハンドラ

特徴

mod_pythonのインストールが必要。
CGIで動かすよりもかなり速い。(らしい)
ファイル名/関数名にアクセスすると、ファイル内の関数が実行される。(ex /hello.py/test1)
PATH_INFOがない場合は、def index()が実行される。
関数の引数は、GET/POSTから取ってくる。test3の場合、?aaa=hoge&bbb=hogeとしてアクセスしないとエラーが出る。
 

httpd.conf / .htaccess
LoadModule python_module modules/mod_python.so
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

 

hello.py
# -*- coding: utf-8 -*-
from mod_python import apache

def index():
	return "Hello! index"

def test1():
	return "Hello! test1"

def test2(req):
	req.write("Hello! test2")

def test3(req, aaa, bbb):
	req.write("Hello! test3\n\n")
	req.write("aaa : "+ aaa + "\n")
	req.write("bbb : "+ bbb + "\n")

 

参考

6.1 publisher ハンドラ
http://www.python.jp/doc/contrib/modpython/hand-pub.html
 

mod_python - PSPハンドラ (Python Server Pages)

特徴

mod_pythonのインストールが必要。
JSPとかPHPみたいな感じで、ちょっとしたスクリプトをHTML内に埋め込む事が出来る。
 

httpd.conf / .htaccess
LoadModule python_module modules/mod_python.so
AddHandler mod_python .psp
PythonHandler mod_python.psp

 

time.psp
<html>
<%
import time
%>
Hello world, the time is: <%=time.strftime("%Y-%m-%d, %H:%M:%S")%>
</html>

 

mod_python - cgiハンドラ

特徴

mod_pythonのインストールが必要。
ドキュメントでは非推奨っぽいニュアンス。
CGIで動かすよりも速い。(らしい)
.htaccessの置いてあるディレクトリでindexesが無効になる。(環境による?)
__name__はファイル名から拡張子を除いたものになる。
 

httpd.conf / .htaccess
LoadModule python_module modules/mod_python.so
SetHandler mod_python
PythonHandler mod_python.cgihandler

 

hello.py
#!/usr/local/bin/python2
# -*- coding: utf-8 -*-

def main():
	print "Content-Type: text/html; charset=utf-8"
	print
	print "Hello! CGIHandler<br />"
	print "__name__ : %s" % __name__

main()

 

httpd.confの設定

実用性考えたら、使いたいのはPublisherとPSP
いちいち.htaccessで書くのも面倒。
拡張子ごとに、.pyはPublisher、.pspPSPとしたい。
探したらやりたい事にマッチする設定をしてる人発見。
まんま設定。
 

LoadModule python_module modules/mod_python.so

# python module (publisher)
AddHandler mod_python .py
PythonHandler mod_python.publisher | .py

# python server pages
AddHandler mod_python .psp
PythonHandler mod_python.psp | .psp

# debug on
PythonDebug On

 

参考

覚え書き改め肩こり日記 mod_python の設定
http://iwashita.dtiblog.com/blog-entry-16.html
 

ダイジェスト認証と古いmod_pythonを併用しちゃ駄目

見事にハマった。
yumでインストール出来るmod_python-3.2.8-3.1辺りだと、ダイジェスト認証をかけたディレクトリで「400 Bad Request」になる。
3.3.1で修正されたらしい。
tar.gzをダウンロードしてcheckinstallした。
 
mod_python download - The Apache HTTP Server Project
http://httpd.apache.org/modules/python-download.cgi
 
[#MODPYTHON-47] Digest Authorization header causes bad request error. - ASF JIRA
http://issues.apache.org/jira/browse/MODPYTHON-47
 

環境

# httpd -version
Server version: Apache/2.2.3
Server built:   Nov 12 2009 18:43:47
# python -V
Python 2.6.1
# rpm -qa | grep mod_python
mod_python-3.3.1-1