新聞中心
python中使用requests語句報(bào)錯(cuò)的原因主要有以下幾種情況:

公司主營業(yè)務(wù):網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出漢壽免費(fèi)做網(wǎng)站回饋大家。
1 連接超時(shí)
服務(wù)器在指定時(shí)間內(nèi)沒有應(yīng)答,拋出 requests.exceptions.ConnectTimeout
requests.get('http://github.com', timeout=0.001)
# 拋出錯(cuò)誤
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(
2. 連接、讀取超時(shí)
若分別指定連接和讀取的超時(shí)時(shí)間,服務(wù)器在指定時(shí)間沒有應(yīng)答,拋出 requests.exceptions.ConnectTimeout
- timeout=([連接超時(shí)時(shí)間], [讀取超時(shí)時(shí)間])
- 連接:客戶端連接服務(wù)器并并發(fā)送http請求服務(wù)器
- 讀?。嚎蛻舳说却?wù)器發(fā)送第一個(gè)字節(jié)之前的時(shí)間
requests.get('http://github.com', timeout=(6.05, 0.01))
# 拋出錯(cuò)誤
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='github.com', port=80): Read timed out.
(read timeout=0.01)3. 未知的服務(wù)器
拋出 requests.exceptions.ConnectionError
requests.get('http://github.comasf', timeout=(6.05, 27.05))
# 拋出錯(cuò)誤
requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.comasf', port=80): Max retries exceeded
with url: / (Caused by NewConnectionError(':
Failed to establish a new connection: [Errno -2] Name or service not known',)) 4. 代理連接不上
代理服務(wù)器拒絕建立連接,端口拒絕連接或未開放,拋出 requests.exceptions.ProxyError
requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "192.168.10.1:800"})
# 拋出錯(cuò)誤
requests.exceptions.ProxyError: HTTPConnectionPool(host='192.168.10.1', port=800): Max retries exceeded with
url: http://github.com/ (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 111]
Connection refused',))) 5. 連接代理超時(shí)
代理服務(wù)器沒有響應(yīng) requests.exceptions.ConnectTimeout
requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "10.200.123.123:800"})
# 拋出錯(cuò)誤
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='10.200.123.123', port=800): Max retries exceeded
with url: http://github.com/ (Caused by ConnectTimeoutError(, 'Connection to 10.200.123.123 timed out. (connect timeout=6.05)')) 6. 代理讀取超時(shí)
說明與代理建立連接成功,代理也發(fā)送請求到目標(biāo)站點(diǎn),但是代理讀取目標(biāo)站點(diǎn)資源超時(shí)
即使代理訪問很快,如果代理服務(wù)器訪問的目標(biāo)站點(diǎn)超時(shí),這個(gè)鍋還是代理服務(wù)器背
假定代理可用,timeout就是向代理服務(wù)器的連接和讀取過程的超時(shí)時(shí)間,不用關(guān)心代理服務(wù)器是否連接和讀取成功
requests.get('http://github.com', timeout=(2, 0.01), proxies={"http": "192.168.10.1:800"})
# 拋出錯(cuò)誤
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.10.1:800', port=1080): Read timed out. (read
timeout=0.5)7. 網(wǎng)絡(luò)環(huán)境異常
可能是斷網(wǎng)導(dǎo)致,拋出 requests.exceptions.ConnectionError
requests.get('http://github.com', timeout=(6.05, 27.05))
# 拋出錯(cuò)誤
requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with
url: /
(Caused by NewConnectionError(': Failed to
establish a new
connection: [Errno -3] Temporary failure in name resolution',)) 網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python使用requests語句報(bào)錯(cuò)的原因有哪些
網(wǎng)站路徑:http://fisionsoft.com.cn/article/cdicoeg.html


咨詢
建站咨詢
