Handling errors gracefully is crucial for robust applications. The requests library also supports sessions to persist certain parameters across requests.
Key Concepts
- Error Handling: Use try-except blocks to handle exceptions that may occur during requests.
- Session Management: Use requests.Session() to create a session object for persisting settings across requests.
Error Handling:
try:
response = requests.get('https://api.example.com/data', timeout=5)
response.raise_for_status() # Raises an HTTPError for bad responses
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
else:
pass
finally:
passSession Management:
session = requests.Session()
session.headers.update({'User-Agent': 'MyApp/1.0'})
response = session.get('https://api.example.com/data')
print(response.text)Enhance a script to handle various exceptions that may occur during HTTP requests.
Use a session to send multiple requests to a public API and print the responses.
Create a session with custom headers and use it to interact with an API.