配置脚本处理程序 Google App Engine

发布于 2025-01-07 16:52:32 字数 924 浏览 5 评论 0原文

我正在尝试使用 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

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

I am trying to make a simple application using Google App Engine.

Below is my code

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

When I run my application via http://localhost:10000 it prints only hello whereas my expected output is hello and inside class.

My directory structure

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

I am pretty sure this has something to do with Script Handlers.So, what is the correct way to define handlers and what is wrong in my way of defining them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分開簡單 2025-01-14 16:52:32

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

您可以更新您的 app.yaml

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

- url: /test
  script: test.py

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

When your first handler pattern /.* matches http://localhost:10000, the remaining handlers are all ignored.

You can updated your app.yaml

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

- url: /test
  script: test.py

And browse http://localhost:10000/test

り繁华旳梦境 2025-01-14 16:52:32

请浏览 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 请求,则为 post),还有底部用于设置应用程序的额外代码。您可以通过向 WSGIApplication 添加参数来向应用程序添加额外的 URL。例如:

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

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

祝你好运。

Please walk through the Getting Started guide from the appengine documentation. It will help you get through the initial setup problems like this.

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

Here is the sample Handler from that documentation.

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()

Note that the class extends webapp.RequestHandler, the method name is get (or post if you are responding to a http post request) Also the extra code at the bottom for setting up the application. You can add extra URL's to the application by adding arguments to the WSGIApplication. For example:

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

Also note that in your app.yaml as both scripts refer to the same url pattern, there is no way that any request will ever get to test.py. The normal pattern is to have specific url patterns at the top and a catch-all patter last.

Good Luck.

天生の放荡 2025-01-14 16:52:32

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

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

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

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

I had a similar problem too. Expanding on Hamish's answer, and correcting the last part where the square brackets are:

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

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

** Edit I also had an extra closing bracket in my code above. Changed that now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文