Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to disable security certificate checks for requests in Python

How to disable security certificate checks for requests in Python

In this article, we will discuss how to disable security certificate checks for requests in Python.

In Python, the requests module is used to send HTTP requests of a particular method to a specified URL. This request returns a response object that includes response data such as encoding, status, content, etc. But whenever we perform operations like get, post, delete, etc. 

We will get SSLCertVerificationError i.e., SSL:Certificate_Verify_Failed self-signed certificate. To get rid of this error there are two ways to disable the security certificate checks. They are

  • Passing verify=False to request method.
  • Use Session.verify=False

Method 1: Passing verify=False to request method

The requests module has various methods like get, post, delete, request, etc. Each of these methods accepts an URL for which we send an HTTP request. Along with the URL also pass the verify=False parameter to the method in order to disable the security checks.

Python3




import requests
 
# sending a get http request to specified url
response = requests.request(
    "GET", "https://www.geeksforgeeks.org/", verify=False)
 
# response data
print(response.text)
print(response)


Output:

<Response [200]>

Explanation: By passing verify=False to the request method we disabled the security certificate check and made the program error-free to execute. But this approach will throw warnings as shown in the output picture. This can be avoided by using urlib3.disable_warnings method.

Handle the abovewarning with requests.packages.urllib3.disable_warnings() method

The above warning that occurred while using verify=False in the request method can be suppressed by using the urllib3.disable_warnings method. 

Python3




import requests
from urllib3.exceptions import InsecureRequestWarning
 
# Suppress the warnings from urllib3
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
 
# sending a get http request to specified url
response = requests.get("https://www.geeksforgeeks.org/",
                        verify=False)
 
# response data
print(response)


Output:

<Response [200]>

Method 2: Use Session.verify=False

The alternate way of disabling the security check is using the Session present in requests module. We can declare the Session.verify=False instead of passing verify=True as parameter. Let’s look into the sample code so that one will get the clear picture of using Session.

Python3




import requests
 
# creating Session object and
# declaring the verify variable to False
session = requests.Session()
session.verify = False
 
# sending a get http request to specified url
response = requests.get("https://www.geeksforgeeks.org/")
 
# response data
print(response.text)


Output

RELATED ARTICLES

Most Popular

Recent Comments