【发布时间】:2013-12-28 06:37:54
【问题描述】:
为了测试 Flask 应用程序,我收到了一个 Flask 测试客户端 POSTing 请求,其中包含文件作为附件
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', <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("file[]") 返回一个空列表? 如何使用烧瓶测试客户端发布多个文件,以便可以在烧瓶服务器端使用 request.files.getlist("file[]") 检索它?
注意:
- 我想要一个烧瓶客户端,我不想要 curl 或任何其他基于客户端的解决方案。
- 我不想在多个请求中发布单个文件
谢谢
已经引用了这些链接:
Flask and Werkzeug: Testing a post request with custom headers
Python - What type is flask.request.files.stream supposed to be?
【问题讨论】:
标签: python post flask werkzeug