如何利用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

转载于:https://www.cnblogs.com/takeitandgo/archive/2011/03/08/1977716.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值