3.1 URLError 3.2 HTTPError Error Codes (continues on next page) 5 # Table mapping response codes to messages; entries have the # form {code: (shortmessage, longmessage)}. responses ={ 100 : ( 'Continue','Request received, please continue '), 101 : ( 'Switching Protocols', 'Switching to new protocol; obey Upgrade header '), 200 : ( 'OK ','Request fulfilled, document follows '), 201 : ( 'Created','Document created, URL follows '), 202 : ( 'Accepted', 'Request accepted, processing continues off-line '), 203 : ( 'Non-Authoritative Information','Request fulfilled from cache '), 204 : ( 'No Content','Request fulfilled, nothing follows '), 205 : ( 'Reset Content','Clear input form for further input. '), 206 : ( 'Partial Content','Partial content follows. '), 300 : ( 'Multiple Choices', 'Object has several resources -- see URI list '), 301 : ( 'Moved Permanently','Object moved permanently -- see URI list '), 302 : ( 'Found','Object moved temporarily -- see URI list '), 303 : ( 'See Other ','Object moved -- see Method and URL list '), 304 : ( 'Not Modified ', 'Document has not changed since given time '), 305 : ( 'Use Proxy', http.server.BaseHTTPRequestHandler.responses is a useful dictionary of response codes in that shows all the response codes used by RFC 2616 . The dictionary is reproduced here for convenience Because the default handlers handle redirects (codes in the 300 range), and codes in the 100–299 range indicate success, you will usually only see error codes in the 400–599 range. The$_{HTTPError}$ instance raised will have an integer ‘code’ attribute, which corresponds to the error sent by the server. See section 10 of RFC 2616 for a reference on all the HTTP error codes. Every HTTP response from the server contains a numeric “status code”. Sometimes the status code indicates that the server is unable to fulfil the request. The default handlers will handle some of these responses for you (for example, if the response is a “redirection” that requests the client fetch the document from a different URL, urllib will handle that for you). For those it can’t handle, urlopen will raise an HTTPError . Typical errors include ‘404’ (page not found), ‘403’ (request forbidden), and ‘401’ (authentication required). Often, URLError is raised because there is no network connection (no route to the specified server), or the specified server doesn’t exist. In this case, the exception raised will have a ‘reason’ attribute, which is a tuple containing an error code and a text error message. e.g. >>> req =urllib.request.Request('http://www.pretend_server.org ') >>> try : urllib .request.urlopen(req) ... except urllib.error.URLError as e: ... print(e.reason) ... (4, 'getaddrinfo failed')