2016-09-28 20 views
0

我正在尋找一種方式來從bing地圖提供一些圖像而不共享URL(因爲它包含密鑰)並將其返回給客戶端(不先保存到磁盤)。我用這個方法從here響應流[200 OK]>不是JSON可串行化流圖像python

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']) 

我使用的另一種方法,我在那裏生成對於那些像那些信息的列表,所以基本上我婉把JSON內sream響應,值關鍵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]>}] 

如果我不喜歡這個,我得到這個呃ROR

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

因爲get_stream_from_url返回響應函數的這個:/我的問題是,我不知道如何以另一種方式把它?

在HTML中我得到的數據,每個數據分配爲這樣的每個圖像:

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

有人可以幫我,如何將流編碼,所以我可以把它作爲與JSON數據?

+0

當你在'images_data_and_url'函數的'return'之前執行'print(results)'時,看起來它是什麼樣子? – linusg

+0

謝謝,我明白了。正如你可能已經注意到的那樣,這些網址很棒,所以我會從那裏開始。也許你做得太複雜了,這就是爲什麼它打破了... – linusg

+0

@linusg網址是好的,因爲我測試了它? – Ritchie

回答

1

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文件,其中,所述圖像嵌入的結果:

有了它,作爲代碼是一樣簡單。

+0

你真的救了我的一天!非常感謝你! – Ritchie

0

沒有必要隱藏Bing地圖密鑰。任何人都可以在免費條款下創建和使用它。因此,他們沒有什麼動機竊取你的鑰匙。在大多數情況下,一個密鑰似乎被盜,根本原因是代碼被移交,其中包含的密鑰或密鑰張貼在論壇帖子的代碼示例中。密鑰的用戶通常不知道代碼中的密鑰。必應地圖團隊能夠跟蹤這些情況並根據需要解決它們。這就是說很少發生。通常,當有人想要盜取Bing地圖密鑰時,他們只需使用Bing地圖網站上的密鑰。