E71:MWSチュートリアル(2) のバックアップ(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- E71:MWSチュートリアル(2) へ行く。
- 1 (2009-04-07 (火) 23:49:49)
- 2 (2009-04-08 (水) 00:13:22)
- 3 (2009-04-08 (水) 00:13:22)
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
- 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