关于request库的基本用法请参考官方文档:Quickstart — Requests 2.26.0 documentation
自动化测试的项目中有一个功能是服务器需要向客户端发送一个超过1G的文件, 使用request.post来发送文件, 但是如果设置了timeout的参数,很可能会引发“requests.exceptions.ReadTimeout”,
实列代码如下:
1.使用FastApi 创建一个接收文件的接口, 将下面代码运行起来
FastApi 文档链接第一步 - FastAPIhttps://fastapi.tiangolo.com/zh/tutorial/first-steps/
import time
import uvicorn
from fastapi import FastAPI,File
app = FastAPI()
@app.post("/test_file")
async def test_file(file_io: UploadFile = File(...)):
# 此接口被调用时该文件已接收完毕,可以存在内存中,也可以是磁盘上临时文件
size = 1024 * 1024
print(size)
start = time.time()
with open(file_io.filename, 'wb') as f:
while True:
content = await file_io.read(size)
if content:
f.write(content)
else:
break
await file_io.close()
print(size, file_io.filename)
return {'filename': file_io.filename, "cost_time": time.time() - start}
if __name_