【问题标题】:How to read content from http 200 response from server built using python flask? [duplicate]如何从使用 python 烧瓶构建的服务器的 http 200 响应中读取内容? [复制]
【发布时间】:2018-05-08 12:38:33
【问题描述】:

我使用 python 烧瓶函数 send_file 将图像文件从服务器发送到客户端。我从服务器收到 Http 200 响应,其中包含内容信息的标头,response.text 返回字节字符串。目前服务器和客户端都在本地系统中。我想访问该文件并将其保存在客户端。 服务器代码:

@app.route("/ss", methods=["POST"])
def ss():
    return send_file("chap.jpg",attachment_filename="chap.jpg")

客户端代码:

payload = {"image": image}
r = requests.post(http://127.0.0:5000/ss, files=payload)
print(r.text)

我尝试将字符串从 r.text 转换为 numpy 数组并将数组另存为 jpg,但我收到 TypeError 错误参数。

【问题讨论】:

    标签: python rest flask


    【解决方案1】:

    您需要访问binary content of the response 并将其保存到文件中。

    with open('myfile.jpg', 'wb') as f:
        f.write(r.content)
    

    【讨论】:

    • 解决方案完美运行。谢谢
    【解决方案2】:

    您要上传文件吗? {"image" : image is not a solution do like me :

    import requests
    
    image = 'image.jpg'
    
    payload = {'image':(image, open(image, 'r'),'multipart/from-data')},
    
    url = 'http://127.0.0:5000/ss'
    
    r = requests.post(url, files=payload)
    
    print r.text
    

    【讨论】:

    • 不,我要下载服务器发来的文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多