Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 如何读取/处理POST请求中发送给Flask API的音频文件?_Python_Http_Flask_Audio_Python Requests - Fatal编程技术网

Python 如何读取/处理POST请求中发送给Flask API的音频文件?

Python 如何读取/处理POST请求中发送给Flask API的音频文件?,python,http,flask,audio,python-requests,Python,Http,Flask,Audio,Python Requests,我正在运行一个简单的Flask后端,它将使用音频文件处理HTTP请求并读取数据。最后,我希望读取数据并让ML模型对音频数据执行推断,但第一步是简单地以正确的编码格式读取数据 我的Flask应用程序代码如下: @app.route('/api/audio', methods=['GET', 'POST']) def get_score(): if request.method == 'POST': length = request.content_length

我正在运行一个简单的Flask后端,它将使用音频文件处理HTTP请求并读取数据。最后,我希望读取数据并让ML模型对音频数据执行推断,但第一步是简单地以正确的编码格式读取数据

我的Flask应用程序代码如下:

@app.route('/api/audio', methods=['GET', 'POST'])
def get_score():
    if request.method == 'POST':
        length = request.content_length
        content_type = request.content_type
        data = request.data
        return f"""Content Type is  {content_type} and data is {data} \n length is {length}"""
    elif request.method == 'GET':
        return 'get method received'
def send_audio():
    #print('attempting to send audio')
    url = 'http://127.0.0.1:5000/api/audio'
    with open('/Users/kaushikandra/laughter-detection/LaughDetection/crowd_laugh_1.wav', 'rb') as file:
        data = {'uuid':'-jx-1', 'alarmType':1, 'timeDuration':10}
        files = {'messageFile': file}

        req = requests.post(url, files=files, json=data)
        print(req.status_code)
        print(req.text)
我在客户端生成POST请求的测试代码如下:

@app.route('/api/audio', methods=['GET', 'POST'])
def get_score():
    if request.method == 'POST':
        length = request.content_length
        content_type = request.content_type
        data = request.data
        return f"""Content Type is  {content_type} and data is {data} \n length is {length}"""
    elif request.method == 'GET':
        return 'get method received'
def send_audio():
    #print('attempting to send audio')
    url = 'http://127.0.0.1:5000/api/audio'
    with open('/Users/kaushikandra/laughter-detection/LaughDetection/crowd_laugh_1.wav', 'rb') as file:
        data = {'uuid':'-jx-1', 'alarmType':1, 'timeDuration':10}
        files = {'messageFile': file}

        req = requests.post(url, files=files, json=data)
        print(req.status_code)
        print(req.text)
当我运行客户端脚本时,我从服务器获得以下输出

200
Content Type is  multipart/form-data; boundary=d95c72e01bdfac029b16da2b8f144cbd and data is b'' 
 length is 129722
我可以从200状态代码中看到flask应用程序正确地接收POST请求,但是当我尝试读取数据时,我得到一个空的b''字符串。解码音频文件的正确方法是什么?还是我在客户端脚本中发送POST请求的方式有问题

我已经了解了有关StackOverflow的其他问题,他们提到将文件作为POST请求中“files”参数的一部分发送。

尝试使用获取音频文件:

@app.route('/api/audio', methods=['GET', 'POST'])
def get_score():
    if request.method == 'POST':
         request.files['messageFile']
如果我记得的话,request.data只是一个空字符串。使用
request.json()