How to Scan HTTP Security Headers and SSL Certificates Using Python

Light - June 9, 2025, 9:31 a.m.

πŸ” Introduction

Keeping your website secure starts with understanding how it's exposed. This Python script helps you analyze two major aspects of web security:

With just a few lines of code, you can perform a quick scan of any website and identify potential weaknesses that hackers might exploit.

πŸ” What Are Security Headers?

Security headers are HTTP response headers that provide browsers with information on how to behave securely when interacting with a site. The script checks for the following headers:

πŸ’» Python Code to Scan a Website

Below is the complete code:

import requests 
import ssl 
import socket 
from urllib.parse import urlparse 


SECURITY_HEADERS = [
    'Content-Security-Policy', # XSS
    'Strict-Transport-Security', # HTTPS
    'X-Content-Type-Options', # MIME-sniffing
    'X-Frame-Options', # clickjacking (iframe)
    'X-XSS-Protection', # XSS filter
    'Referer-Policy', # Referer control
    'Permissions-Policy' # Limit API: camera, mic, geolocation
]


def check_http_headers(url):
    try:
        response = requests.get(url, timeout=10)
        print(f"\n[+] HTTP Status Code: {response.status_code}")
        print(f"[+] Server Header: {response.headers.get('Server', 'N/A')}")

        print("\n[+] Checking Security Headers:")
        for header in SECURITY_HEADERS:
            if header in response.headers:
                print(f"  [βœ”] {header}: {response.headers[header]}")
            else:
                print(f"  [✘] {header}: Missing")
    except Exception as e:
        print(f"[!] Error checking headers: {e}")

    
def check_ssl_certificate(hostname):
    try:
        context = ssl.create_default_context()
        with socket.create_connection((hostname, 443)) as sock:
            with context.wrap_socket(sock, server_hostname=hostname) as ssock:
                cert = ssock.getpeercert()
                print(cert)
                print(f"\n[+] SSL Certificate Info:")
                print(f"  - Issuer: {cert['issuer']}") # cert org
                print(f"  - Subject: {cert['subject']}") # domain
                print(f"  - Valid From: {cert['notBefore']}") 
                print(f"  - Valid To: {cert['notAfter']}")
    except Exception as e:
        print(f"[!] Error checking SSL cert: {e}")



def run_scanner(target_url):
    print(f"\n==== Security Scanner Started ====\nTarget: {target_url}")
    parsed = urlparse(target_url)
    hostname = parsed.hostname or target_url.replace("http://", "").replace("https://", "")
    check_http_headers(target_url)
    check_ssl_certificate(hostname)
    print("\n==== Scan Completed ====\n")


if __name__ == '__main__':
    website = input("Enter website URL (e.g., https://example.com): ")
    run_scanner(website)

# pip install requests

πŸ“¦ Requirements

pip install requests

πŸ“Š Example Output

⚠️ Important Notes

 

Read Similars

Automated LAN Scanning with Nmap and Python – Exporting Results to JSON

Learn how to automatically scan your local network using Nmap with Python, detect active devices and operating systems, and export scan results to JSON format.

5 min read

How to Fingerprint a Device in Your Local Network Using Python

Learn how to fingerprint LAN devices using Python, Scapy, and Impacket. Collect OS info, open ports, HTTP server data, and SMB banners with a single script.

5 min read

How to Scan Local Network for Active Devices Using Python and TCP SYN

Learn how to write a Python script to scan local networks using TCP SYN packets, detect live hosts, and discover connected devices with IPs and hostnames.

5 min read

How to Scan Devices in Your Local Network Using Python and ARP

Learn how to use Python and Scapy to scan your local network, detect connected devices by IP and hostname, and quickly discover active hosts in your LAN.

5 min read

Testing Common Django Security Vulnerabilities with Python Scripts

Learn how to test common Django security vulnerabilities like XSS, CSRF, SQL Injection, and more using Python scripts.

5 min read