Python请求中的SSLError(读取操作超时)
我有一个python API脚本,尽管使用了Python请求中的SSLError(读取操作超时),python,ssl,python-requests,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:
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
我推测是请求模块中的某些东西导致了这种情况,但我不知道是什么原因。读取操作已超时,正如它所说的那样
但是,它会使用
ssl.SSLError
超时。这不是您的所捕获的内容,只不过
正在捕获。如果您想捕获并重试,则需要捕获正确的错误。我看到,由于缺乏足够的详细信息,这里对解决方案有一些混淆。我把答案贴在了最上面的帖子上,但是评论部分的格式不太好,我会在这里贴一个格式正确的答案
正如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
我所做的只是在最后添加了一个额外的通用异常捕获器,它将捕获所有其他未解释的异常
我希望这有帮助
谢谢。
除了例外,e
不适用于>=Python 3
你必须使它成为
,除了异常作为e
啊,这是有意义的。现在,我是这样做的,我将看看它是否有效:try:r=requests.post(URL,data=params,headers=headers,timeout=self.request\u timeout)除了request.exceptions.RequestException:。。。除了例外,e:…
谢谢你的提示!用代码编写一个答案,说明要捕获的正确错误是什么,这将非常有帮助。他显然是糊涂了。有点晚了?在谷歌搜索了这个特定的错误(包括我)之后,还会有更多的人来到这里,错过了一个潜在的解决方案?捕获此异常的正确方法是什么?@Toren明显的方法不起作用吗<代码>导入ssl,除了ssl.SSLError
?