Bottleで、
/slash/
/slash
を同じURLと解釈してコンテンツを表示する際、
@route("/slash") @route("/slash/") def slash(): return "Hello world!"
としていたのですが、他に良い方法がないかを調べた時のメモです。
環境
- Windows10 x64
- Python 2.x
- Bottle 0.12.9
対応
Bottleの公式ドキュメントのレシピに記載がありました。before_request
フックを使うようです。
Recipes — Bottle 0.13-dev documentation
そのため、
@hook("before_request") def strip_path(): request.environ["PATH_INFO"] = request.environ["PATH_INFO"].rstrip("/") @route("/slash") def slash(): return "Hello world!"
としたところ、/slash/
であっても、Hello world!が表示されました。
Bottleのフックについて
Bottleのフックはどんなものがあるんだろうと思って調べたところ、公式ドキュメントに
before_request
after_request
app_reset
の3つのフックの記載がありました。
API Reference — Bottle 0.13-dev documentation
ソースコード
GitHubに上げました。trailing_slash.py
が今回のソースコードです。
thinkAmi-sandbox/Bottle-sample: Bottle (python web framework) : sample codes