Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 urllib.request.urlopen

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 ?

like image 674
Bogdan Ruzhitskiy Avatar asked Apr 09 '15 11:04

Bogdan Ruzhitskiy


1 Answers

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
like image 165
davidg Avatar answered Sep 18 '22 14:09

davidg