2017-11-27 14 views
0

REST 스타일 API를 생성하기위한 cherrypy 문서 페이지의 튜토리얼 7을 따르려고합니다. 복사 - 붙여 넣기 tutorial의 코드,Cherrypy REST 튜토리얼은 TypeError를 반환합니다. _expose()는 정확히 하나의 인수를받습니다

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) 

하지만 cherrypy 3.8을 실행하고

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 버전 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 데코레이터를 제거하지 않았다. 그래서 후자가 문제를 해결했다. –