Python Google App Engine:从 yaml 文件调用特定方法?

发布于 2024-09-08 02:03:30 字数 337 浏览 3 评论 0原文

我是使用 Google App Engine 进行数据库编程的新手,并且正在使用 Python 进行编程。我想知道是否允许一个 Python 文件包含多个请求处理程序类,每个类都有 get 和 post 方法。我知道 yaml 文件允许我指定使用特定 url 运行哪些脚本,如下例所示:

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

我如何告诉它运行 .py 文件中的类之一中的特定方法?这可能/允许吗?我是否需要将不同的请求处理程序类分离到不同的 python 文件中?目前我对数据库的了解还很浅,所以我可能没有任何意义。

谢谢。

I am new to database programming with Google App Engine and am programming in Python. I was wondering if I am allowed to have one Python file with several request handler classes, each of which has get and post methods. I know that the yaml file allows me to specify which scripts are run with specific urls, like the example below:

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

How would I tell it to run a specific method that is in one of the classes in the .py file? Is that even possible/allowed? Do I need to separate the different request handler classes into different python files? My understanding of databases is rather shallow at the moment, so I could be making no sense.

Thanks.

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

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

发布评论

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

评论(1

红墙和绿瓦 2024-09-15 02:03:30

我想知道我是否可以
一个Python文件包含多个
请求处理程序类,每个类
有 get 和 post 方法。

当然!该 app.yaml 只是将控制权转移到 helloworld.py,它将运行该文件中定义的 main 函数 - 并且该函数通常设置启动一个 WSGI 应用程序,该应用程序根据 URL 适当地分派到正确的处理程序类。例如,请查看此处的示例代码,非常早在教程中:

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

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

我不会复制 import 语句和类定义,因为它们并不重要:这是单个 .py 文件如何调度的示例到各种处理程序类(本例中为两个)。

当然,这并不意味着 yaml 文件允许您调用任何方法:相反,它将控制权交给 .py 文件,该文件的 main 负责所有这些事情如下(例如,对于 App Engine 附带的 webapp 迷你框架,它将始终是 getpost 方法 [[或 putdelete、...等,如果您也支持这些 — 很少有人这样做,除非它们特别 RESTful;-)]] 根据确切的 HTTP 被调用传入请求中的方法和 URL。

I was wondering if I am allowed to
have one Python file with several
request handler classes, each of which
has get and post methods.

Sure! That app.yaml just transfers control to helloworld.py, which will run the main function defined in that file -- and that function typically sets up a WSGI app which dispatches appropriately, depending on the URL, to the right handler class. For example, look at the sample code here, very early on in the tutorial:

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

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

I'm not copying the import statements and class definitions, because they don't matter: this is an example of how a single .py file dispatches to various handler classes (two in this case).

This doesn't mean the yaml file lets you call any method whatsoever, of course: rather, it hands control to a .py file, whose main is responsible for all that follows (and e.g. with the webapp mini-framework that comes with App Engine, it will always be get or post method [[or put, delete, ..., etc, if you also support those -- few do unless they're being especially RESTful;-)]] being called depending on the exact HTTP method and URL in the incoming request.

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