Cherrypy中的RESTful Web服务示例 [英] RESTful web service example in cherrypy

查看:51
本文介绍了Cherrypy中的RESTful Web服务示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用python编写RESTful Web服务.但是,在尝试使用Cherrypy网站我最终遇到类似

I am trying to write a RESTful web service in python. But while trying out the tutorials given on Cherrypy Website I ended up with an error like

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".

Where rest.py is my file which contains the exact same code on the site and under subtitle "Give us a 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.

我尝试修复第35行的内容,但没有任何帮助,我被卡住了!请帮助我清除此问题,或者提供一些代码片段以使用cherrypy进行REST服务.谢谢!

I tried out fixing something on line number 35, but nothing helped me, and I am stuck! Please help me to clear this or please give some code snippet to make a REST service in cherrypy. Thank you!

推荐答案

您使用的CherryPy版本( 3.2.2 )不支持 cherrypy.expose 类上的装饰器,该功能已在版本6中添加了.

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.

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

You can use the old syntax of setting the exposed attribute to True(it is also compatible with the newer versions).

该类最终会像:

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)

这篇关于Cherrypy中的RESTful Web服务示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆