2016-01-14 95 views
0

我正在關注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?來了,但如果答案真的是有適用於我目前還不清楚。任何指針將不勝感激!

回答

1

剛剛意識到這個問題是與TestApi.conf:

需要從'/api/v1/test'爲配置路徑更改爲'/'在下面的部分。

class TestApi(object): 
    conf = { 
     '/api/v1/test': { 
      'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 
     } 
    } 

我想這是因爲我已經在server.py安裝路徑通過這樣的配置路徑是從在這一點上相對的。

+1

是的,這是在這個問題相同的問題:http://stackoverflow.com/a/34753017/298371 – cyraxjoe

相關問題