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

报错一: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(': Failed to establish a new connection: [Errno -2] Name or service not known',)) 

你可能感兴趣的:(排错,python)