Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python flask应用程序未处理错误输入,未在postman中显示错误消息_Python_Flask - Fatal编程技术网

Python flask应用程序未处理错误输入,未在postman中显示错误消息

Python flask应用程序未处理错误输入,未在postman中显示错误消息,python,flask,Python,Flask,我有一个flask应用程序,正在用python进行测试。我发送了2个文件,如果其中一个没有给出-它应该给我json错误消息“需要2个文件” 相反,它会像这样崩溃: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>werkze

我有一个flask应用程序,正在用python进行测试。我发送了2个文件,如果其中一个没有给出-它应该给我json错误消息“需要2个文件”

相反,它会像这样崩溃:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'file1' // Werkzeug Debugger</title>

werkzeug.exceptions.BadRequestKeyError:400错误请求:浏览器(或代理)发送了此服务器无法理解的请求。
KeyError:'file1'//Werkzeug调试器

但是我想处理这个案例,只需查看错误消息“需要2个文件”

实际上,这段代码中有两个问题,错误是:
1.其中一个参数文件不存在
2.其中一个文件为空(param exist,但未选择任何文件,应在前端实际处理)


这应该可以解决它,注意这两种情况

此代码中有两个错误,排序结果是当
file2=request.files[“file2”]
在参数中不存在时,它不是
None
,它会崩溃,添加
try:。。。除了:…
文件1=。。。file2=…异常应该使用自定义error@Missilexentthx就行了now@Missilexent嗯,它给出了一个错误:{'result':'err','errcode':'ERR001','errtext':'需要2个文件!'//WerkzeugDebugger@Missilexent它仍然显示错误,但右上角的状态为“500服务器错误”它显示正确的错误消息,但仍会因正确的错误消息而崩溃
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'file1' // Werkzeug Debugger</title>
@app.route('/comp', methods = ['POST'])
def compare_voices():
    if request.method == 'POST':
        try:                    
            file1 = request.files["file1"]
            file2 = request.files["file2"]
        except:
            return jsonify({'response':"need 2 files!"})

        if file1.filename == '' or file2.filename == '': 
            answer = "need 2 files!"                    
        else:                                           
            answer = 'ok'

        return jsonify({'response': answer})