def get_commonname(host, port=443):
    oid = '\x06\x03U\x04\x03'
pem = ssl.get_server_certificate((host, port))
der = ssl.PEM_cert_to_DER_cert(pem)
i = der.find(oid)
if i != -1:
    i = der.find(oid, i + 1)
if i != -1:
    begin = i + len(oid) + 2
end = begin + ord(der[begin - 1])
return der[begin:end]
return None
Disable SSL certificate validation in Python