流式传输的响应 [200 OK]> 不是 JSON 可序列化流图像 python

Response streamed [200 OK]> is not JSON serializable stream image python

我正在寻找一种方法来提供来自 bing 地图的一些图像,而不共享 URL(因为它包含密钥)和 return 它给客户端(无需先保存到磁盘)。我正在使用 here

中的这种方法
def get_stream_from_url(url):
    req = requests.get(url, stream = True)
    return Response(stream_with_context(req.iter_content()), content_type = req.headers['content-type'])

我在另一种方法中使用它,我在其中生成包含这些图像信息的列表,所以基本上我想将 sream 响应放在 json 中作为键 url 的值:

   def images_data_and_url(self, data):
        results = []
        for i in range(1, 13):
            image = {}
            description = data['description']
            url = self.get_stream_from_url(data['url'])
            tile = {"description": description
                    "url": url}
           results.append(image)
        print results
        return json.dumps(results)

编辑: 如果我打印结果我有这个:

<type 'list'>: [{'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>} , {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>},{'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}]

如果我这样做,我会得到这个错误

<Response streamed [200 OK]> is not JSON serializable

这是因为 get_stream_from_url return 函数的响应:/ 我的问题是我不知道如何用另一种方式发送它?

在 HTML 中,我获取数据并为每个图像分配数据,如下所示:

<img class="image1 " data-description="The description" src="">

有人可以帮助我,如何对流进行编码以便我可以将其与 JSON 数据一起发送?

Flask 太过分了。我创建了一个名为 wsinfo 的库,可在此处获取:https://github.com/linusg/wsinfo.

有了它,代码就这么简单:

import base64
import wsinfo
import sys


def html_image(data, content_type):
    if sys.version.startswith("3"):
        from io import BytesIO
        f = BytesIO(data.encode())
    else:
        from StringIO import StringIO
        f = StringIO(data)

    encoded = base64.b64encode(f.read())
    return "data:{};base64,{}".format(content_type, encoded)

url = "http://t0.tiles.virtualearth.net/tiles/a1210223003.jpeg?g=854&mkt=en-US"
w = wsinfo.Info(url)
img = html_image(w.content, w.content_type)

with open("out.html", "w") as html:
    html.write('<img class="image1" data-description="The description" src="{}" />'.format(img))

代码与 Python 2 和 3 一起运行,并生成一个名为 out.html 的 HTML 文件,其中嵌入了图像。

无需隐藏 Bing 地图键。任何人都可以在免费使用条款下创建和使用它。因此,他们几乎没有动机窃取您的密钥。在大多数情况下,密钥似乎被盗,根本原因是交出了包含密钥的代码,或者密钥被发布在论坛帖子的代码示例中。密钥的用户通常不知道代码中的密钥。 Bing 地图团队能够跟踪这些情况并根据需要解决它们。也就是说它很少发生。通常,当有人想窃取 Bing 地图密钥时,他们只需使用 Bing 地图网站上的密钥即可。