Skip to main content
 首页 » 编程设计

python之如何将 BytesIO 对象附加到 Webob.Response 对象

2025年04月02日43lvdongjie

我正在从我的服务器向 http 请求返回一个 Webob.Response 对象。服务器将一个 BytesIO 对象放在一起。如何正确地将 BytesIO 对象附加到 Webob.Response 对象?

我试过:

app_iter = FileIter(BytesIO_Object) 
return Response( 
                app_iter=app_iter, 
                last_modified=last_modified, 
                content_length=content_length, 
                content_type=content_type, 
                content_encoding=content_encoding, 
                content_disposition=content_disposition, 
                accept_ranges=accept_ranges, 
                conditional_response=True) 

运气不好,当我在客户端打印 response.content 时,它只是空的

我也试过:

return Response(self.file, '200 ok', content_type='text/html') 

但这会引发错误:

File "/home/abdul/abdul/scripting-120218/local/lib/python2.7/site-packages/webob/response.py", line 147, in init self._headerlist.append(('Content-Length', str(len(body)))) TypeError: object of type '_io.BytesIO' has no len()

请您参考如下方法:

好吧,终于明白了。您可以将其传递给 FileIter

app_iter = FileIter(BytesIO_Object.read())