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('<urllib3.connection.HTTPConnection object at 0x7f188bac7898>: Failed to establish a new connection: [Errno -2] Name or service not known',)) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章