简体   繁体   English

Cherrypy中的RESTful Web服务示例

[英]RESTful web service example in cherrypy

I am trying to write a RESTful web service in python. 我正在尝试用python编写RESTful Web服务。 But while trying out the tutorials given on Cherrypy Website I ended up with an error like 但是当尝试在Cherrypy网站上给出的教程时,我最终遇到了类似的错误

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

Where rest.py is my file which contains the exact same code on the site and under subtitle "Give us a REST". 其中rest.py是我的文件,其中包含站点上完全相同的代码,并带有副标题“给我们REST”。

I am clear that, obviously from error message, I am missing a parameter that should be passed in. But I am not clear where exactly I should amend that code to make it work. 我很清楚,很明显从错误消息中,我缺少应该传递的参数。但是我不清楚应该在哪里修改该代码以使其正常工作。

I tried out fixing something on line number 35, but nothing helped me, and I am stuck! 我尝试修复35号行上的内容,但没有任何帮助,我被卡住了! Please help me to clear this or please give some code snippet to make a REST service in cherrypy. 请帮助我清除此问题,或者提供一些代码片段以使用cherrypy进行REST服务。 Thank you! 谢谢!

The CherryPy version that you're using ( 3.2.2 ) doesn't support the cherrypy.expose decorator on classes, that functionality was added in version 6 . 您正在使用的CherryPy版本( 3.2.2 )不支持类中的cherrypy.expose装饰器,该功能已在版本6添加

You can use the old syntax of setting the exposed attribute to True (it is also compatible with the newer versions). 您可以使用将exposed属性设置为True的旧语法(它也与较新版本兼容)。

The class would end up like: 该类最终会像:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM