如何使用Flask测试客户端发布多个文件?

 mobiledu2502863347 发布于 2023-02-11 17:40

为了测试Flask应用程序,我得到了一个烧瓶测试客户端POST请求,文件作为附件

def make_tst_client_service_call1(service_path, method, **kwargs):
    _content_type = kwargs.get('content-type','multipart/form-data')
    with app.test_client() as client:
        return client.open(service_path, method=method,
                           content_type=_content_type, buffered=True,               
                                             follow_redirects=True,**kwargs)

def _publish_a_model(model_name, pom_env):
    service_url = u'/publish/'
    scc.data['modelname'] = model_name
    scc.data['username'] = "BDD Script"
    scc.data['instance'] = "BDD Stub Simulation"
    scc.data['timestamp'] = datetime.now().strftime('%d-%m-%YT%H:%M')
    scc.data['file'] = (open(file_path, 'rb'),file_name)
    scc.response = make_tst_client_service_call1(service_url, method, data=scc.data)

处理上述POST请求的Flask Server端点代码是这样的

@app.route("/publish/", methods=['GET', 'POST'])
def publish():
    if request.method == 'POST':
        LOG.debug("Publish POST Service is called...")
        upload_files = request.files.getlist("file[]")
        print "Files :\n",request.files
        print "Upload Files:\n",upload_files
        return render_response_template()

我得到了这个输出

Files:
ImmutableMultiDict([('file', )])

Upload Files:
[]

如果我改变

scc.data['file'] = (open(file_path, 'rb'),file_name)

进(认为它会处理多个文件)

scc.data['file'] = [(open(file_path, 'rb'),file_name),(open(file_path, 'rb'),file_name1)]

我仍然得到类似的输出:

Files:
ImmutableMultiDict([('file', ), ('file', )])

Upload Files:
[]

问题:为什么request.files.getlist("file []")返回一个空列表?如何使用flask测试客户端发布多个文件,以便可以 在flask服务器端使用request.files.getlist("file []")检索它?

注意:

我想有烧瓶客户端我不想要卷曲或任何其他基于客户端的解决方案.

我不想在多个请求中发布单个文件

谢谢

已经提到这些链接:

Flask和Werkzeug:使用自定义标头测试发布请求

Python - flask.request.files.stream应该是什么类型的?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有