Python SSL error Decryption failed or bad record mac
Asked Answered
P

4

15

I am trying to use gspread python module to read the google spreadsheet. But I am getting some error from ssl. I have tried to look into this, but couldn't able to make it work. The gspread module is using python request internally which seems to be use python's ssl package. The code (I have written) and stack trace is provided below with env details.

Env:

  • python: 2.7.13
  • Ubuntu: 14.04
  • openssl: 1.0.1f 6 Jan 2014

Code:

import gspread
from oauth2client.client import SignedJwtAssertionCredentials

scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(user_email, private_key, scope)
google_client = gspread.authorize(credentials)


worksheet = google_client.open_by_url(<worksheet_url>).get_worksheet(1)

list_of_rows = worksheet.get_all_values()

print list_of_rows

Stack trace:

File "<console>", line 1, in <module>
  File "/home/nikhil/.venv/nikhil/src/gspread/gspread/models.py", line 344, in get_all_values
    cells = self._fetch_cells()
  File "/home/nikhil/.venv/nikhil/src/gspread/gspread/models.py", line 231, in _fetch_cells
    feed = self.client.get_cells_feed(self)
  File "/home/nikhil/.venv/nikhil/src/gspread/gspread/client.py", line 186, in get_cells_feed
    r = self.session.get(url)
  File "/home/nikhil/.venv/nikhil/src/gspread/gspread/httpsession.py", line 76, in get
    return self.request('GET', url, **kwargs)
  File "/home/nikhil/.venv/nikhil/src/gspread/gspread/httpsession.py", line 68, in request
    response = func(url, data=data, headers=request_headers, verify=False)
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/sessions.py", line 469, in get
    return self.request('GET', url, **kwargs)
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/sessions.py", line 606, in send
    r.content
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/models.py", line 724, in content
    self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/models.py", line 653, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/packages/urllib3/response.py", line 256, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/packages/urllib3/response.py", line 186, in read
    data = self._fp.read(amt)
  File "/usr/local/lib/python2.7.13/lib/python2.7/httplib.py", line 573, in read
    return self._read_chunked(amt)
  File "/usr/local/lib/python2.7.13/lib/python2.7/httplib.py", line 642, in _read_chunked
    value.append(self._safe_read(chunk_left))
  File "/usr/local/lib/python2.7.13/lib/python2.7/httplib.py", line 688, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "/usr/local/lib/python2.7.13/lib/python2.7/socket.py", line 384, in read
    data = self._sock.recv(left)
  File "/usr/local/lib/python2.7.13/lib/python2.7/ssl.py", line 766, in recv
    return self.read(buflen)
  File "/usr/local/lib/python2.7.13/lib/python2.7/ssl.py", line 653, in read
    v = self._sslobj.read(len)
SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:1864)

Any help would be much appreciated.

UPDATE: I have setup everything from scratch on new machine and also updated the oauth client to utilize new API version. Couldn't make the above env work so keeping this question open for now.

Palmate answered 21/1, 2017 at 14:27 Comment(8)
What is your version of oauth2client?Amygdalin
From what I have read so far it may be an issue related to the buffer size. Can you access a single cell?Amygdalin
@JacquesGaudin oauth2client version is 1.4.12Palmate
I tried to access single row, was the same issue.Palmate
It looks like it is an issue people have with multiple connections. Have you tried a keep-alive? from gspread.httpsession import HTTPSession http_session = HTTPSession(headers={'Connection':'Keep-Alive'}) gc = gspread.Client(credentials, http_session) gc.login()Amygdalin
@JacquesGaudin Thank you for point this out. I will try this out and will update you.Palmate
Did you get it to work?Amygdalin
Sorry, didn't get chance to check it. Will post update soon.Palmate
S
5

I was on Wifi and it happened to me. I plugged the computer with an ethernet cable and it solved the problem for me.

Shorn answered 20/8, 2020 at 18:9 Comment(2)
For me I had to do the opposite ... I was plugged in and just plugged out ... Thanks, I guess ...Selfmoving
I move from the wifi 5GHz to old one; and it worksChip
R
5

I was able to fix the issue by updating the urllib3 library using pip

pip install urllib3 --upgrade
Rox answered 24/3, 2023 at 19:39 Comment(0)
K
1

based on other answers I simply turned my wifi off and on and that did the trick for me.

Kabob answered 21/8, 2023 at 14:54 Comment(1)
+1 Glad I didn't ignore this answer, because it actually worked. So I recommend trying this to anybody reading this comment. Thanks!... :)Pogue
R
1

I just updated my pip and urllib. It worked.

Ribbentrop answered 1/1, 2024 at 7:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.