“TypeError:对象不可调用”使用`cherrypy.dispatch.MethodDispatcher()`

2024-04-26 23:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我将跟随cherrypy教程"Give it a REST",但我想让我的cherrypy服务器启动两个类:一个用于提供一些静态文件,另一个用于RESTful API:

在api.py文件以下内容:

import cherrypy

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

    def GET(self):
        return "Test GET!"

在服务器.py以下内容:

^{pr2}$

但是,当我启动服务器(python server.py)并在http://localhost:1313/api/v1/test上执行GET操作时,会出现以下错误:

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?,但不清楚答案是否真的适用于我。任何建议都将不胜感激!在


Tags: inpyself服务器apigetlibpackages
1条回答
网友
1楼 · 发布于 2024-04-26 23:32:13

刚刚意识到问题出在测试接口.conf公司名称:

需要在下面的小节中将配置的路径从'/api/v1/test'更改为'/'。在

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

我想这是因为我已经在server.py中传入了挂载路径,所以配置路径从那时起是相对的。在

相关问题 更多 >