【问题标题】:gae & python - setting different urls in yaml: 404 not foundgae & python - 在 yaml 中设置不同的 url: 404 not found
【发布时间】:2013-02-28 06:53:50
【问题描述】:

我正在尝试将不同的 url 映射到不同的 python 脚本。

这是我的 yaml

application: myApp
version: 99
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /deleteCustomers
  script: test.app

- url: /.*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"

builtins:
- remote_api: on

如果我转到http://myapp.appspot.com/test,它会显示“404 未找到”... 如果我去http://myapp.appspot.com,就会启动正确的脚本(main.app)

我遇到了同样的问题 -> HERE 但给定的解决方案对我不起作用(即使它是相同的代码!!!)

这是处理程序(为了测试“2 路径 yaml”,我复制了 main.app,它包含客户和商店类以及 mainhandler,将其重命名为 test.app。所以 main.app 和 test.app 都是相同)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        customers = Customers.all()
        stores = Stores.all()

        countCustomers= 0
        countStores= 0

        for p in customers:
            p.delete()
            countCustomers+= 1
        for p in stores:
            p.delete()
            countStores+= 1

        self.response.out.write("\nDeleted Customers: " + str(countCustomers))
        self.response.out.write("\nDeleted Stores: " + str(countStores))

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

我想要实现的是将客户和商店删除拆分为两个单独的调用:

http://www.myapp.appspot.com/deleteCustomershttp://www.myapp.appspot.com/deleteStores

提前感谢您的帮助,最好的问候

【问题讨论】:

  • 试试这个:url:/test.* 你也可以发布处理程序和路由到测试处理程序的路由器。
  • 你能把代码贴在测试应用中吗?

标签: python web-services google-app-engine http-status-code-404


【解决方案1】:

如果您说两个脚本完全相同,那么我假设您使用相同的“/”来指向您的 MainHandler。我不太确定我是否正确理解你,但这是我试图帮助你的尝试。为了实现将商店删除和客户删除拆分为 2 个不同的脚本,您必须将代码拆分为映射到每个 url 的 2 个不同的处理程序,例如:

class StoreDeletionHandler(webapp2.RequestHandler):
def get(self):
    stores = Stores.all()

    countStores= 0

    for p in stores:
        p.delete()
        countStores+= 1

    self.response.out.write("\nDeleted Stores: " + str(countStores))        


app = webapp2.WSGIApplication([('/deleteStores', StoreDeletionHandler)], debug=True)

以上内容将在您的 main.py 脚本中通过 yaml 脚本中的以下调用进行路由:

- url: /.*
  script: main.app

然后在本例中为另一个脚本 test.py 中的第二个 url:

class CustomerDeletionHandler(webapp2.RequestHandler):
    def get(self):

        customers = Customers.all()
        countCustomers= 0

        for p in customers:
            p.delete()
            countcustomers+= 1

        self.response.out.write("\nDeleted Customers: " + str(countCustomers))

app = webapp2.WSGIApplication([
('/deleteCustomers', CustomerDeletionHandler)
], debug=True)

在您的 yaml 文件中,您可以通过以下方式将 url 映射到脚本:

- url: /deleteCustomers
  script: test.app

还要注意,为了将所有后续路由定向到 test.py 脚本,URL 必须以“/deleteCustomers”前缀开头

所以是这样的:

http://www.myapp.appspot.com/deleteCustomers/NewUrl1
http://www.myapp.appspot.com/deleteCustomers/SomethingElse
http://www.myapp.appspot.com/deleteCustomers/YetAnotherUrlForTestpy

以上所有内容都将被定向到 test.py 脚本。要重定向到 main.py 脚本,您只需路由到除 /deleteCustomers 之外的任何其他内容

http://www.myapp.appspot.com/ThisGoesToMain
http://www.myapp.appspot.com/deleteStores #also goes to main
http://www.myapp.appspot.com/deleteStores/YetAnotherUrlForMain

我希望这是你想要的。

【讨论】:

  • 感谢 Tkingovr,我没有在 webapp2.WSGIApplication 函数中设置路径值。现在可以了,再次感谢!
猜你喜欢
  • 2017-07-01
  • 2020-10-30
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2015-06-27
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多