简体   繁体   English

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

[英]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 . 我有一个python API脚本,尽管使用了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. 我认为Requests模块中的某些内容导致了这一点,但我不知道是什么。

The read operation has timed out, as it says. 正如它所说,读取操作已经超时。

It times out, however, with an ssl.SSLError . 但是,它超时了ssl.SSLError This is not what your except is catching. 不是你的except If you want to catch and retry, you need to catch the right error. 如果要捕获并重试,则需要捕获正确的错误。

I saw that there was some confusion here regarding what the solution is because of lack of enough details. 我看到由于缺乏足够的细节,这里有一些关于解决方案的混淆。 I posted the answer on the comment to the top post but the formatting is not great in the comment section and I will post a properly formatted answer here. 我将评论的答案发布到顶部帖子,但评论部分的格式不是很好,我会在这里发布格式正确的答案。

The problem, as Veedrac has mentioned is that I was not catching all the possible exceptions in the code that I posted in the question. 正如Veedrac所提到的那样,问题在于我没有抓住我在问题中发布的代码中的所有可能的异常。 My code only catches "requests.exceptions.RequestException", and any other exception will cause the code to exit abruptly. 我的代码只捕获“requests.exceptions.RequestException”,任何其他异常都会导致代码突然退出。

Instead, I'm gonna re-write the code like this: 相反,我要重写这样的代码:

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

All I did was add an extra generic exception catcher at the end which will catch all other unaccounted for exceptions. 我所做的就是在最后添加一个额外的通用异常捕获器,它将捕获所有其他未解释的异常。

I hope this helps. 我希望这有帮助。

Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM