CherryPyはいくつかの特別な変数と関数を設定と利用をしています。これらはとてもシンプルで使いやすいものですがとても今日強力です。この章では、これらの特別な変数と関数が何であるかをみることにして、次の章でそれらの使い方を学ぶことにします。
http://localhost:8000/dir/page?key1=value1&key2=value2であれば以下のようになるでしょう:
request.base == 'http://localhost:8000'
and
request.path == 'dir/page'
and
request.paramMap == {'key1': 'value1', 'key2': 'value2'}
"status": 200 "content-type": "text/html" "server": "CherryPy 0.1" "date": current date "set-cookie": [] "content-length": 0次の章では、これらの値の修正方法を学ぶことにします。
| 静的コンテンツ | 全てのコンテンツ | 動的コンテンツ |
|---|---|---|
| a.リクエストが来ます | ||
| b.requestのメンバ変数全てがセットされます | ||
| c.initRequestを呼び出します(これは、request.pathとrequest.paramMapを変更するかもしれません) | ||
| d.このリクエストが静的コンテンツか、それとも動的コンテンツに一致するかを決めます(request.pathと設定ファイルのstaticContentセクションに基づきます) | ||
| e.initNonStaticRequestを呼び出します(これはrequest.pathとrequest.paramMapを変更するかもしれません) | ||
| e.静的ファイルを読み込みセットする(response.headerMapの値とresponse.bodyに応じます) | f.CherryClassインスタンスのメソッドを引数つきで呼び出し(request.pathとrequest.paramMapに基づきます)、その結果によってresponse.headerMapの値とresponse.bodyをセットする | |
| f.initResponseを呼び出す(これはresponse.headerMapとresponse.bodyを変更するかもしれません) | g.initResponseを呼び出す(これはresponse.headerMapとresponse.bodyを変更するかもしれません) | |
| g.ブラウザへレスポンスを送る(response.headerMapとresponse.bodyに基づきます) | h.ブラウザにレスポンスを送ります(response.headerMapとresponse.bodyに基づきます) |
見ての通り、クライアントに送り返す直前に、レスポンスヘッダやボディを変更するためにinitResponseとinitNonStaticResponseを使うことが可能です。
あなたがやるべきことは、initNonStaticRequestを使い、URL http://host/customerNameをhttp://host?customer=customerNameに変換するだけです。全てが各ユーザーに透過的になります。
以下のコードを入力します:
def initNonStaticRequest():
if request.path:
request.paramMap['customer']=request.path
request.path=""
CherryClass Root:
mask:
def index(self, customer=""):
<html><body>
Hello, <py-eval="customer">
</body></html>
ファイルをコンパイルし、サーバーを立ち上げ、http://localhost:8000/customer1 やhttp://localhost:8000/worldなどのいくつかのURLを試してください。
CherryClass Root:
mask:
def index(self):
<html><body>
<a href="loop">Click here to come back to this page</a>
</body></html>
view:
def loop(self):
response.headerMap['status']=302
response.headerMap['location']=request.base
return "" # A view should always return a string
やるべきことは、initNonStaticRequestを使ってスタート時間を格納し、initNonStaticResponseを使って作成時間が入っている1行を追加します。
コードは以下の通り:
import time
def initNonStaticRequest():
request.startTime=time.time()
def initNonStaticResponse():
if response.headerMap['content-type']=='text/html':
response.body+='<br>Time: %.04fs'%(time.time()-request.startTime)
CherryClass Root:
mask:
def index(self):
<html><body>
Hello, world
</body></html>
以下の例ではセットアップの仕方とエラーが起こったエラー全てにemailを送ります:
use Mail
def onError():
# Get the error in a string
import traceback, StringIO
bodyFile=StringIO.StringIO()
traceback.print_exc(file=bodyFile)
errorBody=bodyFile.getvalue()
bodyFile.close()
# Send an email with the error
myMail.sendMail("erreur@site.com", "webmaster@site.com", "", "text/plain", "An error occured on
your site", errorBody)
# Set the body of the response
response.body="<html><body><br><br><center>"
response.body+="Sorry, an error occured<br>"
response.body+="An email has been sent to the webmaster"
response.body+="</center></body></html>"
CherryClass MyMail(Mail):
function:
def __init__(self):
self.smtpServer='smtp.site.com'
CherryClass Root:
mask:
def index(self):
<html><body>
<a py-attr="request.base+'/generateError'" href="">Click here to generate an error</a>
</body></html>
def generateError(self):
<html><body>
You'll never see this: <py-eval="1/0">
</body></html>
この例ではCherryPyに備わっている標準モジュールMailの使い方も示してあります。