urllib.error --- urllib.request 引發的例外類別

原始碼:Lib/urllib/error.py


urllib.error module(模組)為 urllib.request 所引發的例外定義了例外 (exception) 類別。基礎例外類別是 URLError

下列例外會被 urllib.error 適時引發:

exception urllib.error.URLError

處理程式 (handler) 在遇到問題時會引發此例外(或其衍生例外)。它是 OSError 的一個子類別。

reason

此錯誤的原因。它可以是一個訊息字串或另一個例外實例。

在 3.3 版的變更: URLError used to be a subtype of IOError, which is now an alias of OSError.

exception urllib.error.HTTPError

雖然是一個例外(URLError 的一個子類別),HTTPError 也可以作為一個非例外的類檔案回傳值(與 urlopen() 所回傳的物件相同)。這適用於處理特殊 HTTP 錯誤,例如為請求認證。

code

一個 HTTP 狀態碼,具體定義見 RFC 2616。這個數值會對應到存放在 http.server.BaseHTTPRequestHandler.responses 程式碼 dictionary 中的某個值。

reason

這通常是一個解釋本次錯誤原因的字串。

headers

導致 HTTPError 的特定 HTTP 請求的 HTTP 回應 header。

在 3.4 版新加入.

exception urllib.error.ContentTooShortError(msg, content)

This exception is raised when the urlretrieve() function detects that the amount of the downloaded data is less than the expected amount (given by the Content-Length header).

content

The downloaded (and supposedly truncated) data.