Reputation: 53
I've been having some issues with this piece of code, and I cannot figure out why.
When I run:
try: r = requests.get('https://httpbin.org/get', timeout=10, headers={'Cache-Control': 'nocache', 'Pragma': 'nocache'}) r.raise_for_status() return r.json() except (requests.exceptions.RequestException, ValueError): return False
NOTE: Host changed for privacy, the actual service is way more erratic/buggy.
I will occasionally get this error:
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='https://httpbin.org/get', port=80): Read timed out. (read timeout=10)
I can't understand what has gone wrong;
I seem to be properly catching requests.exceptions.RequestException
which is a superset/parent of requests.exceptions.ReadTimeout
..
EDIT: It seems updating requests has fixed it.
Upvotes: 5
Views: 6622
Reputation: 4551
It would be nice to have your error replicated, so far I tried code below, but it still does catch a timeout.
import requests
try:
r = requests.get('http://httpstat.us/200?sleep=1000',
timeout=0.01,
headers={'Cache-Control': 'nocache', 'Pragma': 'nocache'})
r.raise_for_status()
print(r.json())
except (requests.exceptions.RequestException, ValueError) as e:
print('Error caught!')
print(e)
prints:
Error caught!
HTTPConnectionPool(host='192.168.1.120', port=8080): Read timed out. (read timeout=0.01)
Even in a minimal form your are still catching requests.exceptions.ReadTimeout
:
try:
raise requests.exceptions.ReadTimeout
except requests.exceptions.RequestException:
print('Caught ReadTimeout')
My best guess is that your exception arises in some other part of code, but not in this example.
Upvotes: 2