[英]Unable to add more than one applications together in cherryPy
When I do this and try to access "/api" than cherryPy throws "TypeError: 'ApiStringGenerator' object is not callable" error 当我这样做并尝试访问“ / api”时,cherryPy会引发“ TypeError:'ApiStringGenerator'对象不可调用”错误
'''
Created on Jan 11, 2016
@author: ankurjat
'''
import cherrypy
import random
import string
import os
conf = {'/': {'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())},
'/static': {'tools.staticdir.on': True,
'tools.staticdir.dir': './resources'},
'/api': {'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.sessions.on': True,
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')]}
}
class ApiStringGenerator(object):
exposed = True
@cherrypy.tools.accept(media='text/plain')
def GET(self, length=8):
value = cherrypy.session['mystring']
return value
def POST(self, length=8):
value = ''.join(random.sample(string.hexdigits, int(length)))
cherrypy.session['mystring'] = value
return value
def PUT(self, value):
cherrypy.session['mystring'] = value
def DELETE(self):
cherrypy.session.pop('mystring', None)
class StringGenerator(object):
@cherrypy.expose
def index(self):
return file('templates/index.html')
if __name__ == '__main__':
cherrypy.tree.mount(ApiStringGenerator(), '/api', conf)
cherrypy.tree.mount(StringGenerator(), '/', conf)
cherrypy.engine.start()
cherrypy.engine.block()
But when I change below lines 但是当我改变下面的线
cherrypy.tree.mount(ApiStringGenerator(), '/api', conf)
cherrypy.tree.mount(StringGenerator(), '/', conf)
cherrypy.engine.start()
cherrypy.engine.block()
By the code 通过代码
webapp = StringGenerator()
webapp.api = ApiStringGenerator()
cherrypy.quickstart(webapp, '/', conf)
Then no error and everything works fine. 然后没有错误,一切正常。 Please help. 请帮忙。
The problem is that the configuration in cherrypy is relative to the mountpoint. 问题在于cherrypy中的配置是相对于安装点的。
So when you are configuring the MethodDispatcher
in /api
inside the mount point /api
. 因此,当您在挂载点/api
内的/api
中配置MethodDispatcher
时。 You are activating the MethodDispatcher
inside /api/api
and the dispatcher that's gets used in /api
it's the default one, hence trying to call the object because the object has the exposed
attribute but it's not callable. 您激活MethodDispatcher
内/api/api
,这就是被使用的调度/api
它是默认的,因此试图调用对象,因为对象具有exposed
属性,但它不是可调用的。 Which is the behavior of the default dispatcher. 这是默认调度程序的行为。
If you want to do: 如果您想这样做:
cherrypy.tree.mount(ApiStringGenerator(), '/api', conf)
The configuration needs to be relative to the /api
: 配置必须相对于/api
:
{'/': {'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.sessions.on': True,
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')]}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.