Python requests請求中的異常總結


一. 連接超時

服務器在指定時間內沒有應答,拋出異常 requests.exceptions.ConnectTimeout 

requests.get('http://github.com', timeout=0.001)

# 拋出異常
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f1b16da75f8>, 'Connection to github.com timed out. (connect timeout=0.001)'))

二. 連接、讀取超時

若分別指定連接和讀取的超時時間,服務器在指定時間沒有應答,拋出異常 requests.exceptions.ConnectTimeout- timeout=([連接超時時間], [讀取超時時間]) 

- 連接:客戶端成功連接服務器的時間
- 讀取:服務器收到請求並成功處理請求的時間

requests.get('http://github.com', timeout=(6.05, 0.01))

# 拋出異常
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='github.com', port=80): Read timed out. (read timeout=0.01)

三. 未知的服務器

請求未知的服務器,拋出異常requests.exceptions.ConnectionError 

requests.get('http://github.comasf', timeout=(6.05, 27.05))

# 拋出異常
requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.comasf', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f75826665f8>: Failed to establish a new connection: [Errno -2] Name or service not known',))

四. 代理連接不上

代理服務器拒絕與HTTP客戶端建立連接;或者請求服務器端口拒絕連接或未開放,拋出異常requests.exceptions.ProxyError 

requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "192.168.10.1:800"})

# 拋出異常
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('<urllib3.connection.HTTPConnection object at 0x7fce3438c6d8>: Failed to establish a new connection: [Errno 111] Connection refused',)))

五. 連接代理超時

連接代理服務器沒有響應,拋出異常requests.exceptions.ConnectTimeout 

requests.get('http://github.com', timeout=(6.05, 27.05), proxies={"http": "10.200.123.123:800"})

# 拋出錯誤
requests.exceptions.ConnectTimeout: HTTPConnectionPool(host='10.200.123.123', port=800): Max retries exceeded with url: http://github.com/ (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7fa8896cc6d8>, 'Connection to 10.200.123.123 timed out. (connect timeout=6.05)'))

六. 代理讀取超時

說明HTTP與代理服務器成功建立連接,代理服務器也成功發送請求到目標服務器,但是代理服務器讀取目標服務器資源超時;即使代理服務器訪問或者讀取目標服務器的資源速度很快,但是如果代理服務器訪問的目標服務器器返回資源過慢導致超時,這個鍋還是代理服務器背;

例如:假定代理可用,timeout參數代表向代理服務器的連接和讀取過程的超時時間(不用關心代理服務器是否與目標服務器連接和讀取成功)。

requests.get('http://github.com', timeout=(2, 0.01), proxies={"http": "192.168.10.1:800"})

# 拋出錯誤
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.10.1:800', port=1080): Read timed out. (read timeout=0.5)

七. 網絡環境異常

可能是斷網導致,拋出異常 requests.exceptions.ConnectionError 

requests.get('http://github.com', timeout=(6.05, 27.05))

# 拋出異常
requests.exceptions.ConnectionError: HTTPConnectionPool(host='github.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc8c17675f8>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))

【注意】所有的Requests顯式拋出的異常都繼承至 requests.exceptions.RequestException 類。

八. 官網的一些參考

你可以告訴 requests 在經過以 timeout 參數設定的秒數時間之后停止等待響應。基本上所有的生產代碼都應該使用這一參數。如果不使用,你的程序可能會永遠失去響應:

>>> requests.get('http://github.com', timeout=0.001)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)

並不是整個下載響應的時間限制,而是如果服務器在 timeout 秒內沒有應答,將會引發一個異常(更精確地說,是在 timeout 秒內沒有從基礎套接字上接收到任何字節的數據時)


- 遇到網絡問題(如:DNS 查詢失敗、拒絕連接等)時,Requests 會拋出一個 requests.exceptions.ConnectionError 異常。
- 如果 HTTP 請求返回了不成功的狀態碼, Response.raise_for_status() 會拋出一個 HTTPError 異常。
- 若請求超時,則拋出一個 Timeout 異常。
- 若請求超過了設定的最大重定向次數,則會拋出一個 TooManyRedirects 異常。
- 所有Requests顯式拋出的異常都繼承自 requests.exceptions.RequestException 。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM