The attribute status_code
contains the status code of the response
good_req = requests.get('https://api.github.com/events')
code_200 = good_req.status_code
notfound_req = requests.get('https://api.github.com/not_found')
code_404 = notfound_req.status_code
requests.codes.__dict__
will provide a list of available http status codes.
It is possible to user raise_for_status
to check if the status_code was 4xx or 5xx and raise a corresponding exception in that case.
good_req = requests.get('https://api.github.com/events')
good_req.raise_for_status()
# is a 200 status code so nothing happens
notfound_req = requests.get('https://api.github.com/not_found')
notfound_req.raise_for_status()
# raises requests.exceptions.HTTPError: 404 Client Error