GAE/Phthon 用フレームワーク
はじめに
- GAE/Python では Web アプリ用に webapp というフレームワークが用意されているので、それを使用して Hello World を書き換える。
「Hello World!」(webapp フレームワーク版)
wmeterserver.py
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import os
from google.appengine.ext.webapp import template
class MainPage(webapp.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'template/index.html')
self.response.out.write(template.render(path, {}))
application = webapp.WSGIApplication([('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
概要
- webapp.RequestHandler クラスを継承した MainPage クラスを作成 (6行目)
- GET リクエストを処理する get メソッドを定義 (7行目)
- テンプレートのファイルを指定し、template.render メソッドでレンダリング (8, 9行目)
- Web アプリ本体。リクエストと処理クラスのマッピング (11行目)
- Web アプリを AppEngine 環境で実行 (15行目)
template/index.html
<html>
<head>
<title>Hello</title>
</head>
<body>
Hello World!
</body>
</html>
まとめ