我可以在Flask中提供一个包含多个部分的http响应吗? [英] Can I serve a multipart http response in Flask?

查看:53
本文介绍了我可以在Flask中提供一个包含多个部分的http响应吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个多部分http响应,类似于表单可以为文件上传产生的多部分http请求.它包含多个数据段,每个数据段都有自己的内容类型.当我在Google上搜索时,我发现的只是有关流式传输的信息.

I want to do a multipart http response similar to the multipart http requests that forms can produce for file uploads. It would contain multiple data segments, each with its own content type. When I google this, all I find is information on streaming.

我不在乎浏览器是否支持此功能,因为它是针对使用libcurl的客户端的.但是,我不确定libcurl是否也支持多部分响应.可以?分部回应是您可以做的事情吗?

I don't care if browsers support this, since it's for a client that is using libcurl. However, I'm not sure if libcurl supports multipart responses either. Does it? Are multipart responses a thing you can do?

推荐答案

以其他答案为基础,并使用

Building on the other answers, and using the requests toolbelt library, the code would look something like the following:

from flask import Flask, Response
from requests_toolbelt import MultipartEncoder

app = Flask(__name__)

@app.route('/downloads')
def downloads():
    m = MultipartEncoder(
           fields={'field0': 'value', 'field1': 'value',
                   'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
        )
    return Response(m.to_string(), mimetype=m.content_type)

这篇关于我可以在Flask中提供一个包含多个部分的http响应吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆