【问题标题】:Cherrypy REST tutorial returns TypeError: _expose() takes exactly 1 argumentCherrypy REST 教程返回 TypeError: _expose() 只需要 1 个参数
【发布时间】:2018-05-11 07:48:19
【问题描述】:

尝试按照cherrypy 文档页面上的教程7 来创建REST 风格的API。从tutorial复制粘贴代码,

import random
import string

import cherrypy

@cherrypy.expose
class StringGeneratorWebService(object):

    @cherrypy.tools.accept(media='text/plain')
    def GET(self):
        return cherrypy.session['mystring']

    def POST(self, length=8):
        some_string = ''.join(random.sample(string.hexdigits, int(length)))
        cherrypy.session['mystring'] = some_string
        return some_string

    def PUT(self, another_string):
        cherrypy.session['mystring'] = another_string

    def DELETE(self):
        cherrypy.session.pop('mystring', None)


if __name__ == '__main__':
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.sessions.on': True,
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type',    'text/plain')],
        }
    }
    cherrypy.quickstart(StringGeneratorWebService(), '/', conf)

但是运行时会报错

File "H:/researchInstrumentCatalog/dqapi/core/test.py", line 36, in <module>
cherrypy.quickstart(test(), '/', conf)
TypeError: expose_() takes exactly 1 argument (0 given) 

我正在运行cherrypy 3.8,因此this question 没有帮助

【问题讨论】:

    标签: python rest cherrypy


    【解决方案1】:

    在您链接的问题中,我提到在 CherryPy 版本 6 中添加了对使用 cherrypy.expose 装饰类的支持。

    您使用的是 3.8。

    将 CherryPy 升级到 6.0 之后的任何版本,或者只是不使用暴露装饰并设置属性为exposed = True

    class StringGeneratorWebService(object):
    
        # this is the attribute that configured the expose decorator
        exposed = True
    
        @cherrypy.tools.accept(media='text/plain')
        def GET(self):
            return cherrypy.session['mystring']
    
        def POST(self, length=8):
            some_string = ''.join(random.sample(string.hexdigits, int(length)))
            cherrypy.session['mystring'] = some_string
            return some_string
    
        def PUT(self, another_string):
            cherrypy.session['mystring'] = another_string
    
        def DELETE(self):
            cherrypy.session.pop('mystring', None)
    

    【讨论】:

    • 我之前尝试过添加exposed = True,但没有删除expose装饰器,所以最终解决了问题
    猜你喜欢
    • 2016-08-17
    • 2012-10-19
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多