Google App Engine 入门:使用webapp framework
五月 4th, 2008
(本文译自:Google App Engine Getting Started)
使用CGI模式来开发网页是相当简单的,但是如果要这样手工完成所有的代码无疑是相当让人感到苦恼的事情。那么,使用Web application frameworks来处理具体的细节是个不错的选择, Google App Engine supports 支持所有基于python的CGI,WCGI框架应用,包括 Django, CherryPy, Pylons, web.py等等... 你可以选择其中的任何一个进行开发.(就我看来,GAE整个框架多处参考了Django,所以使用Django框架,是个不错的选择).
GAE内置了一个叫做
webapp的WEB应用框架,并包含在SDK环境中, 你可以直接在你的应用中使用它。接下来,我们就使用这个框架来完成后面的教程。
Hello, webapp!
一个webapp 应用包含三部分
- 一个或多个
RequestHandler类用来处理http请求和应答 - 一个
WSGIApplication实例,根据不同的URL请求,将处理交给不同的RequestHandler类实例。 - 一个主过程,通过CGI adaptor方式运行
WSGIApplication
webapp 将helloword的例子重新修改一下吧。
编辑 helloworld/helloworld.py 并且把内容修改为:
import wsgiref.handlers刷新http://localhost:8080/ 查看输出结果,是不是还是很简单啊。
from google.appengine.ext import webapp
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
def main():
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == "__main__":
main()
What webapp Does
webapp 模块可以在 google.appengine.ext 包中找到. 这个模块由GAE SDK提供, 并且和实际运行环境是一致的.
上面的代码声明了一个 request handler, MainPage, 把URL(/)交给应用程序来处理. 当 webapp 接受到一个形如 / 这样 HTTP GET 请求,就会调用MainPage 类,并且调用get 请求. 在这个方面里面,关于请求的所有信息可以使用 self.request来调用,一般来说,我们会在这里通过 self.response 设置应答内容,然后退出, webapp 会把应答的内容返回给 MainPage 实例.
这个应用程序本身是一个 webapp.WSGIApplication ,我们可以设置参数 debug=true ,让 webapp 在遇到错误时在浏览器中显示堆栈调用信息。但是,在发布前,请记得要把这个选项去掉。=
这段代码中使用了 wsgiref 模块,这个模块提供基本的CGI处理功能,包含在Python标准库中.要了解关于这么模块的更多信息,请查看 the wsgiref module documentation.
我们将会在后面的教程中使用更多 webapp 的功能. 想要知道 webapp更详细信息,请查看 the webapp reference.
接下来...
WEB应用程序框架使GAE开发更加简单,快速,并且减少了错误的发生。但webapp 只是Python的WEB应用框架之一.接下来,我们会逐步完善它,并为它增加一些功能。
下一章: 用户系统服务
我搭建了一个下午也没有也没有弄好,最后在http://drawerofdon.appspot.com/?p=1上看到怎么做,但是他提到上传indexes.yaml,但是我不知道语法,有人可以帮下忙吗?
2 from google.appengine.ext.webapp.util import run_wsgi_app
3
4 class MainPage(webapp.RequestHandler):
5 def get(self):
6 self.response.headers['Content-Type'] = 'text/plain'
google undefined, webapp undefined
这会是什么原因啊,
2 from google.appengine.ext.webapp.util import run_wsgi_app
3
4 class MainPage(webapp.RequestHandler):
5 def get(self):
6 self.response.headers['Content-Type'] = 'text/plain'
google undefined, webapp undefined
这会是什么原因啊,
2 from google.appengine.ext.webapp.util import run_wsgi_app
3
4 class MainPage(webapp.RequestHandler):
5 def get(self):
6 self.response.headers['Content-Type'] = 'text/plain'
google undefined, webapp undefined
这会是什么原因啊,
debug=True)这句加进去就错 不知道怎么回事 去掉就成功了 谁给解释下
我改用 http://127.0.0.1:8080 就可以访问了 谢天谢地
很明显,你没有权限
858 error appcfg.py:1128 an unexpected error occurred.abort
error 403:---begin server output---
you do not have permission to modify this app.
---end server output---
这是什么意思哦,搞了半天!
Traceback (most recent call last):
File "C:\helloworld\helloworld.py", line 2, in
from google.appengine.ext import webapp
ImportError: cannot import name webapp
import webob
Traceback (most recent call last):
File "C:\helloworld\helloworld.py", line 2, in
from google.appengine.ext import webapp
ImportError: cannot import name webapp
是什么原因
Traceback (most recent call last):
File "C:\helloworld\helloworld.py", line 2, in
from google.appengine.ext import webapp
ImportError: cannot import name webapp
是什么原因
Traceback (most recent call last):
File "C:\helloworld\helloworld.py", line 2, in
from google.appengine.ext import webapp
ImportError: cannot import name webapp
是什么原因
和现在google上的代码有点不同,有问题的话自己debug一下就好了。
我研究看看,是哪里的问题
看来是 wp 的问题, 自作主张改了双引号
对了, 好像你这儿的代码, 英文引号 "" '' 都被换成了中文引号 “” ‘’ 新手照抄的话肯定不知道怎么出错的。 改一改吧
[...] 下一章: 使用 Webapp Framework. [...]