Google 上的 SSL 握手失败

SSL handshake failure on Google Translate

为了从 Google 翻译中获取音频,我已经成功地使用了 gTTS 模块一段时间。我很少使用它(我总共必须发出 25 个请求),并且不相信我会达到任何会导致我的地址被阻止使用该服务的限制。

然而,今天,在尝试使用它之后(我已经1-2个月没有使用它了),我得到了以下程序:

from gtts import gTTS

tts = gTTS('hallo', 'de')
tts.save('hallo.mp3')

导致错误。我找到了问题所在,我设法看到即使是这个简单的程序:

import requests
response = requests.get("https://translate.google.com/")

导致以下错误:

Traceback (most recent call last):
  File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "C:\...\lib\site-packages\urllib3\connection.py", line 326, in connect
    ssl_context=context)
  File "C:\...\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\...\lib\ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "C:\...\lib\ssl.py", line 814, in __init__
    self.do_handshake()
  File "C:\...\lib\ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "C:\...\lib\ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:777)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\...\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\...\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:777)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main2.py", line 2, in <module>
    response = requests.get("https://translate.google.com/")
  File "C:\...\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\...\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\...\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\...\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\...\lib\site-packages\requests\adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:777)'),))

我想知道是否有人知道问题出在哪里。我的浏览器可以毫无问题地访问 Google 翻译网站,使用音频也没有任何问题。

这看起来像是与您的代理设置有关的错误,尤其是当您使用工作电脑时。我有同样的问题,但不同的错误消息,例如:

gTTSError: Connection error during token calculation: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",),))

要进一步调查问题,您可以在命令行中进行调试。

(base) c:\gtts-cli "sample text to debug" --debug --output test.mp3

您应该会看到如下结果;

ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))

解决方法: 我已经检查了 gTTs 文档,无法将您的代理设置传递给 api。因此解决方法是忽略 ssl 验证,这在 gTT 中也不可用。所以唯一的方法是更改​​以下 gtts 文件:

  1. tts.py,第208行更改请求函数添加verifiy=false

            r = requests.get(self.GOOGLE_TTS_URL,
                             params=payload,
                             headers=self.GOOGLE_TTS_HEADERS,
                             proxies=urllib.request.getproxies(),
                             verify=False)
    
  2. 文件 lang.py,第 56 行
page = requests.get(URL_BASE, verify=False)

然后,再次尝试调试命令行。你现在应该可以录制文件了

(base) c:\gtts-cli "sample text to debug" --debug --output test.mp3

gtts.tts - DEBUG - status-0: 200
gtts.tts - DEBUG - part-0 written to <_io.BufferedWriter name=test.mp3'>

接受的答案对我不起作用,因为代码已更改,我让它起作用的方法是在 gtts_token.py 中添加 verify=False 而不是

response = requests.get("https://translate.google.com/", verify=False)