Python REST(Web服务)框架的建议? [英] Recommendations of Python REST (web services) framework?
问题描述
是否有列表的某些地方提供了不同的基于Python的REST框架的建议,以供在服务器端使用来编写您自己的RESTful API?最好有正反两面。
请随时在这里添加建议。 :)
在设计RESTful API时需要注意的一点是GET和POST的合并,就好像它们是一样。使用 Django 的基于功能的视图和 CherryPy 的默认调度程序,尽管这两个框架现在都提供了解决此问题的方法(基于类的视图和 MethodDispatcher )。
HTTP在REST中,动词是非常重要的,除非你对此非常小心,否则你最终会陷入一个 REST反模式。
一些正确的框架是 web.py , Flask 和 Bottle 。当与 mimerender 库(完全公开:我写的)结合使用时,它们可以让您编写出漂亮的RESTful Web服务:
import web
从mimerender导入json
导入mimerender
render_xml = lambda message:'< message>%s< / message>'%message
render_json = lambda ** args:json.dumps(args)
render_html = lambda message:'< html>< ; body>%s< / body>< / html>'%message
render_txt = lambda message:message
url =(
'/(.*)', 'greet'
)
app = web.application(urls,globals())
class greet:
@mimerender(
default ='html ',
html = render_html,
xml = render_xml,
json = render_json,
txt = render_txt
)
def GET(self,name):
如果不是名称:
name ='world'
return {'message':'Hello,'+ name +'!'}
if __name__ ==__main__:
app.run()
$ b服务的逻辑只执行一次,正确的表示选择(Accept header)+派发到适当的渲染函数(或模板)
$ curl localhost:8080 / x
< html>< body> ; Hello,x!< / body>< / html>
$ curl -HAccept:application / htmllocalhost:8080 / x
< html>< body> Hello,x!< / body>< / html>
$ curl -HAccept:application / xmllocalhost:8080 / x
< message> Hello,x!< / message>
$ curl -HAccept:application / jsonlocalhost:8080 / x
{'message':'Hello,x!'}
$ curl -H接受:text / plainlocalhost:8080 / x
你好,x!
更新(2012年4月):增加了关于Django基于类的视图的信息,CherryPy的MethodDispatcher和Flask和Bottle框架。问题被问到时都没有存在。
Is there a list somewhere of recommendations of different Python-based REST frameworks for use on the serverside to write your own RESTful APIs? Preferably with pros and cons.
Please feel free to add recommendations here. :)
解决方案Something to be careful about when designing a RESTful API is the conflation of GET and POST, as if they were the same thing. It's easy to make this mistake with Django's function-based views and CherryPy's default dispatcher, although both frameworks now provide a way around this problem (class-based views and MethodDispatcher, respectively).
HTTP-verbs are very important in REST, and unless you're very careful about this, you'll end up falling into a REST anti-pattern.
Some frameworks that get it right are web.py, Flask and Bottle. When combined with the mimerender library (full disclosure: I wrote it), they allow you to write nice RESTful webservices:
import web import json from mimerender import mimerender render_xml = lambda message: '<message>%s</message>'%message render_json = lambda **args: json.dumps(args) render_html = lambda message: '<html><body>%s</body></html>'%message render_txt = lambda message: message urls = ( '/(.*)', 'greet' ) app = web.application(urls, globals()) class greet: @mimerender( default = 'html', html = render_html, xml = render_xml, json = render_json, txt = render_txt ) def GET(self, name): if not name: name = 'world' return {'message': 'Hello, ' + name + '!'} if __name__ == "__main__": app.run()
The service's logic is implemented only once, and the correct representation selection (Accept header) + dispatch to the proper render function (or template) is done in a tidy, transparent way.
$ curl localhost:8080/x <html><body>Hello, x!</body></html> $ curl -H "Accept: application/html" localhost:8080/x <html><body>Hello, x!</body></html> $ curl -H "Accept: application/xml" localhost:8080/x <message>Hello, x!</message> $ curl -H "Accept: application/json" localhost:8080/x {'message':'Hello, x!'} $ curl -H "Accept: text/plain" localhost:8080/x Hello, x!
Update (April 2012): added information about Django's class-based views, CherryPy's MethodDispatcher and Flask and Bottle frameworks. Neither existed back when the question was asked.
这篇关于Python REST(Web服务)框架的建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!