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
1、HTTPConnectionPool(1) Max retries exceeded with url1、报错: a.HTTPConnectionPool(host='XXX', port=XXX): Max retries exceeded with url: XXXX(Caused by ProxyError('Cannot connect to proxy.', Connectio...