2017-11-27 15 views
0

RESTスタイルAPIを作成するためのcherrypyドキュメントページのチュートリアル7に従ってみてください。コピー&ペーストtutorialからコード、Cherrypy RESTチュートリアルはTypeErrorを返します:_expose()は1つの引数をとります

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) 

与えられた私は、このようにthis questionは、あなたがリンクされていることの問題では

答えて

0

参考にされていなかった、CherryPyは3.8を実行していていますCherryPyバージョン6では、cherrypy.exposeというクラスを装飾するサポートが追加されたと付け加えました。

あなたは3.8を使用しています。

6.0より後のバージョンにCherryPyをアップグレードするか、修飾された公開と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) 
+0

ご迷惑をおかけして申し訳ありませんが、これは機能します。 –

+0

私はexposed = Trueを以前に追加しようとしましたが、exposeデコレータを削除しなかったので、問題を解決したのは後者でした –

関連する問題