[英]error "socket.timeout: The read operation timed out" while installing a python module
[英]catching “socket.timeout The read operation timed out” in python
我正在調用API,並收到以下超時錯誤:
socket.timeout The read operation timed out
追溯到
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/http/client.py", line 1198, in getresponse
response.begin()
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/http/client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/socket.py", line 576, in readinto
return self._sock.recv_into(b)
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/ssl.py", line 937, in recv_into
return self.read(nbytes, buffer)
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/ssl.py", line 799, in read
return self._sslobj.read(len, buffer)
File "/Users/someuser/anaconda/envs/python3/lib/python3.5/ssl.py", line 583, in read
v = self._sslobj.read(len, buffer)
socket.timeout The read operation timed out
如何從回溯的頂部捕獲此錯誤?
這是一個重復的問題,但是正確的方法是通過使用來捕獲socket.timeout
import socket
try:
...
except socket.timeout:
...
該錯誤指出顯式引發的異常,您可以依靠它。 即使調用方法是ssl lib,該錯誤似乎也與實際的套接字連接有關。
谷歌搜索導致我https://community.home-assistant.io/t/difficulty-catching-an-exception/12955/6
似乎SSL ssl.SSLError
時會拋出ssl.SSLError
。 您可以嘗試類似:
import ssl
def getresponse():
...
try:
response.begin
except ssl.SSLError as err:
handle_error(err)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.