【发布时间】:2016-04-17 14:00:27
【问题描述】:
当我这样做并尝试访问“/api”时,cherryPy 会抛出“TypeError: 'ApiStringGenerator' object is not callable”错误
'''
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()
但是当我更改以下行时
cherrypy.tree.mount(ApiStringGenerator(), '/api', conf)
cherrypy.tree.mount(StringGenerator(), '/', conf)
cherrypy.engine.start()
cherrypy.engine.block()
通过代码
webapp = StringGenerator()
webapp.api = ApiStringGenerator()
cherrypy.quickstart(webapp, '/', conf)
然后没有错误,一切正常。 请帮忙。
【问题讨论】: