def retry(num=5, sleep=1):
def decorator(func):
def warpper(*args, **kwargs):
i = 1
while i < num:
try:
result = func(*args, **kwargs)
if 200 == result:
if i == 1:
print('{}第{}次就成功了'.format(func.__name__, i))
return result
except Exception as ex:
file_name = func.__name__
lineno = ex.__traceback__.tb_next.tb_lineno if ex.__traceback__.tb_next else ex.__traceback__.tb_lineno
print('{}_{}行:錯誤'.format(file_name, lineno, i))
finally:
print(func.__name__,i,'次錯誤')
i += 1
time.sleep(sleep)
return warpper
return decorator
當發送請求失敗的時候會重試