Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL handshake failure on Google Translate

I have successfully been using the gTTS module in order to get audio from Google Translate for a while. I use it quite sparsely (I must have made 25 requests in total), and don't believe I could have hit any kind of limit that would cause my address to be blocked from using the service.

However, today, after trying to use it (I haven't used it in 1-2 months), I got the following program:

from gtts import gTTS

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

To cause an error. I tracked down the problem, and I managed to see that even this simple program:

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

Causes the following error:

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)'),))

I would like to know if anyone has an idea what the issue could be. I can get on the Google Translate website without any problems from my browser, and have no issues using the audio either.

like image 681
pie3636 Avatar asked Nov 16 '25 20:11

pie3636


2 Answers

Accepted answer did not work for me since the code has changed, the way i got it to work was to add verify=False in gtts_token.py instead

response = requests.get("https://translate.google.com/", verify=False)
like image 60
krinker Avatar answered Nov 18 '25 10:11

krinker


This looks like an error related to your proxy setting, especially if you are using your work PC. I have got the same issue, but different error message, for example:

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')],)",),))

To further investigate the issue, you can debug it in the command line.

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

you should see results as below;

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

Solution: I have checked the gTTs documentation, there is no way to pass your proxy setting to the api. so the work around is ignore the ssl verification, which in not available also in gTTs. so the only way to do it is to change the following gtts files:

  1. tts.py, in line 208 chage the request function to add verifiy=false

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

Then, try again the debug command line. you should be able to get the file recorded now

(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'>
like image 40
Wael Almadhoun Avatar answered Nov 18 '25 09:11

Wael Almadhoun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!