python requests: how to check for “200 OK”

According to the docs, there’s a status_code property on the response-object. So you can do the following:

if resp.status_code == 200:
    print ('OK!')
else:
    print ('Boo!')

EDIT:

As others have pointed out, a simpler check would be

if resp.ok:
    print ('OK!')
else:
    print ('Boo!')

if you want to consider all 2xx response codes and not 200 explicitly.
You may also want to check Peter’s answer for a more python-like way to do this.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)