问题描述
我有一个 api,其中服务器向客户端发送数据流。在服务器端,填充了流对象。我可以通过在发送之前写入服务器上的文件来验证这一点。但是当客户端收到流并尝试将其写入内存时,它总是导致一个空文件。这是我的代码
TL;DR: Stream 从 api 发送时不为空,但当客户端接收并尝试写入时为空
服务器端 - 发送 io.BytesIO
对象
from io import BytesIO
from fastapi import FastAPI
app = FastAPI()
# capture image and store in io.BytesIO stream
def pic_to_stream(self):
io = BytesIO()
self.c.capture(io,'jpeg') # populate stream with image
return io
# api endpoint to send stream
@app.get('/dropcam/stream')
def dc_stream():
stream,ext = cam.pic_to_stream()
# with open('example.ext','wb') as f:
# f.write(stream.getbuffer()) # this results in non-empty file on server
return StreamingResponse(stream,media_type=ext)
客户端 - 接收 io.BytesIO
对象并尝试写入
import requests
def recieve_stream():
url = 'http://abc.x.y.z/dropcam/stream'
local_filename = 'client/foo_stream.jpeg'
with requests.get(url,stream=True) as r:
with open(local_filename,'wb') as f:
f.write(r.content) # this results in an empty file on client
有人有什么想法吗?我看过 these links 和 more 没有运气。谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)