最近在优化脚本时,发现要提前执行一段curl命令才行,实际在python中可以通过HTTP Library中requests方法就能实现,如
要执行的curl命令为
curl -v 'http://172.17.9.128:8091/pools/default/buckets/meta3/docs/8024028328' -u "XX:XX"
在Python中直接对它进行请求就可以了
查询接口
def GetMetaData2(cb_url,uid): #"通过CB接口获取uid对应的离线消息表" meta = 'meta' bucket_num = uid%5 bucket_name = meta + str(bucket_num) bucket_passwd = str(123) url = cb_url + bucket_name + "/docs" +'/'+ str(uid) password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, user=bucket_name,passwd=bucket_passwd) handler = urllib.request.HTTPBasicAuthHandler(password_mgr) opener = urllib.request.build_opener(handler) urllib.request.install_opener(opener) f = urllib.request.urlopen(url) res = f.read() print("url:{0},response:{1}".format(url, res)) return f.getcode(), res
得到的结果
url:http://172.17.9.128:8091/pools/default/buckets/meta3/docs/8024028328,response:b'{"meta":{"id":"8024028328","rev":"4667-1677ce237da5000060812e5400000000","expiration":1619078740,"flags":0},"json":{"appkey":"c73fb613f8ef2a5c70396818","ttl":1619078740,"smsgs":{"num":5,"max_count":5,"58546795155930667":{"data_msgtype":2,"push_type":8,"is_vip":1,"ctime":1618988908,"ttl":1619075307,"stime":1618988908},"58546795155930668":{"data_msgtype":2,"push_type":8,"is_vip":1,"ctime":1618988911,"ttl":1619075311,"stime":1618989239},"58546795155930682":{"data_msgtype":2,"push_type":8,"is_vip":1,"ctime":1618988914,"ttl":1619075313,"stime":1618989240},"58546795155930672":{"data_msgtype":2,"push_type":8,"is_vip":1,"ctime":1618988915,"ttl":1619075315,"stime":1618989240},"58546795155930678":{"data_msgtype":2,"push_type":8,"is_vip":1,"ctime":1618988917,"ttl":1619075317,"stime":1618989240}}}}'
删除接口使用delete就行
def Delete_uid_metaall(): resp = requests.delete("http://172.17.9.128:8091/pools/default/buckets/meta3/docs/8024028328", auth=HTTPBasicAuth('XXX', 'XXX')) print(resp)
在查询就没有数据了