2
私の目標は、CherryPy(wsgi)+ uWSGI + Nginxを使ってRESTfulなAPIを定義することです。私は#pythonに私に示唆されているように、OPTIONSメソッドを処理する方法を知っています。私は、このメソッドのハンドラを実装することで、呼び出し元が自分のAPIにどのメソッドがサポートされているのか、どのメソッドがサポートされていないのかを理解するのに役立つとアドバイスされました。CherryPyでhttp OPTIONSメソッドを定義する方法は?
#!/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)