title: Get the chain of SSL certs and find the expiry date through PyOpenSSL
body: <p>I am trying to find calculate the expiry date of SSL certificates through OpenSSL library of Python. I am new in using this library and written below code to calculate the expiry date. But, I am always getting certs as None. I just don't want to run this code rather run it securely as well. Can somebody take a look into and suggest whether I am missing any step? Thanks</p>
  
  <pre><code>hostname = socket.getfqdn()
  port = socket.gethostname().split('.')[0]
  if port == 'master':
      port = 8140
  else:
      port = 443
  
  context = SSL.Context(SSL.SSLv23_METHOD)
  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  ssl_sock = SSL.Connection(context, sock)
  ssl_sock.settimeout(10)
  ssl_sock.connect((hostname, port))
  ssl_sock.do_handshake()
  
  certs = ssl_sock.get_peer_cert_chain()
  
  if certs is not None:
      for cert in certs:
          x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
          subject = x509.get_subject()
          common_name = str(subject.CN)
          expire_date = x509.get_notAfter()
  
          datetimeformat = '%Y-%m-%d %H:%M:%S'
  
          current_date = datetime.datetime.now()
          cert_status = datetime.datetime.strptime(expire_date, datetimeformat) - datetime.datetime.strptime(
                                                                                          current_date, datetimeformat)
          cert_status = int(cert_status.days)
          dispatch_value('ssl_check', common_name, cert_status, 'gauge')
  </code></pre>