python requests: how to check for “200 OK”

前端 未结 7 1021
清歌不尽
清歌不尽 2020-12-31 00:57

What is the easiest way to check whether the response received from a requests post was \"200 OK\" or an error has occurred?

I tried doing something like this:

相关标签:
7条回答
  • 2020-12-31 01:46

    Since any 2XX class response is considered successful in HTTP, I would use:

    if 200 <= resp.status_code <= 299:
        print ('OK!') 
    else:
        print ('Boo!')
    
    0 讨论(0)
提交回复
热议问题