

import datetime
import ssl
import OpenSSL


def GetCert(SiteName, Port):
    return ssl.get_server_certificate((SiteName, Port))


def ParseCert(CertRaw):
    Cert = OpenSSL.crypto.load_certificate(
        OpenSSL.crypto.FILETYPE_PEM, CertRaw)
    print(str(Cert.get_subject())[18:-2])
    print(datetime.datetime.strptime(str(Cert.get_notBefore())[2:-1],
          '%Y%m%d%H%M%SZ'))
    print(datetime.datetime.strptime(str(Cert.get_notAfter())[2:-1],
          '%Y%m%d%H%M%SZ'))
    print(str(Cert.get_issuer())[18:-2])


CertRaw = GetCert('some.domain.tld', 443)
print(CertRaw)
ParseCert(CertRaw)



import datetime
import ssl
import OpenSSL


def GetCert(SiteName, Port):
    return ssl.get_server_certificate((SiteName, Port))


def ParseCert(CertRaw):
    Cert = OpenSSL.crypto.load_certificate(
        OpenSSL.crypto.FILETYPE_PEM, CertRaw)

    CertSubject = str(Cert.get_subject())[18:-2]
    CertStartDate = datetime.datetime.strptime(str(Cert.get_notBefore())[2:-1],
                                               '%Y%m%d%H%M%SZ')
    CertEndDate = datetime.datetime.strptime(str(Cert.get_notAfter())[2:-1],
                                             '%Y%m%d%H%M%SZ')
    CertIssuer = str(Cert.get_issuer())[18:-2]

    return {'CertSubject': CertIssuer, 'CertStartDate': CertStartDate,
            'CertEndDate': CertEndDate, 'CertIssuer': CertIssuer}


CertRaw = GetCert('some.domain.tld', 443)

print(CertRaw)

Out = ParseCert(CertRaw)
print(Out)
print(Out['CertSubject'])
print(Out['CertStartDate'])

Fix ParseCert() function, add variables, add a return statement
Fix ParseCert() function, add variables, add a return statement

  
  
  import datetime
  import ssl
  import OpenSSL
  
  
  def GetCert(SiteName, Port):
      return ssl.get_server_certificate((SiteName, Port))
  
  
  def ParseCert(CertRaw):
      Cert = OpenSSL.crypto.load_certificate(
          OpenSSL.crypto.FILETYPE_PEM, CertRaw)
+ 
-     print(str(Cert.get_subject())[18:-2])
+     CertSubject = str(Cert.get_subject())[18:-2]
-     print(datetime.datetime.strptime(str(Cert.get_notBefore())[2:-1],
+     CertStartDate = datetime.datetime.strptime(str(Cert.get_notBefore())[2:-1],
-           '%Y%m%d%H%M%SZ'))
+                                                '%Y%m%d%H%M%SZ')
-     print(datetime.datetime.strptime(str(Cert.get_notAfter())[2:-1],
+     CertEndDate = datetime.datetime.strptime(str(Cert.get_notAfter())[2:-1],
-           '%Y%m%d%H%M%SZ'))
+                                              '%Y%m%d%H%M%SZ')
-     print(str(Cert.get_issuer())[18:-2])
+     CertIssuer = str(Cert.get_issuer())[18:-2]
+ 
+     return {'CertSubject': CertIssuer, 'CertStartDate': CertStartDate,
+             'CertEndDate': CertEndDate, 'CertIssuer': CertIssuer}
  
  
  CertRaw = GetCert('some.domain.tld', 443)
+ 
  print(CertRaw)
- ParseCert(CertRaw)
  
+ Out = ParseCert(CertRaw)
+ print(Out)
+ print(Out['CertSubject'])
+ print(Out['CertStartDate'])
+ 
Fix ParseCert() function, add variables, add a return statement
## Code Before:


import datetime
import ssl
import OpenSSL


def GetCert(SiteName, Port):
    return ssl.get_server_certificate((SiteName, Port))


def ParseCert(CertRaw):
    Cert = OpenSSL.crypto.load_certificate(
        OpenSSL.crypto.FILETYPE_PEM, CertRaw)
    print(str(Cert.get_subject())[18:-2])
    print(datetime.datetime.strptime(str(Cert.get_notBefore())[2:-1],
          '%Y%m%d%H%M%SZ'))
    print(datetime.datetime.strptime(str(Cert.get_notAfter())[2:-1],
          '%Y%m%d%H%M%SZ'))
    print(str(Cert.get_issuer())[18:-2])


CertRaw = GetCert('some.domain.tld', 443)
print(CertRaw)
ParseCert(CertRaw)

## Instruction:
Fix ParseCert() function, add variables, add a return statement
## Code After:


import datetime
import ssl
import OpenSSL


def GetCert(SiteName, Port):
    return ssl.get_server_certificate((SiteName, Port))


def ParseCert(CertRaw):
    Cert = OpenSSL.crypto.load_certificate(
        OpenSSL.crypto.FILETYPE_PEM, CertRaw)

    CertSubject = str(Cert.get_subject())[18:-2]
    CertStartDate = datetime.datetime.strptime(str(Cert.get_notBefore())[2:-1],
                                               '%Y%m%d%H%M%SZ')
    CertEndDate = datetime.datetime.strptime(str(Cert.get_notAfter())[2:-1],
                                             '%Y%m%d%H%M%SZ')
    CertIssuer = str(Cert.get_issuer())[18:-2]

    return {'CertSubject': CertIssuer, 'CertStartDate': CertStartDate,
            'CertEndDate': CertEndDate, 'CertIssuer': CertIssuer}


CertRaw = GetCert('some.domain.tld', 443)

print(CertRaw)

Out = ParseCert(CertRaw)
print(Out)
print(Out['CertSubject'])
print(Out['CertStartDate'])
