我们的一些用户要求我们在我们发送请求的HTTP标头中包含与其帐户相关的数据,甚至是我们从API获得的响应.在命名,格式化等方面添加自定义HTTP标头的一般惯例是什么?
另外,您可以随意发布您在网上偶然发现的任何智能用途; 我们正试图用最好的目标来实现这个目标:)
我想问一个关于这个的问题multipart/form-data.在HTTP标头中,我发现了Content-Type: multipart/form-data; boundary=???.
是???免费的用户定义?或者它通常来自HTML?我可以定义??? = abcdefg吗?
如何multipart/form-data在python中发送请求?如何发送文件,我理解,但如何通过这种方法发送表单数据无法理解.
我有以下代码:
import time
from fastapi import FastAPI, Request
app = FastAPI()
@app.get("/ping")
async def ping(request: Request):
print("Hello")
time.sleep(5)
print("bye")
return {"ping": "pong!"}
Run Code Online (Sandbox Code Playgroud)
如果我在本地主机上运行我的代码 - 例如http://localhost:8501/ping- 在同一浏览器窗口的不同选项卡中,我得到:
Hello
bye
Hello
bye
Run Code Online (Sandbox Code Playgroud)
代替:
Hello
Hello
bye
bye
Run Code Online (Sandbox Code Playgroud)
我已经阅读过有关使用的内容httpx,但仍然无法实现真正的并行化。有什么问题?
python asynchronous concurrent-processing python-asyncio fastapi
我们正在使用 Python FastAPI 编写一个 Web 服务,该服务将托管在 Kubernetes 中。出于审计目的,我们需要保存特定路由的request/的原始 JSON 正文。JSON的主体大小约为1MB,最好这不应该影响响应时间。我们怎样才能做到这一点?responserequestresponse
我试图计算出我的客户端可以上传的最大文件大小,以便我的 python fastapi 服务器可以毫无问题地处理它。
我通过 POST 接受该文件。本地保存时,可以使用file.read()读取内容,但是显示通过file.name不正确(16)的名称。当我尝试按此名称查找它时,出现错误。可能是什么问题?
我的代码:
@router.post(
path="/po/{id_po}/upload",
response_model=schema.ContentUploadedResponse,
)
async def upload_file(
id_po: int,
background_tasks: BackgroundTasks,
uploaded_file: UploadFile = File(...)):
"""pass"""
uploaded_file.file.rollover()
uploaded_file.file.flush()
#shutil.copy(uploaded_file.file.name, f'/home/fyzzy/Desktop/api/{uploaded_file.filename}')
background_tasks.add_task(s3_upload, uploaded_file=fp)
return schema.ContentUploadedResponse()
Run Code Online (Sandbox Code Playgroud) 我根据官方文档使用fastapi上传文件,就像:
@app.post("/create_file/")
async def create_file(file: UploadFile=File(...)):
file2store = await file.read()
# some code to store the BytesIO(file2store) to the other database
Run Code Online (Sandbox Code Playgroud)
当我使用 python requests lib 发送请求时:
f = open(".../file.txt", 'rb')
files = {"file": (f.name, f, "multipart/form-data")}
requests.post(url="SERVER_URL/create_file", files=files)
Run Code Online (Sandbox Code Playgroud)
file2store 始终为空。有时(很少见),它可以获取文件字节,但几乎所有时间都是空的,所以我无法在另一个数据库上恢复文件。我还尝试了 'bytes' 而不是 'UploadFile',我得到了相同的结果。我的代码有什么地方不对,还是我使用fastapi上传文件的方式有问题?我google了很长时间,但没有成功。所以我在这里提出问题,希望知道答案的人可以帮助我。谢谢
我创建了一个端点,如下所示:
@app.post("/report/upload")
def create_upload_files(files: UploadFile = File(...)):
try:
with open(files.filename,'wb+') as wf:
wf.write(file.file.read())
wf.close()
except Exception as e:
return {"error": e.__str__()}
Run Code Online (Sandbox Code Playgroud)
它是用 uvicorn 启动的:
../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload
Run Code Online (Sandbox Code Playgroud)
我正在执行一些测试,使用 Python 请求上传大约100 MB的文件,大约需要 128 秒:
../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload
Run Code Online (Sandbox Code Playgroud)
我使用 Flask 通过 API 端点测试了相同的上传脚本,大约需要 0.5 秒:
f = open(sys.argv[1],"rb").read()
hex_convert = binascii.hexlify(f)
items = {"files": hex_convert.decode()}
start = time.time()
r = requests.post("http://192.168.0.90:5000/report/upload",files=items)
end = time.time() - start
print(end)
Run Code Online (Sandbox Code Playgroud)
我做错了什么吗?
我想在 FastAPI 中创建一个可能接收(多部分)Form数据或JSON正文的端点。有没有办法让这样的端点接受或者检测正在接收哪种类型的数据?
python ×8
fastapi ×7
http ×3
file-upload ×2
starlette ×2
asynchronous ×1
backend ×1
forms ×1
html ×1
http-headers ×1
json ×1
logging ×1
python-2.7 ×1
server-side ×1
upload ×1