Python requests.post 在处理较大文件时抛出解密失败或错误记录 mac

问题描述 投票:0回答:1

我正在从我的一个 python 应用程序发出 POST 请求,如下所示:

with open(file_name, "rb") as audio_file:
    ngrok_url = 'https://****-***-***-*-**.ngrok-free.app/transcribe'
    response = requests.post(ngrok_url, data = audio_file, stream=True)
    transcription = json.loads(response.text)

在一个单独的烧瓶应用程序中,它在 ngrok 代理上运行以给我一个公共地址,我正在获取音频并将其提供给耳语以进行转录,如下所示:

@app.route('/transcribe', methods=["POST"])
def transribe_audio():
    with open('test_audio.mp3', 'wb') as f:
        for chunk in request.stream:
            f.write(chunk)
    model = whisper.load_model("base")
    result = model.transcribe('test_audio.mp3', word_timestamps=True)
    response = json.dumps(result)
    return response

虽然这对音频文件工作得很好<1MB, I get below error in my requests.post script for any large file sizes(So far tested with 25mb and bigger files) :

更新完整的错误日志以便更好地理解

requests.exceptions.SSLError: HTTPSConnectionPool(host='****-***-***-*-**.ngrok-free.app',
port=443): Max retries exceeded with url: /transcribe (Caused by SSLError(SSLError(1, '[SSL:
DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2633)')))
Traceback (most recent call last):
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/http/client.py", line 1371, in getresponse
    response.begin()
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/http/client.py", line 319, in begin
    version, status, reason = self._read_status()
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/http/client.py", line 280, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2633)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/urllib3/connectionpool.py", line 787, in urlopen
    retries = retries.increment(
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='0e45-175-101-4-11.ngrok-free.app', port=443): 
Max retries exceeded with url: /transcribe (Caused by SSLError(SSLError(1, '[SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] 
decryption failed or bad record mac (_ssl.c:2633)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/trsncrpt/myservice.py", line 162, in processWhisperOpenai
    response = requests.post(ngrok_url, data = audio_file, stream=True)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/requests/api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/requests/api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/home/user_dev/.pyenv/versions/3.9.7/lib/python3.9/site-packages/requests/adapters.py", line 563, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='****-***-***-*-**.ngrok-free.app', port=443): 
Max retries exceeded with url: /transcribe (Caused by SSLError(SSLError(1, '[SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] 
decryption failed or bad record mac (_ssl.c:2633)')))

我在 cpu 上运行耳语:

AMD Ryzen 5 2500U

python python-3.x python-requests ngrok openai-whisper
1个回答
0
投票

不确定你的案例的具体情况,但我在使用不同的库时在烧瓶应用程序中遇到了类似的错误。我遇到的问题是因为 gunicorn 会尝试多线程 openssl(我认为),并且当我要么消失

  1. 在 gunicorn 中设置线程 = 1
  2. 在发出请求的函数上使用
  3. 创建了一个进程池以在另一个进程中发出请求。

在我的案例中,当有大约 10-20 个并发请求时,它甚至导致了一些解释器崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.