如何利用Google App Engine搭建网络服务(一)
注:本文大部分内容可以在Google App Engine开发指南中查到,如果大家在使用过程中遇到什么问题,欢迎大家留言。
1.开发环境:
下载Python2.5或以上版本,本人安装的是Python2.7。
下载App Engine SDK:http://code.google.com/intl/zh-CN/appengine/downloads.html
这里基本需要的命令只有两条:
dev_appserver.py folder_name:在本地虚拟服务器。
appcfg.py update folder_name:上传目录到服务器。
2.hello_world程序:
十分简单,可以参见:http://code.google.com/intl/zh-CN/appengine/docs/python/gettingstarted/helloworld.html
3.基本的webapp框架:
webapp由三部分组成:RequestHandler类,将网址映射到RequestHandler的WSGIApplication对象,与使用CGI适配器运行WSGIApplication的一个主要例程。
在主要例程中调用主函数:
if __name__ == "__main__":
main()
在main函数中调用WSGIApplication:
def main():
run_wsgi_app(application)
这里application是一个将网址映射到RequestHandler的对象:
application = webapp.WSGIApplication([('/', MainPage)], debug=True)
然后你可以实现多个不同的RequestHandler
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
将这些按照正确的顺序排列起来,就可以处理简单的程序。
4.调用Google的数据库:
调用Google数据库非常简单,需要定义数据模型:
class Greeting(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
调用数据库:
from google.appengine.ext import db
然后从数据库中取出信息即可:
greetings = db.GqlQuery("SELECT * FROM Greeting ORDER BY date DESC LIMIT 10")
5.以下是一个基本的样例,模拟Google用户登录,留言等操作,可以登录(http://yaoyaowd.appspot.com/usernote 试用):
import cgi
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
class CommentDB(db.Model):
author = db.UserProperty()
topic = db.StringProperty(multiline=True)
content = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<html><body>Hello, this is yaoyao test page</body><html>')
class CommentPage(webapp.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
self.response.out.write('<html><body>')
comments = db.GqlQuery("SELECT * FROM CommentDB ORDER BY date DESC")
for comment in comments:
self.response.out.write('<b>%s</b> wrote for topic <b>%s</b>' % (comment.author.nickname(), comment.topic))
self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(comment.content))
self.response.out.write("""
<form action="/post_comment" method="post">
<div><b>Title:</b><textarea name="topic" rows="1" cols="60"></textarea></div>
<div><b>Content:</b><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post Comment"></div>
</form>
</body>
</html>""")
else:
self.redirect(users.create_login_url(self.request.uri))
class PostCommentPage(webapp.RequestHandler):
def post(self):
comment = CommentDB()
comment.author = users.get_current_user()
if comment.author:
comment.topic = self.request.get('topic')
comment.content = self.request.get('content')
comment.put()
self.redirect('/comment')
application = webapp.WSGIApplication([('/', MainPage),
('/comment', CommentPage),
('/post_comment', PostCommentPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
作者:Dong Wang