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

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

*E71:MWSチュートリアル(2) [#v3e8ed1a]

引き続き、''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!!! [#pa2d09a0]

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

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

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

 E━data━Web server┳Data
                    ┣conf
                    ┣htdocs┳Framework
                    ┃      ┣Web_Applications
                    ┃      ┣rest
                    ┃      ┣demo
                    ┃      ┗demo2 ← 今回作成するディレクトリ
                    ┃        ┣indexbasic.py
                    ┃        ┣plain.psp
                    ┃        ┗.htaccess
                   ┗logs

-indexbasic.py mod_pythonから実行されるHandlerが入ってるpythonスクリプト
-plain.psp 動的ページを生成するpythonスクリプト。jsp見たいな感じかな〜
-.htaccess お約束のapacheの設定ファイル

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

 # 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