Python requests exception handling

后端 未结 5 1931
梦谈多话
梦谈多话 2020-12-25 09:43

How to handle exceptions with python library requests? For example how to check is PC connected to internet?

When I try

try:
    requests.get(\'http         


        
5条回答
  •  有刺的猬
    2020-12-25 10:36

    Assuming you did import requests, you want requests.ConnectionError. ConnectionError is an exception defined by requests. See the API documentation here.

    Thus the code should be :

    try:
       requests.get('http://www.google.com')
    except requests.ConnectionError:
       # handle the exception
    

提交回复
热议问题