1. 程式人生 > >Python Requests中異常總結

Python Requests中異常總結

1. 連線超時

伺服器在指定時間內沒有應答,丟擲 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)'))

2. 連線、讀取超時

若分別指定連線和讀取的超時時間,伺服器在指定時間沒有應答,丟擲 requests.exceptions.ConnectTimeout
- timeout=([連線超時時間], [讀取超時時間])
- 連線:客戶端連線伺服器並併發送http請求伺服器
- 讀取:客戶端等待伺服器傳送第一個位元組之前的時間

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)

3. 未知的伺服器

丟擲 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',))

4. 代理連線不上

代理伺服器拒絕建立連線,埠拒絕連線或未開放,丟擲 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',)))

5. 連線代理超時

代理伺服器沒有響應 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)'))

6. 代理讀取超時

說明與代理建立連線成功,代理也傳送請求到目標站點,但是代理讀取目標站點資源超時
即使代理訪問很快,如果代理伺服器訪問的目標站點超時,這個鍋還是代理伺服器背
假定代理可用,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)

7. 網路環境異常

可能是斷網導致,丟擲 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',))

8. 官網的一些參考

你可以告訴 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