0

我正在使用 Python 和 Bottle 为 Snapchat 开发 API 包装器,但是为了返回文件(由 Python 脚本检索),我必须将字节(由 Snapchat 返回)保存到 .jpg 文件中。我不太确定我将如何做到这一点并且仍然能够访问该文件以便可以将其返回。这是我到目前为止所拥有的,但它返回 404。

@route('/image')
def image():
    username = request.query.username
    token = request.query.auth_token
    img_id = request.query.id
    return get_blob(username, token, img_id)

def get_blob(usr, token, img_id):
    # Form URL and download encrypted "blob"
    blob_url = "https://feelinsonice.appspot.com/ph/blob?id={}".format(img_id)
    blob_url += "&username=" + usr + "&timestamp=" + str(timestamp()) + "&req_token=" + req_token(token)
    enc_blob = requests.get(blob_url).content  
    # Save decrypted image
    FileUpload.save('/images/' + img_id + '.jpg')
    img = open('images/' + img_id + '.jpg', 'wb')
    img.write(decrypt(enc_blob))
    img.close()
    return static_file(img_id + '.jpg', root='/images/')
4

1 回答 1

1

您在编写图像文件时是否有意使用相对路径?(然后文件位置将取决于您程序的当前工作目录。)

也许改变这条线

img = open('images/' + img_id + '.jpg', 'wb')

对此

img = open('/images/' + img_id + '.jpg', 'wb')

会成功吗?

于 2013-10-21T01:47:02.803 回答