我正在關注cherrypy教程"Give it a REST",除了我想讓我的cherrypy服務器啓動兩個類:一個服務一些靜態文件和另一個用於基於REST的API:「TypeError:object is not callable」using cherrypy.dispatch.MethodDispatcher()`
api.py:
import cherrypy
class TestApi(object):
conf = {
'/api/v1/test': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
}
}
exposed = True
def GET(self):
return "Test GET!"
server.py:
import cherrypy
import api
server_conf = {
'server.socket_port': 1313,
}
class Root(object):
conf = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir': "/some/path",
'tools.staticdir.debug': True
}
}
@cherrypy.expose
def index(self):
return "Hello world!"
if __name__ == '__main__':
cherrypy.config.update(server_conf)
cherrypy.tree.mount(Root(), '/', Root.conf)
cherrypy.tree.mount(api.TestApi(), '/api/v1/test',
api.TestApi.conf)
cherrypy.engine.start()
cherrypy.engine.block()
然而,當我啓動服務器(python server.py
)和一個GET http://localhost:1313/api/v1/test
我得到這個錯誤:
500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request.
Traceback (most recent call last): File "/usr/local/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 670, in respond response.body = self.handler() File "/usr/local/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 217, in call self.body = self.oldhandler(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/cherrypy/_cpdispatch.py", line 68, in call raise x TypeError: 'TestApi' object is not callable
我擡頭類似的問題,並在 how to use multiple dispatchers in same cherrypy application?來了,但如果答案真的是有適用於我目前還不清楚。任何指針將不勝感激!
是的,這是在這個問題相同的問題:http://stackoverflow.com/a/34753017/298371 – cyraxjoe