cherrypy 中有没有办法将函数 foo_test()
公开为服务器上的 /foo/test
端点?
下面的示例公开了两个端点 /index
和 /foo_test
:
class Test(object):
@cherrypy.expose
def index(self):
@cherrypy.expose
def foo_test(self):
注意:
- 我已经在
@cherrypy.expose(['foo/test'])
中使用别名进行了测试,但别名仅允许字符串和字符串列表。
最佳答案
最后我不得不重写_cp_dispatch
,如 http://docs.cherrypy.org/en/latest/advanced.html#the-special-cp-dispatch-method 中所述。
class Test(object):
def __init__(self):
self.foo = Foo()
def _cp_dispatch(self, vpath):
if len(vpath) == 1:
return self
if len(vpath) == 2:
return self.foo
return self
@cherrypy.expose
def index(self):
class Foo(object):
@cherrypy.expose
def test(self):
关于python - 使用带有路径前缀的cherrypy公开函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070793/