Eve库简单教程
文章目錄
- 1. 簡(jiǎn)介
- 2. 使用實(shí)例
- 2.1 hello world
- 2.2 支持認(rèn)證功能
- 2.3 支持schema 校驗(yàn)
1. 簡(jiǎn)介
Eve是一個(gè) open source 為人類設(shè)計(jì)的python rest api框架。它允許輕松地構(gòu)建和部署高度可定制、功能齊全的RESTful Web服務(wù)。
Eve由 Flask 和 Cerberus 它提供本地支持 MongoDB 數(shù)據(jù)存儲(chǔ)。社區(qū)提供對(duì)SQL、ElasticSearch和Neo4JS后端的支持 extensions.
代碼庫(kù)在python 2.7、3.5+和pypy下進(jìn)行了徹底的測(cè)試。
2. 使用實(shí)例
2.1 hello world
from eve import Evesettings = {'DOMAIN': {'people': {}}}app = Eve(settings=settings) app.run()啟動(dòng)該程序,發(fā)送請(qǐng)求
$ curl -i http://example.com/people HTTP/1.1 200 OK2.2 支持認(rèn)證功能
from eve import Eve from eve.auth import BasicAuthclass MyBasicAuth(BasicAuth):def check_auth(self, username, password, allowed_roles, resource, method):return username == 'admin' and password == 'secret'app = Eve(auth=MyBasicAuth)if __name__ == '__main__':app.run()2.3 支持schema 校驗(yàn)
>>> schema = {'name': {'type': 'string'}, 'age': {'type': 'integer', 'min': 10}} >>> document = {'name': 'Little Joe', 'age': 5} >>> v.validate(document, schema) False >>> v.errors {'age': ['min value is 10']}>>> v = Validator() >>> v.require_all False>>> schema = { ... 'name': {'type': 'string'}, ... 'a_dict': { ... 'type': 'dict', ... 'require_all': True, ... 'schema': { ... 'address': {'type': 'string'} ... } ... } ... }>>> v.validate({'name': 'foo', 'a_dict': {}}, schema) False >>> v.errors {'a_dict': [{'address': ['required field']}]}>>> v.validate({'a_dict': {'address': 'foobar'}}, schema) True總結(jié)
- 上一篇: HTML中jquery轮播图旋转,jqu
- 下一篇: ABB基础学问:IRB1200详尽介绍