需要做网站建设和推广,wordpress火车头但存图片,网站开发工程师待遇,设计平台兼职概述
zdppy_api是一款为了快速开发而生的#xff0c;基于异步的#xff0c;使用简单的Python后端API接口开发框架。 本框架的目标是让Python后端开发变得越来越简单#xff0c;直到发现原来还可以更简单#xff01; 一切都是为了提高开发效率#xff01;#xff01;基于异步的使用简单的Python后端API接口开发框架。 本框架的目标是让Python后端开发变得越来越简单直到发现原来还可以更简单 一切都是为了提高开发效率
响应文本
服务端
from api import resp, Api# 创建应用
app Api(routes[resp.text_route(/, 你好张大鹏)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqprint(req.get(http://127.0.0.1:8000/).text)
响应JSON
服务端
from api import resp, Api# 创建应用
app Api(routes[resp.json_route(/, {msg: 你好张大鹏})])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqprint(req.get(http://127.0.0.1:8000/).json())响应文件
准备data/test.txt文件
a
b
c
123服务端
from api import resp, Api# 创建应用
app Api(routes[resp.dir_route(/, data)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqprint(req.get(http://127.0.0.1:8000/test.txt).text)获取查询参数
服务端
from api import req, resp, Apiasync def index(request):生成路由方法name req.get_query(request, name)return resp.json({name: name})# 创建应用
app Api(routes[resp.route(/, index)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqprint(req.get(http://127.0.0.1:8000/?namezhang大鹏).json())
获取路径参数
服务端
from api import req, resp, Apiasync def index(request):生成路由方法a req.get_path(request, a)b req.get_path(request, b)c req.get_path(request, c)d req.get_path(request, d)return resp.json({a: a, b: b, c: c, d: d})# 创建应用
app Api(routes[resp.route(/{a}/{b:int}/{c:float}/{d:path}, index)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqprint(req.get(http://127.0.0.1:8000/hello/11/22.22/33/44.txt).json())
获取请求头参数
服务端
from api import req, resp, Apiasync def index(request):生成路由方法token req.get_header(request, Token)user_agent req.get_header(request, User-Agent)return resp.json({token: token, user_agent: user_agent})# 创建应用
app Api(routes[resp.route(/, index)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqheaders {Token: xxx,User-Agent: abc
}
print(req.get(http://127.0.0.1:8000/, headersheaders).json())
获取文本类型的参数
服务器
from api import req, resp, Apiasync def index(request):生成路由方法data await req.get_text(request)return resp.json({data: data})# 创建应用
app Api(routes[resp.post(/, index)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqheaders {Content-Type: application/text}
print(req.post(http://127.0.0.1:8000/, datahello,world, headersheaders).json())获取JSON数据
服务端
from api import req, resp, Apiasync def index(request):生成路由方法data await req.get_json(request)return resp.json({data: data})# 创建应用
app Api(routes[resp.post(/, index)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqdata {name: zhangsan, age: 33}
print(req.post(http://127.0.0.1:8000/, datadata).json())
print(req.post(http://127.0.0.1:8000/, jsondata).json())文件上传
服务端
from api import req, resp, Api# 创建应用
app Api(routes[resp.upload(/upload, data)])if __name__ __main__:import uvicornuvicorn.run(server:app)
客户端
import reqfiles {file: open(server.py)}
print(req.post(http://127.0.0.1:8000/upload, filesfiles).json())