Cherrypy 对象不可调用 - MethodDispatcher

Cherrypy Object not callable - MethodDispatcher

我需要一点休息 API 并选择了 Cherrypy。我写 Cherrypy RESTApi 大约 2 年了。现在,使用与往常相同的开始,我 运行 陷入了错误,这让我发疯。 我在 Ubuntu 16.04 上使用 Python 3.5.2 和 Cherrypy 8.1.2。代码:

import cherrypy


class Sotd:
    exposed = True

    @cherrypy.tools.json_out()
    def GET(self):
        return {"message": "blub"}


class RestAPI:
    exposed = True


if __name__ == '__main__':

    api = RestAPI()
    api.sotd = Sotd()

    cherrypy.tree.mount(
        api,
        '/api',
        {
            '/sotd':
                {
                    'request.dispatcher': cherrypy.dispatch.MethodDispatcher()
                }
        }
    )

    cherrypy.server.socket_host = '0.0.0.0'
    cherrypy.server.socket_port = 8080
    cherrypy.engine.start()
    cherrypy.engine.block()

现在我正在尝试进行 GET 调用并期待我的 JSON 回来。

curl http://localhost:8080/api/sotd

然后事情就爆发了

Traceback (most recent call last):
  File "/usr/lib/python3.5/inspect.py", line 1089, in getfullargspec
    sigcls=Signature)
  File "/usr/lib/python3.5/inspect.py", line 2156, in _signature_from_callable
    raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: <__main__.Sotd object at 0x7efc1271b4a8> is not a callable object

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 64, in __call__
    test_callable_spec(self.callable, self.args, self.kwargs)
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 95, in test_callable_spec
    (args, varargs, varkw, defaults) = getargspec(callable)
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 212, in getargspec
return inspect.getfullargspec(callable)[:4]
   File "/usr/lib/python3.5/inspect.py", line 1095, in getfullargspec
    raise TypeError('unsupported callable') from ex
TypeError: unsupported callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py", line 670, in respond
    response.body = self.handler()
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/lib/encoding.py", line 220, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 68, in __call__
    raise x
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 60, in __call__
    return self.callable(*self.args, **self.kwargs)
TypeError: 'Sotd' object is not callable

我看到了关于 cherrypy 和不可调用对象的 1-2 Post - 但解决方案(简单配置失败)对我没有用:(

感谢您的帮助!

嗯,这是配置失败... 不是 'request.dispatcher' 是 'request.dispatch'