報錯一: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',))