How can I avoid exceptions from urllib.request.urlopen
if response.status_code
is not 200? Now it raise URLError
or HTTPError
based on request status.
Is there any other way to make request with python3 basic libs?
How can I get response headers if status_code != 200
?
The docs state that the exception type, HTTPError
, can also be treated as a HTTPResponse
. Thus, you can get the response body from an error response as follows:
import urllib.request
import urllib.error
def open_url(request):
try:
return urllib.request.urlopen(request)
except urllib.error.HTTPError as e:
# "e" can be treated as a http.client.HTTPResponse object
return e
and then use as follows:
result = open_url('http://www.stackoverflow.com/404-file-not-found')
print(result.status) # prints 404
print(result.read()) # prints page contents
print(result.headers.items()) # lists headers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With