1

I'm using following code, to get a big API response using python requests library :

try : 
    r = requests.get("download_link")
    data = r.content
except requests.exceptions.ConnectionError as ex:
    print('Issue file download...'+str(ex))
except Exception as ex:
    print('Issue file download...'+str(ex))

Python requests version : 2.18.4,

Python version : 3-x

Here the use case is, as soon as the API is called, I'm disconnecting the internet, and this is not throwing any error(program is stuck at the API call). The API called would take 30+ secs to give complete response based on network speed, and there is high possibility of internet disconnection during this API call.

I would like to catch error when internet disconnects during the API call. Could someone please guide me on how to handle it.

Ajith
  • 87
  • 3
  • 12
  • This is not necessarily possible. It's not like there are "ping" packets going back and forth all the time to monitor the connection state, and there is no entity that can send you a notice if your internet access is interrupted. It's just that the delay between packets gets longer. Remember, if you have a sudden network delay, you want the transfer to continue when things get back to normal. – Tim Roberts May 07 '21 at 06:04
  • In my case, the program is running on a device. And there could be complete network loss or internet disconnection during API call(response could take more than 30+ secs), and I would like to catch error when internet disconnects during the API call. – Ajith May 07 '21 at 06:13
  • What's the point? What are you going to do about it? All you can do is wait for things to come alive again, at which point the download will continue. Look, there is no way for you to get an event that says "you can no longer reach the Internet". You are talking to a web site, and either you can reach that web site, or you can't. – Tim Roberts May 07 '21 at 06:16
  • Part of the problem is there is no such thing as "the internet". If you want to start a separate thread that runs 24/7 and tries to ping some well-known IP address, then you would know when you stop getting pings. That doesn't necessarily mean you aren't still talking to your target site. And again, what action are you going to take? – Tim Roberts May 07 '21 at 06:20
  • @TimRoberts can we use `status_code` here? – nobleknight May 07 '21 at 06:27
  • @TimRoberts Here this internet disconnection during API call is blocking the program (its stuck) if internet disconnects in-between the API call, I would just like to log the exception(send the log for monitoring), store failed APIs in queue to trigger it again when internet is back. But most importantly I would like to execute other parts of the program and not stuck at this API call if internet disconnects. – Ajith May 07 '21 at 06:33

1 Answers1

1

Since requests >= 2.4.0, you can use the timeout argument in seconds:

try : 
    timeout = 10
    r = requests.get("download_link", timeout=10)
    data = r.content
except requests.exceptions.ConnectionError as ex:
    print('Issue file download...'+str(ex))
except requests.exceptions.ReadTimeout as ex:
    print('Issue file download...'+str(ex))
except Exception as ex:
    print('Issue file download...'+str(ex))
AlbertFX91
  • 108
  • 7
  • How could the timeout help here ? If I want to catch the internet disconnection error during the API call. I cant completely decide on a timeout value as it can differ (above 10 secs for sure). The API response is bigger one and can take more than 30+ secs based on network speed. – Ajith May 07 '21 at 06:14
  • Yeah that's true... I have been playing around with the Session object and HTTPAdapter but without any news. I think we have here two possible solutions: The first one is to debug the HTTP request in low level or, the second one, is to provide a big timeout (I hate to say it....) – AlbertFX91 May 07 '21 at 06:38
  • If I give big timeout, the API request waits till that timeout and wont throw any error as soon as internet disconnects during the API call – Ajith May 07 '21 at 06:42