Type stubs for Python backports modules, providing type annotations for backported functionality from newer Python versions
npx @tessl/cli install tessl/pypi-types-backports@0.1.0Type stubs for Python backports modules, providing type annotations for backported functionality from newer Python versions. This package was historically part of the typeshed project and provided typing support for libraries that backported features to older Python versions.
pip install types-backportsfrom backports.ssl_match_hostname import match_hostname, CertificateErrorAlternative import style:
import backports.ssl_match_hostnamefrom backports.ssl_match_hostname import match_hostname, CertificateError
import ssl
import socket
# Get peer certificate from SSL connection
sock = ssl.wrap_socket(socket.socket())
sock.connect(('example.com', 443))
cert = sock.getpeercert()
# Verify hostname matches certificate
try:
match_hostname(cert, 'example.com')
print("Hostname matches certificate")
except CertificateError as e:
print(f"Hostname verification failed: {e}")
finally:
sock.close()
# Advanced usage with certificate inspection
def verify_with_details(hostname, port=443):
"""Verify hostname with detailed certificate information."""
context = ssl.create_default_context()
with socket.create_connection((hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
cert = ssock.getpeercert()
# Extract certificate details
subject = dict(x[0] for x in cert['subject'])
common_name = subject.get('commonName', '')
try:
match_hostname(cert, hostname)
return True, f"Verified: {common_name}"
except CertificateError as e:
return False, f"Failed for {common_name}: {e}"
# Example usage
success, message = verify_with_details('example.com')
print(message)Provides SSL certificate hostname verification functionality that was backported from newer Python versions to work with older Python installations.
def match_hostname(cert: _PeerCertRetDictType, hostname: str) -> None:
"""
Verify that cert (in decoded format as returned by SSLSocket.getpeercert())
matches the given hostname.
Parameters:
- cert: SSL certificate data structure from SSLSocket.getpeercert()
- hostname: str, hostname to verify against certificate
Returns:
None: Function doesn't return a value, raises exception on failure
Raises:
CertificateError: If hostname doesn't match certificate
"""Exception class for SSL certificate validation failures.
class CertificateError(ValueError):
"""
Exception raised when SSL certificate validation fails during hostname matching.
Inherits from ValueError and is raised by match_hostname when the provided
certificate does not match the given hostname.
"""Root backports namespace provides dynamic attribute access for incomplete typing.
def __getattr__(name: str) -> Incomplete:
"""
Provides dynamic attribute access for incomplete typing in the backports namespace.
Parameters:
- name: str, attribute name being accessed
Returns:
Incomplete: Placeholder type for incomplete typing
"""The package uses types from the standard library:
from typing import Any, Dict, List, Union
# SSL certificate dictionary structure as returned by SSLSocket.getpeercert()
_PeerCertRetDictType = Dict[str, Union[
str, # Simple string fields like 'serialNumber', 'version'
List[List[str]], # Subject/issuer DN components
List[str] # subjectAltName entries
]]
# Placeholder type for incomplete typing from _typeshed
Incomplete = Anyssl.match_hostname() function insteadbackports.ssl_match_hostname PyPI package, which backported SSL hostname matching functionality to older Python versions