Retrieving SSLSocket Information

This section provides a tutorial example on retrieving SSL socket information from an HTTPS connection.

If you want to see more information about an HTTPS connection, you can retrieve the ssl.SSLSocket object from the connect object and print out its properties as shown in this Python example script:

#- http-client-SSLSocket-info.py
#- Copyright (c) 2025 HerongYang.com. All Rights Reserved.

import sys
host = sys.argv[1]
cert = sys.argv[2]

import ssl
context = ssl.create_default_context()
context.load_verify_locations(cafile=cert)

import http.client
conn = http.client.HTTPSConnection(host, context=context)
conn.request("GET", "/")

sock = conn.sock
print(sock.getsockname())
print(sock.getpeername())
print(sock.cipher())

server_cert = sock.getpeercert()
print(server_cert.get('subject'))
print(server_cert.get('issuer'))

res = conn.getresponse()
print(res.read(64))

conn.close()

There are several SSLSocket properties and methods used in this script:

Here is what I get from the HTTPS connection with www.google.com:

herong$ python3 http-client-SSLSocket-info.py \
  www.google.com /private/etc/ssl/cert.pem

('10.0.0.148', 56628)
('142.251.151.119', 443)
('TLS_AES_256_GCM_SHA384', 'TLSv1.3', 256)
((('commonName', 'www.google.com'),),)
((('countryName', 'US'),), 
 (('organizationName', 'Google Trust Services'),), 
 (('commonName', 'WR2'),))

b'<!doctype html><html itemscope="" itemtype="http://schema.org/We'

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

Communicating with HTTPS Servers

 http.client for HTTPS Connection

 Modifying SSL Parameters

Retrieving SSLSocket Information

 Dumping Server Certificate

 Use pyOpenSSL for HTTPS Connection

 tinyec - Tiny Library for ECC

 Generating EC Public-Private Keys

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB