#!/usr/bin/env python from __future__ import print_function import contextlib import socket import ssl import sys print(sys.version_info) print(ssl.OPENSSL_VERSION) HOST = 'www.python.org' ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ctx.load_default_certs() ctx.verify_mode = ssl.CERT_REQUIRED ctx.check_hostname = True with contextlib.closing(ctx.wrap_socket( socket.socket(), server_hostname=HOST)) as sock: sock.connect((HOST, 443)) try: ctx.wrap_socket(socket.socket(), server_hostname='') except Exception as e: print('Expected exception:', e) else: print('NO EXCEPTION') try: ctx.wrap_socket(socket.socket()) except Exception as e: print('Expected exception:', e) else: print('NO EXCEPTION') ctx.check_hostname = False with contextlib.closing(ctx.wrap_socket( socket.socket(), server_hostname=HOST)) as sock: sock.connect((HOST, 443)) with contextlib.closing(ctx.wrap_socket( socket.socket(), server_hostname='')) as sock: sock.connect((HOST, 443)) with contextlib.closing(ctx.wrap_socket( socket.socket())) as sock: sock.connect((HOST, 443))