Skip to content

Instantly share code, notes, and snippets.

@miklevin
Last active October 26, 2016 21:03
Show Gist options
  • Save miklevin/40a416ffb8b2eef1a98c70cf5341b8e1 to your computer and use it in GitHub Desktop.
Save miklevin/40a416ffb8b2eef1a98c70cf5341b8e1 to your computer and use it in GitHub Desktop.
A reminder to myself about handling Requests exceptions
import requests
r = None
try:
r = requests.get("http://www.yahoofdsdsf.com/", proxies={'http': '127.0.0.1:8800'})
except requests.exceptions.ProxyError:
print("Proxy Error")
except requests.exceptions.ConnectTimeout:
print("Taking too long to connect")
except requests.exceptions.ReadTimeout:
print("Taking too long to read back")
except requests.exceptions.TooManyRedirects:
print("Too Many Redirects")
except requests.exceptions.HTTPError:
print("HTTP Error")
except requests.exceptions.RequestException as e:
print("Catastrophic Error with requests")
print(e)
except:
print("Something else entirely went wrong")
if type(r) == requests.models.Response:
if r.ok:
print("Things went well")
else:
print(r.url)
print(r.status_code)
print(r.reason)
else:
print("Requests failed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment