目录
1、HTTPConnectionPool2、socket.timeout:3、try……except
1、HTTPConnectionPool
(1) Max retries exceeded with url
1、报错:
a.HTTPConnectionPool(host='XXX', port=XXX): Max retries exceeded with url: XXXX(Caused by ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)))
b.HTTPConnectionPool(host='XXX', port=XXX): Max retries exceeded with url: XXXX(Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000020B87AAC4E0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
c.HTTPConnectionPool(host='XXX', port=XXX): Max retries exceeded with url: XXXX (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response')))
2、原因:
a.http连接太多没有关闭导致
b.访问次数频繁,被禁止访问
c.每次数据传输前客户端要和服务器建立TCP连接,为节省传输消耗,默认为keep-alive,即连接一次,传输多次,然而在多次访问后不能结束并回到连接池中,导致不能产生新的连接
3、解决方法:
a.增加连接次数
b.requests使用了urllib3库,默认的http connection是keep-alive的,requests设置False关闭。
c.headers中的Connection默认为keep-alive,将headers中的Connection一项置为close
4、小知识补充:
a.会话对象requests.Session能够跨请求地保持某些参数,比如cookies,即在同一个Session实例发出的所有请求都保持同一个cookies,而requests模块每次会自动处理cookies,这样就很方便地处理登录时的cookies问题。
(2)代码
"""增加重连次数,关闭多余连接,使用代理"""
import requests
headers
= {'Connection': 'close'}
requests
.adapters
.DEFAULT_RETRIES
= 5
s
= requests
.session
()
s
.keep_alive
= False
s
.proxies
= {"https": "47.100.104.247:8080", "http": "36.248.10.47:8080", }
try:
s
.get
(url
)
except Exception
as err
:
pass
"""This will GET the URL and retry 3 times in case of requests.exceptions.ConnectionError. backoff_factor will help to apply delays between attempts to avoid to fail again in case of periodic request quo"""
import requests
from requests
.adapters
import HTTPAdapter
from requests
.packages
.urllib3
.util
.retry
import Retry
session
= requests
.Session
()
retry
= Retry
(connect
=3, backoff_factor
=0.5)
adapter
= HTTPAdapter
(max_retries
=retry
)
session
.mount
('http://', adapter
)
session
.mount
('https://', adapter
)
try:
s
.get
(url
)
except Exception
as err
:
pass
2、socket.timeout:
(1)socket.timeout
1、报错:
a.socket.timeout: timed out
2、原因:
a.socketTimeout:指客户端和服务进行数据交互的时间,是指两者之间如果两个数据包之间的时间大于该时间则认为超时,而不是整个交互的整体时间,比如如果设置1秒超时,如果每隔0.8秒传输一次数据,传输10次,总共8秒,这样是不超时的。而如果任意两个数据包之间的时间超过了1秒,则超时。
b.一次http请求,必定会有三个阶段,一:建立连接;二:数据传送;三,断开连接。当建立连接在规定的时间内(ConnectionTimeOut )没有完成,那么此次连接就结束了。后续的SocketTimeOutException就一定不会发生。只有当连接建立起来后,也就是没有发生ConnectionTimeOutException ,才会开始传输数据,如果数据在规定的时间内(SocketTimeOut)传输完毕,则断开连接。否则,触发SocketTimeOutException
3.解决方法:
import socket
socket.setdefaulttimeout(timeout)
4.小知识补充:
SocketTimeoutException一般是服务器响应超时,即服务器已经收到了请求但是没有给客户端进行有效的返回;
ConnectTimeoutException指服务器请求超时,指在请求的时候无法客户端无法连接上服务端
(2)代码
import socket
socket
.setdefaulttimeout
(timeout
)
try:
pass
except socket
.timeout
as err
:
pass
3、try……except
try:
pass
except requests
.exceptions
.ReadTimeout
:
pass
except urllib3
.exceptions
.ReadTimeoutError
:
pass
except TimeoutError
:
pass
except urllib3
.exceptions
.NewConnectionError
:
pass
except urllib3
.exceptions
.MaxRetryError
:
pass
except requests
.exceptions
.ProxyError
:
pass