我有一个关于在 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处理socket.error:[Errno 104]连接被对等方重置 https://stackoverflow.com/questions/20568216/python-handling-socket-error-errno-104-connection-reset-by-peer
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)