Retrieving SSLSocket Information in Python

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

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with Google Chrome

 Using HTTPS with Mozilla Firefox

 Using HTTPS with Microsoft Edge

 Using HTTPS with Apple Safari

 Using HTTPS with IE (Internet Explorer)

 Android and Server Certificate

 iPhone and Server Certificate

 Windows Certificate Stores and Console

 RDP (Remote Desktop Protocol) and Server Certificate

 macOS Certificate Stores and Keychain Access

 Linux Certificate Stores and Tools

 Perl Scripts Communicating with HTTPS Servers

 PHP Scripts Communicating with HTTPS Servers

Python Scripts Communicating with HTTPS Servers

 Python http.client for HTTPS Connection

 Modifying SSL Parameters in Python

Retrieving SSLSocket Information in Python

 Dumping Server Certificate in Python

 Using pyOpenSSL for HTTPS Connection

 Java Programs Communicating with HTTPS Servers

 .NET Programs Communicating with HTTPS Servers

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Comodo Free Personal Certificate

 Digital Signature - Microsoft Word

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Archived Tutorials

 References

 Full Version in PDF/EPUB