Open In App

Introduction to Python certifi Module

Last Updated : 04 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In the modern world, security is an important concern. When we connect our application to a server, it needs to know that the website is safe and trustworthy. This is where SSL certificates are used. SSL certificates are like digital "IDs" that websites use to prove their trustworthiness. But how does our application know which certificates are trustworthy? This is where we use the 'certifi' package. The 'certifi' package has a bundle of trusted SSL certificates that our Python programs can use. We don't have to worry about figuring out which certificates to trust.

Let's now start the conversation with What, Why, and How.

What is certifi in Python?

certifi is a Python package that provides Mozilla’s carefully curated collection of root certificates for validating the trustworthiness of SSL/TLS certificates. It ensures that our Python code can securely communicate with HTTPS servers without the risk of man-in-the-middle attacks or other security threats due to untrusted certificates.

Why Use certifi?

When making HTTPS requests, Python uses a set of root certificates to verify the identity of the server it is communicating with. By default, Python may not always have the most up-to-date certificates, which can lead to issues when accessing secure servers. certifi provides an updated collection of trusted certificates, ensuring that our application can securely connect to external services.

How Does certifi Work?

The certifi module works by bundling a set of root certificates and making them available for Python’s SSL/TLS modules, such as requests. These certificates are regularly updated to include new trusted certificates and remove any that are no longer considered secure.

Installing the certifi Module

Before we use 'certifi' in our python programs, we have to install it. We, open our command prompt and type the following command:

pip install certifi

Installing the requests module

We have to install the requests module also, which is commonly used to make HTTP requests in Python, and the certifi module to provide the SSL certificates. Type the following command in terminal:

pip install requests
download-cerifi

Uses of the certifi Module

We can use certifi module for:

  • To ensure that our Python applications can securely communicate with websites.
  • When we make requests to an API, certifi helps verify that the connection is secure.
  • This module provides a collection of root certificates to validate the SSL certificates of servers.

Importing the necessary Modules

We have import necessary module in our python program to see the output

import requests
import certifi

Example Code:

Now, we are going to see a simple code to seehow the certifi module works. We'll write a Python script that makes a secure request to a website using the requests library, which automatically uses certifi to verify the website’s SSL certificate.

verify=certifi.where(): This parameter tells the requests module to use the certificate bundle provided by certifi for SSL verification.

Python
# Import the necessary modules
import requests
import certifi

# Make a secure GET request to a website
response = requests.get("https://www.geeksforgeeks.org/", verify=certifi.where())

# Check if the request was successful
if response.status_code == 200:
    print("The website is secure and reachable!")
else:
    print("Failed to reach the website.")

Output:

py-modu-output
Using certifi module in Python

If the website was not reachable or secure, the output would be:

Failed to reach the website.

Ensuring Secure Connections

Using certifi ensures that our Python application is always using an up-to-date and trusted set of root certificates. This is especially important when working in environments where security is a priority, such as financial applications, healthcare systems, or any application that handles sensitive data.

Handling SSL Errors

If we encounter SSL errors when making requests in Python, the issue might be related to outdated or untrusted certificates. In such cases, ensuring that we have certifi installed and up to date can often resolve the problem.

Conclusion

The certifi module plays a crucial role in ensuring that Python applications can securely interact with external services over HTTPS. By providing an updated and trusted collection of root certificates, certifi helps to prevent potential security issues related to SSL/TLS communication. Whether we're building a small personal project or a large-scale application, incorporating certifi is a best practice for maintaining secure connections.


Next Article
Article Tags :
Practice Tags :

Similar Reads