Python+request超时和重试

Python+request超时和重试

一、什么是超时?

1、连接超时

连接超时指的是没连接上,超过指定的时间内都没有连接上,这就是连接超时。(连接时间就是httpclient发送请求的地方开始到连接上目标主机url地址的时间)

2、读取超时

读取超时表示的是连接上了,但是读数据时超过了指定的时间范围,这就是读取超时(读取时间就是HttpClient已经连接到了目标服务器,然后进行内容数据的获取的时间)

二、为什么要设置重试?

比如连接超时,程序就一直处于无响应状态。这时我们需要去不断重试连接,但也不可能是无止境的去重试,所以需要设置一个timeout超时时间。在timeout超时时间内如果无法连接到目标主机url地址,就返回一个异常错误(将这个连接超时的的特殊url写到log日志中,方便管理员查看;读取的数据量大,或者是目标服务器本身的问题(比如读取数据库慢,并发量大等...)也会影响读取时间也可以设置读取超时就返回报错。方便业务管理和问题定位)

1-1 连接超时案例(原始)

import requests
url = 'http://www.google.com.hk'
r = requests.get(url)
print(r.text)

运行结果:

    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.google.com.hk', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0404D230>: Failed to establish
a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))

由1-1案例可以看到因为连接超时,报了一个ConnectionError连接异常。所以为了防止程序因为连接超时报异常导致程序停止运行。我们就使用try……except……做一个异常处理。

1-2 连接超时案例(加requests异常处理)

import requests
import time
url = 'http://www.google.com.hk'
try:
    print(time.strftime('%Y-%m-%d %H:%M:%S'))
    r = requests.get(url)
except requests.exceptions.ConnectionError as e:
    print("连接超时")
print(time.strftime('%Y-%m-%d %H:%M:%S'))

运行结果:

2019-09-24 16:54:50
连接超时
2019-09-24 16:55:11

由1-2案例可以看出,使用try……except……异常处理后,没有再报错。并且可以看出默认的超时时间是21秒

1-3连接超时案例(修改默认超时时间)

import requests
import time
url = 'http://www.google.com.hk'
try:
    print(time.strftime('%Y-%m-%d %H:%M:%S'))
    r = requests.get(url,timeout=5)#timeout修改了超时时间,以秒为单位
except requests.exceptions.ConnectionError as e:
    print("连接超时")
print(time.strftime('%Y-%m-%d %H:%M:%S'))

运行结果:

2019-09-24 17:16:52
连接超时
2019-09-24 17:16:57

由案例1-3得出,设置timeout=5后,默认超时时间被修改成了5秒。所以当碰到一些页面请求时间相应时间过长的情况下,我们可以适当的延长超时时间来达到成功访问页面

1-4连接超时案例(设置多次请求次数)

import requests
import time
from requests.adapters import HTTPAdapter
url = 'http://www.google.com.hk'
s = requests.Session()
s.mount('http://',HTTPAdapter(max_retries=3))#设置重试次数为3次
s.mount('https://',HTTPAdapter(max_retries=3))
try:
    s.get(url,timeout=5)
except requests.exceptions.ConnectionError as e:
    print('连接失败,该URL可能被墙掉了')

运行结果:

连接失败,该URL可能被墙掉了

参考文档:https://www.jianshu.com/p/0a15c253a0d9(设置requests中的重复请求) 、https://2.python-requests.org//zh_CN/latest/user/advanced.html#transport-adapters(requests 2.18.1中文文档中适配器和请求超时)

 

posted @ 2019-09-24 17:59  婷小生  阅读(11911)  评论(0编辑  收藏  举报