配置脚本处理程序Google App Engine

时间:2012-02-21 05:37:46

标签: python google-app-engine python-2.5

我正在尝试使用Google App Engine制作一个简单的应用程序。

以下是我的代码

helloworld.py

print "hello"

class helloworld():
        def myfunc(self):
                st = "inside class"
                return st

test.py

import helloworld

hw_object  = helloworld.helloworld()
print  hw_object.myfunc()

app.yaml

handlers:
- url: /.*
  script: helloworld.py

- url: /.*
  script: test.py

当我通过http://localhost:10000运行我的应用程序时,它仅打印hello,而我的预期输出为helloinside class

我的目录结构

E:\helloworld>dir
app.yaml       helloworld.py  test.py

我很确定这与Script Handlers有关。那么,定义处理程序的正确方法是什么,以及我定义它们的方法有什么问题。

3 个答案:

答案 0 :(得分:3)

当您的第一个处理程序模式/.*http://localhost:10000匹配时,其余的处理程序都会被忽略。

您可以更新app.yaml

handlers:
- url: /hello
  script: helloworld.py

- url: /test
  script: test.py

并浏览http://localhost:10000/test

答案 1 :(得分:0)

请参阅appengine文档中的“入门指南”。它将帮助您解决这样的初始设置问题。

http://code.google.com/appengine/docs/python/gettingstarted/helloworld.html

以下是该文档中的示例处理程序。

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

application = webapp.WSGIApplication(
                                 [('/', MainPage)],
                                 debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

请注意,该类扩展了webapp.RequestHandler,方法名称为get(如果您正在响应http post请求,则发布)以及底部用于设置应用程序的额外代码。您可以通过向WSGIApplication添加参数来向应用程序添加额外的URL。例如:

application = webapp.WSGIApplication(
                                 [('/', MainPage)],
                                 [('/help/', HelpPage)],
                                 debug=True)

另请注意,在app.yaml中,由于两个脚本都引用相同的url模式,因此任何请求都无法访问test.py.正常模式是在顶部有特定的url模式,并且最后一个模式。

祝你好运。

答案 2 :(得分:0)

我也有类似的问题。扩展Hamish的答案,并纠正方括号的最后部分:

application = webapp.WSGIApplication([
                            ('/', MainPage), 
                            ('/help/', HelpPage)],
                            debug=True)

参考: https://webapp-improved.appspot.com/guide/routing.html

**编辑我上面的代码中还有一个额外的结束括号。现在改变了。