gpt4 book ai didi

cherrypy - 帮助我更好地理解 CherryPy PageHandlers

转载 作者:行者123 更新时间:2023-12-01 03:00:08 26 4
gpt4 key购买 nike

假设我有一些代码(使用 CherryPy),如下所示:

import cherrypy

class Names:
def index(self, name=None):
return "Names.index: " + str(name)
index.exposed = True

class Root:
def index(self):
return "This is the root"
index.exposed = True

if __name__ == "__main__":
root = Root()

root.names = Names()

cherrypy.tree.mount(root, '/')
cherrypy.engine.start()
cherrypy.engine.block()

如果我点击网址 http://localhost:8080/names/ ,我看到 Names.index: None ,这很好。这意味着正在调用 Names() 类。

但是,如果我去 http://localhost:8080/names/mark ,我收到 404 错误而不是 Names.index: 标记我期待。

这让我很困惑,因为根据 PageHandler文档:

When a request is processed, the URI is split into its components, and each one is matched in order against the nodes in the tree. Any trailing components are "virtual path" components and are passed as positional arguments.



现在假设我将 Names() 类更改为如下所示:
class Names:
def index(self, name=None):
return "Names.index: " + str(name)
index.exposed = True

def name(self, name=None):
return "Names.name: " + str(name)
name.exposed = True

现在我可以去 http://localhost:8080/names/name/mark 我看到 Names.name: 标记。

有人可以解释这里发生了什么吗?

最佳答案

索引方法是 partial matching 的一个异常(exception)规则。您可以使用 默认 方法代替,或者在这个特定的例子中 make 姓名 一种方法本身。

关于cherrypy - 帮助我更好地理解 CherryPy PageHandlers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1076587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com