Cloudant-Python 是 CouchDB 和 Cloudant 实例的 Python 异步接口。封装了请求来处理头、JSON 序列化、流响应体等等功能的样板,例如:
import cloudant # connect to https://garbados.cloudant.com account = cloudant.Account('garbados', async=True) # and https://garbados.cloudant.com/allyourbase database = account.database('allyourbase') # create the database future = database.put() response = future.result() # throw an error if the response code indicates failure response.raise_for_status() print database.get().result().json() # { "db_name": "allyourbase", ... }
异步 HTTP 请求返回 Future 对象等待 HTTP 响应,可调用 Response 对象的 result() 方法来获取结果。如果你没有传递 async=True 则执行同步请求:
async=True
import cloudant # connect to http://localhost:5984 account = cloudant.Account() response = account.get() print response.json() # { "couchdb": "Welcome", ... }
获取 Cloudant-Python 的方法:
pip install cloudant