我有一个关于在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 ConnectionResetError as e:
if retry_count == MAX_RETRIES:
raise e
time.sleep(for_some_time)
get_html(url, retry_count + 1)