iPhoneで撮った写真なんかとちょっとだけプログラム。世の中ってば自分の知らないことばかり。いろいろとりあえずかじってみる。
3. Google App Engine for Python でユーザからの送信されたデータを処理する。
google app engine。
3つめのエントリではユーザからの送信されたデータを表示するという単純なものです。
「<,>,&」が送信された場合に、そのまま表示してしまうとページが崩れてしまう恐れがありますので、pythonの標準ライブラリからCGIモジュールを使って置き換えをしています。
#pythonの標準ライブラリからcgiモジュールをインポートする。
import cgi
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
#メソッドがgetの場合
def get(self):
#出力
self.response.out.write("""
<html>
<body>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</body>
</html>""")
class Guestbook(webapp.RequestHandler):
#メソッドがpostの場合
def post(self):
self.response.out.write('<html><body>You wrote:<pre>')
#cgi.escapeでポストされたcontentの「&,<,>」を置き換えて出力。
self.response.out.write(cgi.escape(self.request.get('content')))
self.response.out.write('</pre></body></html>')
#URLリクエストによって振り分ける。
#http://アプリケーションのURL/の場合はMainPage、
#http://アプリケーションのURL/signの場合はGuestbook。
application = webapp.WSGIApplication(
[('/', MainPage),
('/sign', Guestbook)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
CGIについてはpythonのサイトに詳細が載っています(英語)。
http://docs.python.org/library/cgi.html
Convert the characters ‘&’, ‘<' and '>‘ in string s to HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML.
get()とpost()の2つを定義することで、それぞれのリクエストメソッドに対応した動作をすることが出来ます。
このデータをDBに登録すると、例えば掲示板やチャットのようなアプリケーションが作成できます。
| Print article | This entry was posted by admin on 2010年7月13日 at 11:56 PM, and is filed under GoogleAppEngine. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |