urllib3.exceptions.MaxRetryError:HTTPConnectionPool(host=’’, port=): Max retries exceeded with url: ** /execute (Caused by NewConnectionError)
场景:
python selenium 爬虫测试
错误信息:
urllib3.exceptions.MaxRetryError:
HTTPConnectionPool(host=‘127.0.0.1’, port=60264): Max retries exceeded with url: /session/e01a77d0b37d544b76244f69c7d6a860/execute (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0x00000214CF1B9F08>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。’))
分析原因:
Case I.端口被占用所致
Case II.采用爬虫工具selenium中,提前quit()(类似于<关闭服务>)后,又请求webdriver服务引起
采用方式:
引用socket
进行ip查询,判断是否有端口被占用
code:
def get_host_ip():
"""
get host ip address
获取本机IP地址
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
def is_port_used(ip, port):
"""
check whether the port is used by other program
检测端口是否被占用
:param ip:
:param port:
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, port))
return True
except OSError:
return False
finally:
s.close()
print(1, is_port_used(get_host_ip(), 80))
增加最大重试次数
requests.adapters.DEFAULT_RETRIES = 5
session = requests.session()
session.keep_alive = False
未果,打断点寻找情况2的原因
发现迭代代码块中有,提前quit后,继续请求webdriver相关服务所致.
晕 (´థ౪థ)σ
五一快乐,祝你快落
完毕.