- Requests - Discussion
- Requests - Useful Resources
- Requests - Quick Guide
- Requests - Web Scraping using Requests
- Requests - Proxy
- Requests - Event Hooks
- Requests - Authentication
- Requests - SSL Certification
- Requests - Handling Sessions
- Requests - Handling History
- Requests - Handling Redirection
- Requests - Handling Timeouts
- Requests - Working with Errors
- Requests - Working with Cookies
- Requests - File Upload
- Handling POST, PUT, PATCH & DELETE Requests
- Requests - Handling GET Requests
- Requests - HTTP Requests Headers
- Handling Response for HTTP Requests
- Requests - Working with Requests
- Requests - How Http Requests Work?
- Requests - Environment Setup
- Requests - Overview
- Requests - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Requests - Working with Errors
This chapter will discuss how to deal with errors coming down when working with the Http request pbrary. It is always a good practice to have errors managed for all possible cases.
Error Exception
The requests module gives the following types of error exception −
ConnectionError − This will be raised, if there is any connection error. For example, the network failed, DNS error so the Request pbrary will raise ConnectionError exception.
Response.raise_for_status() − Based on status code i.e. 401, 404 it will raise HTTPError for the url requested.
HTTPError − This error will be raised for an invapd response coming down for the request made.
Timeout − Errors raised for a timeout for the URL requested.
TooManyRedirects − If the pmit is crossed for maximum redirections than it will raise TooManyRedirects error.
Example
Here is an example of errors shown for timeout −
import requests getdata = requests.get(,timeout=0.001) print(getdata.text)
Output
raise ConnectTimeout(e, request=request) requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host= jsonplaceholder.ty picode.com , port=443): Max retries exceeded with url: /users (Caused by Connect TimeoutError(<urlpb3.connection.VerifiedHTTPSConnection object at 0x000000B02AD E76A0>, Connection to jsonplaceholder.typicode.com timed out. (connect timeout = 0.001) ))Advertisements