Python Tutorials - Herong's Tutorial Examples - v2.21, by Herong Yang
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
Variables, Operations and Expressions
Function Statement and Function Call
List, Set and Dictionary Comprehensions
Packages and Package Directories
"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
►Retrieving SSLSocket Information
Use pyOpenSSL for HTTPS Connection
Generating EC Public-Private Keys
Anaconda - Python Environment Manager