Python requests请求中的异常总结
一. 连接超时
服务器在指定时间内没有应答,抛出异常 requests.exceptions.ConnectTimeout
requests.get('http://github.com', timeout=0.001) # 抛出异常 requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f1b16da75f8>, 'Connection to github.com timed out. (connect timeout=0.001)'))
二. 连接、读取超时
若分别指定连接和读取的超时时间,服务器在指定时间没有应答,抛出异常 requests.exceptions.ConnectTimeout- timeout=([连接超时时间], [读取超时时间])
- 连接:客户端成功连接服务器的时间
- 读取:服务器收到请求并成功处理请求的时间
requests.get('http://github.com', timeout=(6.05, 0.01)) # 抛出异常 requests.exceptions.ReadTimeout: HTTPConnectionPool(host='github.com', port=80): Read timed out. (read timeout=0.01)
三. 未知的服务器
请求未知的服务器,抛出异常requests.exceptions.ConnectionError
requests.get('http://github.comasf', timeout=(6.05, 27.05)) # 抛出异常 requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.comasf', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f75826665f8>: Failed to establish a new connection: [Errno -2] Name or service not known',))
四. 代理连接不上
代理服务器拒绝与HTTP客户端建立连接;或者请求服务器端口拒绝连接或未开放,抛出异常requests.exceptions.ProxyError
requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "192.168.10.1:800"}) # 抛出异常 requests.exceptions.ProxyError: HTTPConnectionPool(host='192.168.10.1', port=800): Max retries exceeded with url: http://github.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fce3438c6d8>: Failed to establish a new connection: [Errno 111] Connection refused',)))
五. 连接代理超时
连接代理服务器没有响应,抛出异常requests.exceptions.ConnectTimeout
requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "10.200.123.123:800"}) # 抛出错误 requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='10.200.123.123', port=800): Max retries exceeded with url: http://github.com/ (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7fa8896cc6d8>, 'Connection to 10.200.123.123 timed out. (connect timeout=6.05)'))
六. 代理读取超时
说明HTTP与代理服务器成功建立连接,代理服务器也成功发送请求到目标服务器,但是代理服务器读取目标服务器资源超时;即使代理服务器访问或者读取目标服务器的资源速度很快,但是如果代理服务器访问的目标服务器器返回资源过慢导致超时,这个锅还是代理服务器背;
例如:假定代理可用,timeout参数代表向代理服务器的连接和读取过程的超时时间(不用关心代理服务器是否与目标服务器连接和读取成功)。
requests.get('http://github.com', timeout=(2, 0.01), proxies={"http": "192.168.10.1:800"}) # 抛出错误 requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.10.1:800', port=1080): Read timed out. (read timeout=0.5)
七. 网络环境异常
可能是断网导致,抛出异常 requests.exceptions.ConnectionError
requests.get('http://github.com', timeout=(6.05, 27.05)) # 抛出异常 requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc8c17675f8>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
【注意】所有的Requests显式抛出的异常都继承至 requests.exceptions.RequestException 类。
八. 官网的一些参考
你可以告诉 requests 在经过以 timeout 参数设定的秒数时间之后停止等待响应。基本上所有的生产代码都应该使用这一参数。如果不使用,你的程序可能会永远失去响应: >>> requests.get('http://github.com', timeout=0.001) Traceback (most recent call last): File "<stdin>", line 1, in <module> requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001) 并不是整个下载响应的时间限制,而是如果服务器在 timeout 秒内没有应答,将会引发一个异常(更精确地说,是在 timeout 秒内没有从基础套接字上接收到任何字节的数据时) - 遇到网络问题(如:DNS 查询失败、拒绝连接等)时,Requests 会抛出一个 requests.exceptions.ConnectionError 异常。 - 如果 HTTP 请求返回了不成功的状态码, Response.raise_for_status() 会抛出一个 HTTPError 异常。 - 若请求超时,则抛出一个 Timeout 异常。 - 若请求超过了设定的最大重定向次数,则会抛出一个 TooManyRedirects 异常。 - 所有Requests显式抛出的异常都继承自 requests.exceptions.RequestException 。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!