2012-01-19 3 views
2

내 목표는 CherryPy (wsgi) + uWSGI + Nginx를 사용하여 RESTful API를 정의하는 것입니다. #python에 나와 같이 OPTIONS 메서드를 처리하는 방법을 궁금하네요. 이 메서드에 대한 처리기를 구현하면 호출자가 내 API에 어떤 메서드가 지원되는지, 어떤 메서드가 지원되지 않는지 이해하는 데 도움이됩니다.CherryPy에서 http 옵션 메서드를 정의하는 방법은 무엇입니까?

는 여기에 지금까지있어 무엇 :


#!/usr/bin/env python 

import cherrypy 

# modules used for data access 
import nosql 
import dao 

class Product(object): 

    exposed = True 

    def GET(self, key, *args, **kwargs): 
     try: 
      p = Product(nosql.get(key)) 
      return p.json 
     except: 
      # return 500 error with traceback if debug 
      pass 

    def POST(self, *args, **kwargs): 
     try: 
      p = dao.Product(*args, **kwargs) 
      k = nosql.generate_key(Product.__name__) 
      nosql.set(k,str(p)) 
     except: 
      # return 500 error with traceback if debug 
      pass 

    def OPTIONS(self, *args, **kwargs): 
     """ 
     The question is, what to return here? I'm looking 
     at the following rfc: 

     http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html 
     """ 
     return "GET, POST" 

conf = { 
    '/': { 
     'request.dispatch':cherrypy.dispatch.MethodDispatcher(), 
    }, 
} 

application = cherrypy.tree.mount(Product, config=conf) 

답변

2

옵션 응답의 몸이 헤더로, 중요한, 그리고 확실하게 지정하지으로하지 않습니다. 여러분이 언급했듯이, 대부분의 클라이언트는 실제로 메소드에만 관심이 있습니다. 이것들은 "Allow"응답 헤더에 지정되어 있습니다. CherryPy는 MethodDispatcher를 사용할 때 자동으로 도움이됩니다. 귀하가 반환 할 수있는 것은 고객의 응용 프로그램 요구를 충족 시키려고 노력하는 것입니다.

관련 문제