首页 > 编程语言 > 详细

python max api

时间:2020-01-16 18:01:13      阅读:67      评论:0      收藏:0      [点我收藏+]
# -*- coding: utf-8 -*-

接口名称:
python3 max
接口描述:

接口地址:http://127.0.0.1:8000/test_max/
返回格式:json
请求方式: post

请求参数说明(入参):
name:姓名
age:年龄

名称 必填 类型 说明
key 是 string 姓名全名
qq 是 string 身份证的年龄

返回参数说明(出参):

名称 类型 说明
error_code int 返回状态码
reason string 返回原因
result string 返回实体内容(包含如下)
name string 年龄最大的那位姓名
age_max string 年龄最大的值

JSON返回示例:

{
"error_code": 0,//返回状态码
"reason": "success",//返回原因
"result": {//返回实体内容
"data": {
"name": "xiaoming",
"age_max": "18",
}
}
}


python后端代码编写如下:
# -*- coding:UTF-8 -*-
import json

def max_lgh(http_data):
try:
http_request_value_list = http_data[‘data‘]
except Exception as e:
msg = ‘获取数据失败{0}‘.format(e)
response_data = {"error_code": 500,"reason": "failed","message": msg}
return json

#获取年龄最大的值,传值示例: http_request_value_list = [{‘name‘: ‘li‘, ‘age‘: 24},{‘name‘: ‘he‘, ‘age‘: 45} ]
b = max(http_request_value_list, key=lambda x: x[‘age‘])
response_data = {"error_code": 0,"reason": "success","result": {"data": b}}
# 返回json字符串
json_dump = json.dumps(response_data)
return json_dump


Python代码请求示例:
# -*- coding:UTF-8 -*-
import requests

url = "http://127.0.0.1:8000/test_max/"

par = {"data":
[ {"name": "xiaoming",
"age": "18"
},
{"name": "xiaogan",
"age": "14"
},]
}
r = requests.get(url, params=par)
print(r.text) # 打印文本
res = r.json() # 返回的是json,用r.json解析器转成字典
# 字典取某个字段
conclusion = res["result"]["data"]
print(conclusion)

python max api

原文:https://www.cnblogs.com/lghlxy/p/12202463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!