为了测试烧瓶中的应用程序,我得到了与文件作为附件如何使用Flask测试客户端发布多个文件?
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请求烧瓶服务器端点编码是这样的
@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', <FileStorage: u'Single_XML.xml' ('application/xml')>)])
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', <FileStorage: u'Single_XML.xml' ('application/xml')>), ('file', <FileStorage: u'Second_XML.xml' ('application/xml')>)])
Upload Files:
[]
问:为什么 request.files.getlist( “文件[]” )正在返回一个空列表? 如何使用瓶子测试客户端发布多个文件,以便它可以在瓶子服务器端使用request.files.getlist(“file []”)检索?
注:
- 我想有烧瓶的客户,我不想卷曲或任何其他客户端的解决方案。
- 我不想上传单个文件的多个请求
感谢
提到这些链接已经:
Flask and Werkzeug: Testing a post request with custom headers
Python - What type is flask.request.files.stream supposed to be?
谢谢。是。在浏览代码后,对于“getlist”,我昨天晚上意识到这一点,getList返回multiDict中给定的键。由于缺乏对此的理解,我使用了错误的键名。但是你确实回答了我的问题,因此会接受你的问题。还发布了我在其他答案中收集的信息。 – user2390183