Exception Handling Of Python Requests Module

Python request module is a simple and elegant Python HTTP library. It provides methods for accessing Web resources via HTTP. In the following article, we will use the HTTP GET method in the Request module. This method requests data from the server and the Exception handling comes in handy when the response is not successful. Here, we will go through such situations. We will use Python’s try and except functionality to explore the exceptions that arise from the Requests module.

  • url: Returns the URL of the response
  • raise_for_status(): If an error occur, this method returns a HTTPError object
  • request: Returns the request object that requested this response
  • status_code: Returns a number that indicates the status (200 is OK, 404 is Not Found)
     

Successful Connection Request

The first thing to know is that the response code is 200 if the request is successful.

  • Python3
# code importrequests  # The following line makes an https request, and stores the response # in the variable 'r' r =requests.get("https://www.google.com")  # The following line give us the response code r.status_code

Output:200

Exception Handling for HTTP Errors

Here, we tried the following URL sequence and then passed this variable to the Python requests module using raised_for_status(). If the try part is successful, we will get the response code 200, if the page that we requested doesn’t exist. This is an HTTP error, which was handled by the Request module’s exception HTTPError and you probably got the error 404.

  • Python3
# code importrequests  url ="https://www.amazon.com/nothing_here" try:     r =requests.get(url, timeout=1)     r.raise_for_status() exceptrequests.exceptions.HTTPError as errh:     print("HTTP Error")     print(errh.args[0]) # Prints the response code print(r)

Output:HTTP Error 404 Client Error: Not Found for url: https://www.amazon.com/nothing_here <Response [404]>

General Exception Handling

You could also use a general exception from the Request module. That is requests.exceptions.RequestException.

  • Python3
url ="https://www.google.com"# code try:     r =requests.get(url, timeout=1)     r.raise_for_status() exceptrequests.exceptions.RequestException as errex:     print("Exception request")

Output:Exception request

Now, you may have noticed that there is an argument ‘timeout’ passed into the Request module. We could prescribe a time limit for the requested connection to respond. If this has not happened, we could catch that using the exception requests.exceptions.ReadTimeout. To demonstrate this let us find a website that responds successfully.

  • Python3
importrequests  url ="https://www.google.com"# code try:     r =requests.get(url, timeout=1)     r.raise_for_status() exceptrequests.exceptions.ReadTimeout as errrt:     print("Time out")  print(r)

Output:<Response [200]>

If we change timeout = 0.01, the same code would return, because the request could not possibly be that fast.Time out <Response [200]>

Exception Handling for Missing Schema

Another common error is that we might not specify HTTPS or HTTP in the URL. For example, We cause use requests.exceptions.MissingSchema to catch this exception.

  • Python3
url ="www.google.com"# code try:     r =requests.get(url, timeout=1)     r.raise_for_status() exceptrequests.exceptions.MissingSchema as errmiss:     print("Missing schema: include http or https") exceptrequests.exceptions.ReadTimeout as errrt:     print("Time out")

Output:Missing scheme: include http or https

Exception Handling for Connection Error

Let us say that there is a site that doesn’t exist. Here, the error will occur even when you can’t make a connection because of the lack of an internet connection

  • Python3
# code try:   r =requests.get(url, timeout =1, verify =True)   r.raise_for_status() exceptrequests.exceptions.HTTPError as errh:   print("HTTP Error")   print(errh.args[0]) exceptrequests.exceptions.ReadTimeout as errrt:   print("Time out") exceptrequests.exceptions.ConnectionError as conerr:   print("Connection error")

Output:Connection error

Putting Everything Together

Here, We put together everything we tried so far the idea is that the exceptions are handled according to the specificity. 

For example, url =  “https://www.gle.com”,  When this code is run for this URL will produce an Exception request. Whereas, In the absence of connection requests.exceptions.ConnectionError will print the Connection Error, and when the connection is not made the general exception is handled by requests.exceptions.RequestException.

  • Python3
url ="https://www.gle.com" # code try:     r =requests.get(url, timeout=1, verify=True)     r.raise_for_status() exceptrequests.exceptions.HTTPError as errh:     print("HTTP Error")     print(errh.args[0]) exceptrequests.exceptions.ReadTimeout as errrt:     print("Time out") exceptrequests.exceptions.ConnectionError as conerr:     print("Connection error") exceptrequests.exceptions.RequestException as errex:     print("Exception request")

Output:

Note: The output may change according to requests. Time out


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *