Python请求中的SSLError(读取操作超时)

时间:2014-06-13 01:09:20

标签: python ssl python-requests

我有一个python API脚本,尽管使用try/except,我的脚本有时会在此行终止。这是代码:

    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
    ...

错误发生在上面显示的代码的第二行。这是错误:

     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

我认为Requests模块中的某些内容导致了这一点,但我不知道是什么。

3 个答案:

答案 0 :(得分:5)

读取操作已经超时,正如它所说的那样。

然而,它超时ssl.SSLError。这您的except正在捕捉的内容。如果要捕获并重试,则需要捕获正确的错误。

答案 1 :(得分:1)

我看到由于缺乏足够的细节,这里对于解决方案有一些困惑。我将评论的答案发布到了顶部,但评论部分的格式并不理想,我将在此处发布格式正确的答案。

正如Veedrac提到的,问题是我没有在问题中发布的代码中捕获所有可能的异常。我的代码仅捕获“ requests.exceptions.RequestException”,其他任何异常都将导致代码突然退出。

相反,我将重新编写如下代码:

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
except Exception, e:
        print "Exception {0} occurred".format(e)
        continue

我所做的就是在末尾添加一个额外的通用异常捕获器,该捕获器将捕获所有其他未说明的异常。

我希望这会有所帮助。

谢谢。

答案 2 :(得分:1)

except Exception, e不适用于> = Python 3 您必须使其except Exception as e