【问题标题】:Weird behavour of _cp_dispatch (Cherrypy)_cp_dispatch (Cherrypy) 的奇怪行为
【发布时间】:2018-07-28 18:28:08
【问题描述】:

我有一个提供文件的应用程序。 HTTP GET/POST/PUT/DELETE 方法应该做不同的事情(显然)。因此,我编写了一个对象来表示提供 GET/POST/... 方法的文件。该文件由调度程序应用程序的_cp_dispatch 方法返回。查看我的源代码。

但是 Cherrypy 引发了一个异常,指出

  • TypeError: 'DispatchedFile1' object is not callable
  • TypeError: unsupported callable
  • TypeError: <__main__.DispatchedFile1 object at 0x7f209b4e7d30> is not a callable object

如果我使用 @expose d index 方法创建 Web 应用程序的“正常”方式,则此示例可以正常工作。

import cherrypy, os

@cherrypy.expose
class DispatchedFile1(object):
    def __init__(self, path):
        self._path = path

    def GET(self, start = 0, chunk_size = 0):
        if(start and chunk_size):
            return "{} ; OFFSET: {}, CHUNK_SIZE: {}".format(self._path,
                    start, chunk_size).encode("UTF-8")
        return self._path.encode("UTF-8")
class DispatchedFile2(object):
    def __init__(self, path):
        self._path = path

    @cherrypy.expose
    def index(self, start = 0, chunk_size = 0):
        if(start and chunk_size):
            return "{} ; OFFSET: {}, CHUNK_SIZE: {}".format(self._path,
                    start, chunk_size).encode("UTF-8")
        return self._path.encode("UTF-8")


class FileDispatcherApp1(object):
    def _cp_dispatch(self, vpath):
        print(vpath)

        res = DispatchedFile1("/".join(vpath))
        vpath.clear()
        print(vpath)
        return res

class FileDispatcherApp2(object):
    def _cp_dispatch(self, vpath):
        print(vpath)

        res = DispatchedFile2("/".join(vpath))
        vpath.clear()
        print(vpath)
        return res


class DummyApp(object):
    @cherrypy.expose
    def index(self):
        return "index"

cherrypy.tree.mount(FileDispatcherApp1(), "/files1")
cherrypy.tree.mount(FileDispatcherApp2(), "/files2")
cherrypy.quickstart(DummyApp(), "/")

第二个版本运行良好,但不能满足我的需求。 我在这里错过了什么吗?如何修复我的第一个版本?

PS.:我知道我可以使用cherrypy.request手动查找方法。

【问题讨论】:

    标签: python-3.x cherrypy


    【解决方案1】:

    好吧,原来我忘了添加正确的请求调度程序。

    正确的源代码是:

    import cherrypy, os
    
    @cherrypy.expose
    class DispatchedFile1(object):
        def __init__(self, path):
            self._path = path
    
        def GET(self, start = 0, chunk_size = 0):
            if(start and chunk_size):
                return "{} ; OFFSET: {}, CHUNK_SIZE: {}".format(self._path,
                        start, chunk_size).encode("UTF-8")
            return self._path.encode("UTF-8")
    class DispatchedFile2(object):
        def __init__(self, path):
            self._path = path
    
        @cherrypy.expose
        def index(self, start = 0, chunk_size = 0):
            if(start and chunk_size):
                return "{} ; OFFSET: {}, CHUNK_SIZE: {}".format(self._path,
                        start, chunk_size).encode("UTF-8")
            return self._path.encode("UTF-8")
    
    
    class FileDispatcherApp1(object):
        def _cp_dispatch(self, vpath):
            print(vpath)
    
            res = DispatchedFile1("/".join(vpath))
            vpath.clear()
            print(vpath)
            return res
    
    class FileDispatcherApp2(object):
        def _cp_dispatch(self, vpath):
            print(vpath)
    
            res = DispatchedFile2("/".join(vpath))
            vpath.clear()
            print(vpath)
            return res
    
    
    class DummyApp(object):
        @cherrypy.expose
        def index(self):
            return "index"
    
    cherrypy.tree.mount(FileDispatcherApp1(), "/files1", {"/": {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}})
    cherrypy.tree.mount(FileDispatcherApp2(), "/files2")
    cherrypy.quickstart(DummyApp(), "/")
    

    安装应用时注意{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2015-07-20
      相关资源
      最近更新 更多