_cp_dispatch (Cherrypy) 的奇怪行为
Weird behavour of _cp_dispatch (Cherrypy)
我有一个提供文件服务的应用程序。 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
如果我使用 "normal" 方法通过 @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
.
手动查找方法
嗯,原来我忘了添加正确的请求调度程序。
正确的源代码是:
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()}
。
我有一个提供文件服务的应用程序。 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
如果我使用 "normal" 方法通过 @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
.
嗯,原来我忘了添加正确的请求调度程序。
正确的源代码是:
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()}
。