トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

E71:MWSチュートリアル(2) のバックアップ(No.3)


E71:MWSチュートリアル(2)

引き続き、Mobile Web Server : How to Develop Contentを参考に、多少手を加えつつ進めていきます。

前回の内容はE71:MWSチュートリアルを参照のこと

さてさて、今回はより高度な動的ページの作成とシャレ込みます!

ちなみに、Mobile Web Server : How to Develop Contentだと、「4.1 Basic HTML page with Python(P13)」あたりからの内容です。

Basic HTML page with Python!!!

ここでは、mod_pythonの機能でももっともベーシックで押さえどこ的な機能を使ってみます。

もちろん、これはNWSじゃなきゃ動かないわけではないです。(と思います笑)

前置きはこの辺にして、今回のデモにあたり新たに作成するファイル、ディレクトリを以下に示します。

E━data━Web server┳Data
                   ┣conf
                   ┣htdocs┳Framework
                   ┃      ┣Web_Applications
                   ┃      ┣rest
                   ┃      ┣demo
                   ┃      ┗demo2 ← 今回作成するディレクトリ
                   ┃        ┣indexbasic.py
                   ┃        ┣plain.psp
                   ┃        ┗.htaccess
                  ┗logs
  • .htaccess お約束のapacheの設定ファイル
  • indexbasic.py mod_pythonから実行されるHandlerが入ってるpythonスクリプト
  • plain.psp 動的ページを生成するpythonスクリプト。jsp見たいな感じかな〜

では、各ソースコードの内容を以下に示します。今回は前回よりもちゃんと解説を入れようかな〜

.htaccess

まずは.htaccessから。ここから説明した方が、仕組みがわかりやすいから。

# setting Handler 
SetHandler mod_python ←(1)
# Add Python Handler
PythonHandler indexbasic ←(2)
# Debug On
PythonDebug On ←(3)
# Option OFF
Options None ←(4)
  1. apacheのハンドラーとしてmod_pythonを指定します。
  2. mod_pythonには様々なhandlerモジュールがあるのですが、indexbasicというファイル名のPythonHandler?

■indexbasic.py

# These modules must be import for the script to work in the server
from mod_python import apache, psp  ←(1)

# template directory
TMPL_DIR = "templates"

MAIN_TMPL = "plain.psp"

def handler(req): ←(2)
    """
        Called by mod_python for every request
         Initializes and calls HRHandler
    """
    # An instance of HRHandler is initialized
    hrh = HRHandler()
    result = hrh.handler(req)
    
    # HRHandler id deleted
    del hrh
    # The status code is returned to the server
    return result 

class HRHandler(object): ← (3)
    """
        HRHandler is the main class of this script
    """
    
    # Instance initialization
    def __init__(self):     ←(4)
        pass    # Nothing to need initialization
    
    def handler(self, requ): ←(5)
        """
            Handle the request and run the template
            return apache.OK if no errors
        """
        
        # Page title  ←(6)
        title = 'Example Page'
        
        # Page content ←(7)
        content = 'This is the Example page content'
        
        # Give values to variables used in the plain.html template ←(8)
        requ.content_type = 'text/html, charset=UTF-8'
        requ.html_head = '' # Nothing in the html files head
        requ.html_body_attr = '' # No attributes for the html body
        
        # from the main template filename
        # fname = TMPL_DIR + '¥¥' + MAIN_TMPL
        fname = MAIN_TMPL
        
        # run the main template with the given data ←(9)
        template = psp.PSP(requ, filename=fname)
        template.run( { 'content':content, 'html_head':requ.html_head, 'html_body_attr':requ.html_body_attr, 'title':title } )
        
        # delete the main template to save memry
        del template
        return apache.OK
  1. apachepspモジュールをインポートします。お約束
  2. handler関数を定義します。