python - 使用带有路径前缀的cherrypy公开函数

标签 python cherrypy

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/

相关文章:

python - 将 try/except 的 except 部分分离到函数中

生成器长度至少为 n 的 Pythonic 方式

caching - SQLAlchemy 是否支持缓存?

python-2.7 - 如何使用 HTTPS 在 CherryPy WSGI 服务器 (Cheroot) 上运行 Flask 应用程序?

python - 安装 Python 时使用 pypy 轻松安装

python - 如何从 Ansible list 文件中获取主机列表?

python - 合并列的重复单元格

python - 有没有办法通过 cherrypy 服务器获取客户端主机名

python - Cherrypy 索引调度程序不像定义的那样

python - Cherrypy 中的子页面和路由