# 未增加重试版本-未使用装饰器
def download(url):
for i in range(5):
try:
res = requests.get(url)
print(res.status_code)
if res.status_code == 200:
return '成功'
return '失败'
except Exception as e:
print('异常重试', e )
continue
return '异常'
# text = input('请输入网址:').strip()
text = 'https://www.pythonav.com'
result = download(text)
print(result)
#加重试版本-使用装饰器
def retry(func):
def inner(*args, **kwargs):
for i in range(5):
try:
ret = func(*args, **kwargs)
return ret
except Exception as e:
print('异常重试', e)
continue
return '异常'
return inner
@retry
def download(url):
res = requests.get(url)
print(res.status_code)
if res.status_code == 200:
return '成功'
return '失败'
# text = input('请输入网址:').strip()
text = 'https://www.pythonav.com'
result = download(text)
print(result)