我有一个关于在 Python3 中处理 ConnectionResetError 的问题。这通常发生在我使用 urllib.request.Request 函数时。我想知道如果我们遇到这样的错误是否可以重做请求。例如
def get_html(url):
try:
request = Request(url)
response = urlopen(request)
html = response.read()
except ConectionReserError as e:
get_html(url)
最佳答案
这真的取决于服务器,但你可以这样做:
def get_html(url, retry_count=0):
try:
request = Request(url)
response = urlopen(request)
html = response.read()
except ConectionResetError as e:
if retry_count == MAX_RETRIES:
raise e
time.sleep(for_some_time)
get_html(url, retry_count + 1)
另见 Python handling socket.error: [Errno 104] Connection reset by peer
关于python - 处理 ConnectionResetError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39287669/