Python

Python SSL Example

Python is a language that has lots of tools and resources for creating websites, studying data, machine learning, and other things. It can also work with SSL using various modules. SSL is a vital protocol that allows secure communication between a specified client and a server. We can use it in many programming languages, but in this article, we will focus on Python.

This write-up will teach:

What is SSL (Secure Socket Layer)?

The SSL, known as Secure Socket Layer, is employed to assure/ensure the privacy and security of data transmitted/transferred over the Internet. It encrypts the data that is sent between a web browser and a web server so that only the intended recipient can read it. SSL is utilized to protect sensitive information, such as credit card numbers, passwords, and personal details.

Let’s understand the SSL in detail using several examples.

Example 1: Verifying SSL Certificates

SSL certificates have a start and finish date that shows when they are legal/valid. To check if a website’s SSL certificate is valid using Python, we can use the requests module. Here is a code that shows/demonstrates how to do it:

import requests
response = requests.get("https://google.com")
if response.status_code == 200:
    print("Valid SSL certificate")
else:
    print("Invalid SSL certificate")

In this code:

  • The “requests.get()” method of the “requests” module is utilized to send the GET request to the website.
  • After that, it checks the response status code, which indicates/specifies that the request was successful.
  • A status code of 200 means OK, which means the request was successful and the server retrieved the expected content.

Output

The above output verifies that the SSL certificate is valid.

Example 2: Acquiring SSL Certificates

To acquire SSL certificates, the following example code is used in Python:

import ssl
import urllib.request
parsed_url = urllib.parse.urlparse("https://google.com")
print(ssl.get_server_certificate((parsed_url.hostname, 443)))

Here:

  • Firstly, we imported two modules ssl and urllib.request.
  • After that, the “urllib.parse.urlparse()” function is utilized to parse the particular URL. This function returns a tuple that contains the components of the URL, such as scheme, netloc, path, query, and fragment.
  • Next, the “ssl.get_server_certificate()” function is used to retrieve the SSL certificate of the server that hosts the URL. This function takes a tuple of (hostname, port) as an argument and returns a string that contains the PEM-encoded certificate.
  • Here, the hostname is obtained from the parsed URL, and the port is 443, which is the default port for HTTPS connections.

Output

The SSL certificate of the server has been retrieved successfully.

Example 3: Establishing a Connection with the Website Using SSL Module

To establish a connection with a specific website using the SSL module, the following code is used in Python:

import socket
import ssl
target_host = 'www.Linuxhint.com'
ssl_context = ssl.create_default_context()
with socket.create_connection((target_host, 443)) as connection:
    with ssl_context.wrap_socket(connection, server_hostname=target_host) as secure_socket:
        print(secure_socket.version())

In this code:

  • First, we imported the “ssl” and “socket” modules.
  • After that, we initialized the target host to connect/access the website through HTTPS.
  • Next, we create an SSL context object using the “ssl.create_default_context()” function.
  • The “socket.create_connection()” function is used to create a plain socket object that connects to the target host on port 443, which is the default port for HTTPS.
  • Finally, the “ssl_context.wrap_socket()” method wraps the plain socket object with an SSL socket object, which can encrypt and decrypt data using SSL.
  • Here, the with statement is used to ensure that the socket is closed automatically when we are done with it.

Output

The above output displays the SSL protocol version that is utilized by the SSL socket.

Note: To use SSL in Python on Windows, you need to import a module called SSL and have a valid certificate file. You can get a certificate file from the website or a trusted authority.

Conclusion

The Secure Socket Layer (SSL) is a protocol that is utilized to ensure/assure the privacy and security of data transmitted/transferred over the Internet. The “ssl” module, “requests” module, and the “socket” module are used in Python to work with SSL, such as verifying, acquiring, or implementing SSL. This tutorial elaborated on Python SSL using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.