sonus vareed
sonus vareed

Reputation: 31

HTTPerror in python urllib.request

i was opening a url by using urllib.request.urlopen.

The following exception was raised

    http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)

HTTPError

I went through the documentation of the requests library

*exception urllib.error.HTTPError

Though being an exception (a subclass of URLError), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.

code

An HTTP status code as defined in RFC 2616. This numeric value corresponds to a value found in the dictionary of codes as found in http.server.BaseHTTPRequestHandler.responses.

reason

This is usually a string explaining the reason for this error. headers The HTTP response headers for the HTTP request that caused the HTTPError.*

But in my case there was no error code or string which gave reason for the exception. enter image description here

Upvotes: 2

Views: 8161

Answers (1)

Coco
Coco

Reputation: 906

If you catch the exception you can see the reason, like this:

try:      
    urllib.request.urlopen(req) 
except urllib.error.HTTPError as e:     
    print(e.reason)

Upvotes: 3

Related Questions