【问题标题】:how to attach a BytesIO object to a Webob.Response Object如何将 BytesIO 对象附加到 Webob.Response 对象
【发布时间】:2016-03-16 18:21:05
【问题描述】:

我将 Webob.Response 对象从我的服务器返回到 http 请求。服务器将一个 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')

但这会引发错误:

init 中的文件“/home/abdul/abdul/scripting-120218/local/lib/python2.7/site-packages/webob/response.py”,第 147 行 self._headerlist.append(('Content-Length', str(len(body)))) TypeError: '_io.BytesIO' 类型的对象没有 len()

【问题讨论】:

    标签: python webob bytesio


    【解决方案1】:

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

    app_iter = FileIter(BytesIO_Object.read())
    

    【讨论】:

      猜你喜欢
      • 2015-11-19
      • 2014-10-05
      • 2012-05-21
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多