例如,执行curl post api长这个样子:
curl 'http://10.10.10.10:8880' -d '{"args1":"val1","args1":"val2","args3":"val3"}
试了很多种python下进行curl提交的方法,下面这种可行,后续有更好办法再补充:
1. def send_mes(val1, val2, val3):
url = 'http://10.10.10.10:8880'
post_args = {"args1":"%s","args1":"%s","args3":"%s"} %(val1, val2, val3)
post_args_json = json.dumps(post_args) # 对数据进行JSON格式化编码
req = urllib2.Request(url, post_args_json) # 生成页面请求的完整数据
response = urllib2.urlopen(req) # 发送页面请求
rst = json.loads(response.read())
if rst["code"] != 0:
return False
if rst["errorcode"] == 0:
return True