是否有一份关于在服务器端使用不同基于python的REST框架来编写自己的RESTful api的推荐列表?最好有正反两面。

请随意在这里添加建议。:)


当前回答

我真的很喜欢樱桃皮。下面是一个基于rest的web服务的例子:

import cherrypy
from cherrypy import expose

class Converter:
    @expose
    def index(self):
        return "Hello World!"

    @expose
    def fahr_to_celc(self, degrees):
        temp = (float(degrees) - 32) * 5 / 9
        return "%.01f" % temp

    @expose
    def celc_to_fahr(self, degrees):
        temp = float(degrees) * 9 / 5 + 32
        return "%.01f" % temp

cherrypy.quickstart(Converter())

这强调了我真正喜欢CherryPy的地方;这是一个完全可行的例子,即使对不了解框架的人来说也是可以理解的。如果你运行这段代码,你可以立即在浏览器中看到结果;例如,访问http://localhost:8080/celc_to_fahr?degrees=50将在您的web浏览器中显示122.0。

其他回答

下面是CherryPy文档中关于REST的讨论:http://docs.cherrypy.org/dev/progguide/REST.html

它特别提到了一个内置的CherryPy dispatcher,称为MethodDispatcher,它基于http -动词标识符(GET, POST等…)调用方法。

看一看

Itty(博客文章) 瓶 web.py 朱诺

没想到居然没人提到烧瓶。

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

我不是python世界的专家,但我一直在使用django,这是一个优秀的web框架,可以用来创建一个restful框架。

我强烈推荐TurboGears或Bottle:

TurboGears:

比django更简洁 更灵活,更少面向html 但是:不太出名

瓶:

非常快 非常容易学 但是:简约而不成熟