NR | CODE | COMMENT |
---|---|---|
#1 | response = requests.get(url,...) |
within a try block, create variable with a response object |
#2 | response.raise_for_status() |
raises exception 4xx & 5xx HTTP Statuses/errors |
#3 | except requests.exceptions.RequestException as e: |
exception base class for all other exceptions |
#4 | print(e, file=sys.stderr) |
print exception object into stderr stream |
Last active
January 28, 2022 07:19
-
-
Save pkutaj/a078d3406ca90997b22db9e3c2b42ecf to your computer and use it in GitHub Desktop.
Request Module Exception Handling Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import sys | |
url = "http://example.com" | |
try: | |
response = requests.get(url,...) #1 | |
response.raise_for_status() #2 | |
except requests.exceptions.RequestException as e: #3 | |
print(e, file=sys.stderr) #4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment