Please check over my Python code for a HTTP GET operation using the requests
library, and provide any potential pointers for improvement.
import requests
token = input()
payload={}
headers = {
"Accept": "application/yang-data+json",
"Content-Type": "application/yang-data+json",
"Authorization": "Bearer {}".format(token),
}
url = "https://sandbox-xxxx.cisco.com/restconf/data/native/router/bgp"
try:
response = requests.get(url, headers=headers, data=payload, verify=False, timeout=10)
except requests.exceptions.HTTPError as errh:
print(f"An HTTP Error occured: {errh}")
except requests.exceptions.ConnectionError as errc:
print(f"An Error Connecting to the API occured: {errc}")
except requests.exceptions.Timeout as errt:
print(f"A Timeout Error occured: {errt}")
except requests.exceptions.RequestException as err:
print(f"An Unknown Error occured: {err}")
else:
print(response.status_code)
print(response.json())
1 Answer 1
Since this is the entire script,
- it doesn't need to exist and you can just invoke
curl
directly; and - we can be a little more forgiving on exception-handling best practices.
There isn't a whole lot of value in separating your except
s for different exception types. This is one of the few use cases where a catch-all except Exception
is not a bad idea. If you print the repr()
of the exception object using the !r
format specifier, it will show you the exception type and content while omitting the traceback. If you do want to see the traceback, just delete your try
/except
entirely and let the default printing take effect.
occured
is spelled occurred
.
Don't call input()
prompt-less.
verify=False
is risky. If the certificate does not have a valid trust chain, then you should pull the certificate and trust it explicitly either in your OS or within requests
.
Consider using pprint
to print your JSON document.
When you print the status code you should also print the reason string.
Suggested
from pprint import pprint
import requests
token = input('Please enter your bearer authentication token: ')
sandbox = input('Please enter your sandbox ID: ')
headers = {
"Accept": "application/yang-data+json",
"Content-Type": "application/yang-data+json",
"Authorization": "Bearer " + token,
}
try:
with requests.get(
url=f"https://sandbox-{sandbox}.cisco.com/restconf/data/native/router/bgp",
headers=headers,
data={},
verify=False,
timeout=10,
) as response:
doc = response.json()
except Exception as e:
print(f'An error occurred: {e!r}')
else:
print(response.status_code, response.reason)
pprint(doc)
-
\$\begingroup\$ Excellent, this is exactly the kind of feedback I was after. I have a few questions off the back of what you said but I'll take them to Google-fu. Thanks, Reinderien again, appreciate you sticking with me and my ignorance throughout the post. All the best. \$\endgroup\$pythontestuser– pythontestuser2022年05月13日 17:21:48 +00:00Commented May 13, 2022 at 17:21
-
\$\begingroup\$ @pythontestuser You can ask here, too; I'm happy to answer (and if the chain gets too long it'll just be moved to chat) \$\endgroup\$Reinderien– Reinderien2022年05月13日 18:19:37 +00:00Commented May 13, 2022 at 18:19
-
\$\begingroup\$ Thanks Reinderien, I really appreciate your time. For your point 1 you made. I started as testing initially with curl. Then I moved towards creating a python script as my end goal is going to be having variable substitutions that a user can enter in a gui (or another system can interact to or from.) I'm a network engineer, so of course I read Python was the way to go about this, so this is why I have started to focus mainly on Python. Would you still say this is the incorrect approach I am looking to take? For point 2, nothing further as you explained everything perfectly, thanks. \$\endgroup\$pythontestuser– pythontestuser2022年05月13日 20:05:28 +00:00Commented May 13, 2022 at 20:05
-
\$\begingroup\$ When expanding to a simple GUI or any kind of non-trivial substitution Python is a good choice. \$\endgroup\$Reinderien– Reinderien2022年05月13日 20:26:23 +00:00Commented May 13, 2022 at 20:26
-
\$\begingroup\$ OK, cool. appreciate the confirmation. \$\endgroup\$pythontestuser– pythontestuser2022年05月13日 22:59:15 +00:00Commented May 13, 2022 at 22:59
my_url
andurl
are two different variables \$\endgroup\$