亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

基于python的REST框架eve測試與mongodb的數據操作

標簽:
MongoDB


Eve是一款Python的REST API框架,用于构建和部署高可定制的、全功能的RESTful的Web服务。Eve是一个开源项目,遵循BSD开源协议,已在Python 2.6、2.7以及Python 3.3版本下进行了非常全面的测试。

特色

强调REST

非常全面的CRUD操作

可自定义的资源端点

自定义多个项目端点

筛选和排序

分页

HATEOAS

JSON和XML渲染

条件请求

数据完整性和并发控制

多个添加操作

数据验证

可扩展的数据验证

资源级缓存控制

版本

验证

CORS跨地资源共享

默认情况下只读

默认值

预定义的数据库过滤器

预测

事件关联

速率限制

MongoDB支持

和别的框架一样,咱们需要安装eve的模块

113532639.jpg

直接yum安装吧,我自己选择mongodb的时候,都会用10gen的。

cat /etc/yum.repos.d/10gen.repo

[10gen]

name=10gen Repository

baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64

gpgcheck=0

开始安装吧~

113307581.jpg

好了,咱们现在就测试下吧:

启动server.py还需要做的就是配上一个数据库、配置文件(默认是settings.py)、启动脚本来让API可以在线使用。总之,你会发现,配置和调优API是一件非常简单的事情。

server端

115007669.jpg

客户端

115032430.jpg

官方还推荐了一个eve demo

https://github.com/nicolaiarocci/eve-demo

115802876.jpg

demo里面有几个实例 ~

返回是可以定义的:

xml

curl -H "Accept: application/xml" -i "http://127.0.0.1:5000/"

json

curl -H "Accept: application/json" -i "http://127.0.0.1:5000/"

121116936.jpg

在这定义规则啥的 ~

Here is how the complete people definition looks in our updated settings.pyfile:

people = {

    # 'title' tag used in item links. Defaults to the resource title minus

    # the final, plural 's' (works fine in most cases but not for 'people')

    'item_title': 'person',

    # by default the standard item entry point is defined as

    # '/people/<ObjectId>'. We leave it untouched, and we also enable an

    # additional read-only entry point. This way consumers can also perform

    # GET requests at '/people/<lastname>'.

    'additional_lookup': {

        'url': '[\w]+',

        'field': 'lastname'

    },

    # We choose to override global cache-control directives for this resource.

    'cache_control': 'max-age=10,must-revalidate',

    'cache_expires': 10,

    # most global settings can be overridden at resource level

    'resource_methods': ['GET', 'POST'],

    'schema': schema

}

插入数据~

curl -d '[{"firstname": "rui", "lastname": "fengyun"}, {"firstname": "li", "lastname": "liying"}]' -H 'Content-Type: application/json'  http://127.0.0.1:5000/people

查询数据~

curl -i http://eve-demo.herokuapp.com/people?where={"lastname": "liying"}

124028547.jpg

我们从mongo的cli中瞅瞅,看看数据

125358837.jpg

In the above response, a Last-Modified header is included. It can be used later to retrieve only the items that have changed since:

curl -H "If-Modified-Since: Wed, 05 Dec 2012 09:53:07 UTC" -i http://127.0.0.1:5000/people/

eve 其实在一定程度上可以理解为mongodb的http接口 就是redis的webdis一样的

类似mysql的取出lastname字段为Doe的数据

$ curl -i http://eve-demo.herokuapp.com/people?where={"lastname": "Doe"}

and the native Python syntax:

$ curl -i http://eve-demo.herokuapp.com/people?where=lastname=="Doe"

排序的方式

$ curl -i http://eve-demo.herokuapp.com/people?sort=[("lastname", -1)]

可以做出mongodb limit 的效果

$ curl -i http://eve-demo.herokuapp.com/people/?where={"lastaname": "Doe"}&sort=[("firstname",1)]&page=5

多次插入数据

$ curl -d 'item1={"firstname": "barack", "lastname": "obama"}' -d 'item2={"firstname": "mitt", "lastname": "romney"}' http://127.0.0.1/people

Response:

{

    [

        "status": "OK",

        "updated": "Thu, 22 Nov 2012 15:22:27 UTC",

        "_id": "50ae43339fa12500024def5b",

        "_links": {"self": {"href": "eve-demo.herokuapp.com/people/50ae43339fa12500024def5b", "title": "person"}}

    ],

    [

        "status": "OK",

        "updated": "Thu, 22 Nov 2012 15:22:27 UTC",

        "_id": "50ae43339fa12500024def5c",

        "_links": {"self": {"href": "eve-demo.herokuapp.com/people/50ae43339fa12500024def5c", "title": "person"}}

    ]

}

总结: eve是个强大又可以扩展res框架,也可以把他用为mongodb的http接口 ~

我对他的理解还是有些片面,有时间再好好看看。

©著作权归作者所有:来自51CTO博客作者rfyiamcool的原创作品,谢绝转载,否则将追究法律责任

python eveeve restfulpython mongodbpython应用


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消