def get_host_cert(host, port=443):
  host_certs = []
  def verify_cb(conn, cert, errnum, depth, ok):
    host_certs.append(cert)
    # Return True to indicates that the certificate was ok.
    return True

  context = SSL.Context(SSL.SSLv23_METHOD)
  context.set_verify(SSL.VERIFY_PEER, verify_cb)  # Demand a certificate
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  connection = SSL.Connection(context, s)
  try:
    connection.connect((host, port))
    connection.send('')
  except SSL.SysCallError:
    pass
  except socket.gaierror:
    logging.debug('Host name is not valid')
  finally:
    connection.shutdown()
    connection.close()
  if not host_certs:
    logging.warning('Unable to get host certificate from %s:%s', host, port)
    return ''
  return _dump_cert(host_certs[-1])
Contacts the host and returns its certificate.
dataset/ETHPy150Open chromium/web-page-replay/certutils.py/get_host_cert