小编use*_*183的帖子

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

为了测试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)
Run Code Online (Sandbox Code Playgroud)

处理上述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()
Run Code Online (Sandbox Code Playgroud)

我得到了这个输出

Files:
ImmutableMultiDict([('file', <FileStorage: u'Single_XML.xml' …
Run Code Online (Sandbox Code Playgroud)

python post werkzeug flask

9
推荐指数
1
解决办法
4896
查看次数

标签 统计

flask ×1

post ×1

python ×1

werkzeug ×1