【问题标题】:RESTful web service example in cherrypyCherrypy 中的 RESTful Web 服务示例
【发布时间】:2017-06-07 02:36:26
【问题描述】:

我正在尝试用 python 编写一个 RESTful Web 服务。但是在尝试 Cherrypy Website 上给出的教程时,我遇到了类似的错误

Traceback (most recent call last):
  File "rest.py", line 35, in <module>
    cherrypy.quickstart(StringGeneratorWebService(), '/', conf)
TypeError: expose_() takes exactly 1 argument (0 given)

rest.py 是我的文件,其中包含网站上完全相同的代码,并且在副标题“给我们一个 REST”下。

我很清楚,显然从错误消息来看,我缺少一个应该传入的参数。但我不清楚我应该在哪里修改该代码以使其工作。

我尝试在第 35 行修复一些问题,但没有任何帮助,我被卡住了!请帮助我清除此问题,或者请提供一些代码 sn-p 以在cherrypy 中创建 REST 服务。谢谢!

【问题讨论】:

  • 您使用的是哪个版本的 CherryPy 和 Python?
  • 我用的是python 2.7版和cherrypy 3.2.2版

标签: python rest cherrypy


【解决方案1】:

您使用的 CherryPy 版本 (3.2.2) 不支持类上的 cherrypy.expose 装饰器,该功能是 added in version 6

您可以使用将exposed 属性设置为True 的旧语法(它也与较新的版本兼容)。

课程最终会是这样的:

class StringGeneratorWebService(object):
    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多