爲了測試燒瓶中的應用程序,我得到了與文件作爲附件如何使用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