python copy文件超时_详解Python requests 超时和重试

网络请求不可避免会遇上请求超时的情况,在 requests 中,如何设置请求超时时间又如何设置重试呢?

直接上代码

1、设置请求超时时间5s  timeout=5 来指定

def get_url():

Headers = {

'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'

}

url = "https://www.google.com.hk"

print(datetime.datetime.now())

try:

res = requests.get(url=url, headers=Headers, timeout=5)

except Exception as e:

pass

print(datetime.datetime.now())

五秒超时

2124d9ab4f8e3aec4aad3ff8b56910b8.png

2、设置超时重试,如下

def get_url1():

Headers = {

'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'

}

url = "https://www.google.com.hk"

print(datetime.datetime.now())

s = requests.Session()

s.mount('http://', HTTPAdapter(max_retries=3))

s.mount('https://', HTTPAdapter(max_retries=3))

try:

res = s.head(url=url, headers=Headers, timeout=5)

except Exception as e:

pass

print(datetime.datetime.now())

超时重试三次,加上第一次应该是一共四次 耗时20s

5f0d15bf37b44b645fa56dcf10954a02.png

上面这种重试是requests自己封装好的,但是不太满足个性化的需求 于是有了第三种超时重试

3、直接就是死循环,重试多次,一直到成功:

def get_url2():

Headers = {

'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'

}

url = "https://www.google.com.hk"

keep = True

while keep:

try:

res = requests.head(url=url, headers=Headers, timeout=5)

keep = False

except Exception as e:

print(datetime.datetime.now())

可以看到一直重试

3a37d1c6392382ab0ec1366da8fee3ad.png

死循环可不行啊,加个次数就好了,对上面代码改进下,有了第四种

4、自定义重试次数

def get_url3():

Headers = {

'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'

}

url = "https://www.google.com.hk"

keep = True

maxtimes = 3

count = 0

print(datetime.datetime.now())

while keep and count < maxtimes:

try:

res = requests.head(url=url, headers=Headers, timeout=5)

keep = False

except Exception as e:

print(datetime.datetime.now())

count = count + 1

print('重试' + str(count))

3399c54c9b89f9947cc9fc81f43eb1be.png

这时候有的人会问,这第四种和第二种有啥锤子区别???看看下面这个例子

5、自定义时间间隔。

某些情况下,爬虫的时候短时间内重试多次返回的东西是一样的,这时候就可以设置多少间隔后再重试。

def get_url4():

Headers = {

'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'

}

url = "https://www.google.com.hk"

keep = True

maxtimes = 3

count = 0

print(datetime.datetime.now())

while keep and count < maxtimes:

try:

res = requests.get(url=url, headers=Headers, timeout=5)

keep = False

except Exception as e:

print(datetime.datetime.now())

# 延时10s后重试

time.sleep(10)

count = count + 1

print('重试' + str(count))

6a973db0720befc11b755d1962d17545.png

这样就可以控制每次重复请求的时间间隔,或者中间做点其他的东西。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值