これらのテクニックはあるアプリケーションサーバーでは実装するには苦痛になることもあるかもしれません。CherryPyを使えば、これらはわずか3行のコードのみで済みます!
あなたがやるべきことは、標準モジュールのHttpAuthenticateとCookieAuthenticateを使うことのみです。以下は両方のモジュールを使う例です。
use HttpAuthenticate, CookieAuthenticate
CherryClass Root:
mask:
def index(self):
<html><body>
<a py-attr="request.base+'/httpProtected/index'" href="">Click here to enter a restricte
d area using HTTP authentication</a><br>
<a py-attr="request.base+'/cookieProtected/index'" href="">Click here to enter a restric
ted area using cookie authentication</a><br>
In both cases, the login and password are "login" and "password"
</body></html>
CherryClass HttpProtected(HttpAuthenticate):
function:
def getPasswordListForLogin(self, login):
# Here we define what the login and password are
if login=='login': return ['password']
return []
mask:
def index(self):
<html><body>You're in</body></html>
CherryClass CookieProtected(CookieAuthenticate):
function:
def index(self):
<html><body>You're in</body></html>
CherryClass CookieProtected(CookieAuthenticate):
function:
def getPasswordListForLogin(self, login):
# Here we define what the login and password are
if login=='login': return ['password']
return []
mask:
def index(self):
<html><body>
You're in<br>
Click <a href="doLogout">here</a> to log out.
</body></html>
ごらんの通り、あなたがやるべきことはHttpAuthenticateかCookieAuthenticateから継承したCherryClassを作成し、与えられたログイン名とマッチするパスワードのリストを返すgetPasswordListForLoginと呼ばれる関数を実装するだけです(これは、例えば全てのユーザーに対して有効なマスターキーを保持ができるようになります)。
このとおり、これらの2つのモジュールを使うことは本当に簡単です。
次の章では、また別のCherryPyの標準モジュール、Formの使い方についてみていきましょう。