Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSLError (Read operation timed out) in Python requests

I have a python API script and my script sometimes gets terminated on this line despite using try/except. Here is the code:

    try:
            r = requests.post(URL, data=params, headers=headers, timeout=self.request_timeout)
            try:
                response = r.json()
            except Exception, e:
                message = "ERROR_0104! Unexpected error occured. The error is: "
                message += str(e)
                print message
                aux_func.write_log(message)
                return 'Switch'
    except requests.exceptions.RequestException:
            print "Exception occurred on 'API requests post' procedure."
            counter += 1
            continue
    ...

The error occurs on the second line of above shown code. This is the error:

     r = requests.post(URL, data=params, headers=headers, timeout=self.request_timeout)
      File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 88, in post
        return request('post', url, data=data, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
        return session.request(method=method, url=url, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request
        resp = self.send(prep, **send_kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send
        r = adapter.send(request, **kwargs)
      File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 394, in send
        r.content
      File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 679, in content
        self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
      File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 616, in generate
        decode_content=True):
      File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/response.py", line 236, in stream
        data = self.read(amt=amt, decode_content=decode_content)
      File "/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/response.py", line 183, in read
        data = self._fp.read(amt)
      File "/usr/lib/python2.7/httplib.py", line 543, in read
        return self._read_chunked(amt)
      File "/usr/lib/python2.7/httplib.py", line 585, in _read_chunked
        line = self.fp.readline(_MAXLINE + 1)
      File "/usr/lib/python2.7/socket.py", line 476, in readline
        data = self._sock.recv(self._rbufsize)
      File "/usr/lib/python2.7/ssl.py", line 305, in recv
        return self.read(buflen)
      File "/usr/lib/python2.7/ssl.py", line 224, in read
        return self._sslobj.read(len)
    ssl.SSLError: The read operation timed out

I presume something within the Requests module is causing this, but I don't know what.

like image 631
rrlamichhane Avatar asked Jun 13 '14 01:06

rrlamichhane


Video Answer


1 Answers

The read operation has timed out, as it says.

It times out, however, with an ssl.SSLError. This is not what your except is catching. If you want to catch and retry, you need to catch the right error.

like image 85
Veedrac Avatar answered Sep 27 '22 17:09

Veedrac