我有一个包含多个路径的类,我希望他们都使用json工具,除了一个。如何从工具中排除特定路线(以下示例中为foo
)?
import cherrypy
class HelloWorld(object):
_cp_config = {
'tools.json_out.on': True,
'tools.json_in.on': True,
'/foo': {
'tools.json_out.on': True,
'tools.json_in.on': True
}
}
@cherrypy.expose()
def index(self):
return "Hello World!"
@cherrypy.expose()
def foo(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld())
答案 0 :(得分:3)
你可以使用cherrypy.config
装饰器:
import cherrypy
class HelloWorld(object):
_cp_config = {
'tools.json_out.on': True,
'tools.json_in.on': True
}
@cherrypy.expose
def index(self):
return "Hello World!"
@cherrypy.expose
@cherrypy.config(**{'tools.json_in.on': False, 'tools.json_out.on': False})
def foo(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld())
答案 1 :(得分:1)
@ cyraxjoe的答案是另一种但相当的方法
import cherrypy
class HelloWorld(object):
_cp_config = {
'tools.json_out.on': True,
'tools.json_in.on': True
}
@cherrypy.expose
def index(self):
return "Hello World!"
@cherrypy.expose
@cherrypy.config(**{'tools.json_in.on': False, 'tools.json_out.on': False})
def foo(self):
return "Hello World!"
foo._cp_config = {
'tools.json_out.on': False,
'tools.json_in.on': False,
}
cherrypy.quickstart(HelloWorld())