【发布时间】:2018-07-28 18:28:08
【问题描述】:
我有一个提供文件的应用程序。 HTTP GET/POST/PUT/DELETE 方法应该做不同的事情(显然)。因此,我编写了一个对象来表示提供 GET/POST/... 方法的文件。该文件由调度程序应用程序的_cp_dispatch 方法返回。查看我的源代码。
但是 Cherrypy 引发了一个异常,指出
TypeError: 'DispatchedFile1' object is not callableTypeError: unsupported callableTypeError: <__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