Search code examples
pythonpython-3.xhttpcherrypy

Cherrypy Object not callable - MethodDispatcher


i need a little REST API and picked Cherrypy. I'm writing Cherrypy RESTApi since about 2 years. Now, using the same start as usual i am running into an error, it is driving me crazy. I'm on a Ubuntu 16.04 with Python 3.5.2 and Cherrypy 8.1.2. The Code:

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()

Now im trying to make the GET Call and expect my JSON back.

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

And things blow up

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

I saw this 1-2 Post about cherrypy and Not Callable Object - but the solutions (simple config fails) didnt work out for me :(

Thanks for your help!


Solution

  • Well, it was a config fail... It's not 'request.dispatcher' it is 'request.dispatch'