欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python requests报错解决办法:Max retries exceeded with url/Name or service not known

程序员文章站 2022-05-26 15:13:51
...

报错一:Max retries exceeded with url

原因:访问URL超过最大连接数,关闭长连接可解决,代码如下

import requests


# 原代码
response = requests.post(url, data=data)


# 修复后代码
# 方案一:设置headers关闭持久链接
headers = {
    'Connection': 'close'
}
response = requests.post(url, data=data, headers=headers)

#方案二:设置Keep-alive = False
s = requests.session()
s.keep_alive = False
s.post(url, data=data)

报错二:Failed to establish a new connection: [Errno -2] Name or service not known’,)

原因:域名无法访问,经检查CentOS未配置DNS,Linux查看DNS配置命令cat /etc/resolv.conf


若为以下报错,同时出现上面两个报错,优先检查服务器是否配置DNS,切记!

HTTPConnectionPool(host='url', port=port): Max retries exceeded with url: /path (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f188bac7898>: Failed to establish a new connection: [Errno -2] Name or service not known',))