I am doing multiple RESTful API calls using requests inside a loop. I would like to handle exceptions safely, without the script stopping. Here's my code:
try:
r = requests.get(url)
except requests.exceptions as e:
print("ERROR: Requests exception: %s" % e)
if 'json' in r.headers['Content-Type']:
return r.json()
This works for a while, but then fails with this traceback on the second line. The except clause isn't catching the exception.
File "C:\Python27\myfiles\callAPI.py", line 82, in getAPI
r = requests.get(url)
File "C:\Python27\lib\site-packages\requests\api.py", line 67, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests_cache\core.py", line 128, in request
**kwargs
File "C:\Python27\lib\site-packages\requests\sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests_cache\core.py", line 101, in send
return send_request_and_cache_response()
File "C:\Python27\lib\site-packages\requests_cache\core.py", line 93, in send_request_and_cache_response
response = super(CachedSession, self).send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 608, in send
r.content
File "C:\Python27\lib\site-packages\requests\models.py", line 737, in content
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
File "C:\Python27\lib\site-packages\requests\models.py", line 663, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))
How can I safely handle this?