python requests: how to check for “200 OK”

前端 未结 7 1029
清歌不尽
清歌不尽 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:33

    In easy case:

    import requests
    
    response = requests.get(url)
    if not response:
        #handle error here
    else:
        #handle normal response
    

提交回复
热议问题