2014-10-18 4 views
0

정확하게 두 가지 요청을 허용하는 최소한의 웹 사이트를 만들려고합니다. 먼저 '/'에 대한 GET 요청으로 간단한 문자열로 응답해야합니다. 두 번째는 '/ put_url'에 대한 데이터 요청을 허용하는 PUT 요청입니다. 다음은 지금까지 내가 가지고있는 것입니다 :PUT 방법을 사용하는 미니멀리스트 체리 피 웹 사이트

import cherrypy 

class Main(object): 
    @cherrypy.expose 
    def index(self): 
     return "Hy?" 

class Uploader(object): 
    exposed = True 
    def PUT(self, data): 
     print "hello" 
     print data 

if __name__ == '__main__': 
    conf = { 
     '/put_url': { 
      'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 
      'tools.response_headers.on': True, 
      'tools.response_headers.headers': [('Content-Type', 'text/plain')] 
      } 
     } 

    webapp = Main() 
    webapp.put_url = Uploader() 
    cherrypy.quickstart(webapp, '/', conf) 

어떻게 작동합니까? 웬일인지 나는 이것을 이해할 수 없다. 감사.

+0

문서 [이 예제] (http://docs.cherrypy.org/en/latest/tutorials.html#tutorial-7-give-us-a-rest)를 참조하십시오. –

답변

1

수락 헤더를 추가하십시오. 이렇게하려면 PUT 메소드에 데코레이터를 추가하십시오.

class Uploader(object): 
    exposed = True 
    @cherrypy.tools.accept(media='text/plain') 
    def PUT(self, data): 
     print "hello" 
     print data 

수락 헤더로 text/plain을 항상 사용하지 않을 수도 있습니다. 그것은 귀하의 응용 프로그램에 따라 다릅니다.

는 문제는 당신이 동일한 응용 프로그램에 디스패처를 혼합하는 것입니다

curl -X PUT -d data=xyz localhost:8080/put_url 
1

사용할 수 있습니다 테스트합니다. 기본 디스패처를 혼합하는 것도 가능

import cherrypy as cp 

class Main(object): 
    exposed = True 

    def GET(self): 
     return "Hy?" 

class Uploader(object): 
    exposed = True 

    def PUT(self, data): 
     cp.log.error("hello") 
     cp.log.error(data) 
     return "The data {} has been puted".format(data) 

if __name__ == '__main__': 
    conf = { 
     '/': { 
      'request.dispatch': cp.dispatch.MethodDispatcher() 
     }, 
     '/put_url': { 
      'tools.response_headers.on': True, 
      'tools.response_headers.headers': [('Content-Type', 'text/plain')] 
     } 
    } 
    webapp = Main() 
    webapp.put_url = Uploader() 
    cp.quickstart(webapp, '/', conf) 

: HTTP 메서드 등의 방법을 가지고 메인 오브젝트를 변경하면이 같은 뭔가를해야합니다 응용 프로그램의 루트에 디스패처를 이동 예를 들어

예를 들어, 방법 디스패처, 상기 API 객체가 /api에 장착하는 방법을

import cherrypy as cp 

class Main(object): 
    @cp.expose 
    def index(self): 
     return "Hy?" 


class Uploader(object): 
    exposed = True 

    def PUT(self, data): 
     cp.log.error("hello") 
     cp.log.error(data) 
     return "The data {} has been puted".format(data) 


class API(object): 
    exposed = True 

    def __init__(self): 
     self.put_url = Uploader() 

    def GET(self): 
     return "Welcome to the API!" 


if __name__ == '__main__': 
    cp.tree.mount(Main()) 
    cp.tree.mount(API(), '/api', config={ 
     '/': { 
      'request.dispatch': cp.dispatch.MethodDispatcher() 
     }, 
     '/put_url': { 
      'tools.response_headers.on': True, 
      'tools.response_headers.headers': [('Content-Type', 'text/plain')] 
     } 
    }) 
    cp.engine.start() 
    cp.engine.block() 

알 수 있습니다. put_url 메서드의 PUT으로 URL /api/put_url을 사용하면됩니다. cherrypy.tree.mount에 사용 된 구성 섹션은 탑재 지점에 상대적입니다.이 경우 /api입니다.