将 PEM 文件解析为对象
Parsing PEM file into Object
我有一个包含一些证书的 PEM 文件。我想将它们解析为一个对象,该对象具有 sha_hash、pem 和过期变量。
我已经创建了对象并且它可以工作。我创建了一个对象列表。我遇到的问题是解析。请参阅下面的完整代码。问题是假设我点击了 SHA 或 BEGIN 或 END 案例..它将行添加到对象..但随后它点击了其他案例..并再次添加它。
一旦它完成其中一个 if 语句,我想做的是转到下一行!
class Certificate(object):
"""A class for parsing and storing information about
certificates:"""
def __init__(self, sha_hash="", pem="", expiration=""):
super(Certificate, self).__init__()
self.sha_hash = sha_hash
self.pem = pem
self.expiration = expiration
def main():
cert_file = '/Users/ludeth/Desktop/testCerts.pem'
myList = []
cert = Certificate()
with open(cert_file, 'r') as myFile:
cert = Certificate()
for line in myFile:
if "SHA" in line:
cert.sha_hash = line
if "BEGIN" in line:
cert.pem = cert.pem + line
if "END" in line:
cert.pem = cert.pem + line
myList.append(cert)
break
else:
cert.pem = cert.pem + line
if __name__ == '__main__':
main()
这是因为您有多个 if
和最后一个 if/else
。如果你想始终完全匹配这些条件之一,你可以改为
if "SHA" in line:
cert.sha_hash = line
elif "BEGIN" in line:
cert.pem = cert.pem + line
elif "END" in line:
cert.pem = cert.pem + line
myList.append(cert)
break
else:
cert.pem = cert.pem + line
我有一个包含一些证书的 PEM 文件。我想将它们解析为一个对象,该对象具有 sha_hash、pem 和过期变量。
我已经创建了对象并且它可以工作。我创建了一个对象列表。我遇到的问题是解析。请参阅下面的完整代码。问题是假设我点击了 SHA 或 BEGIN 或 END 案例..它将行添加到对象..但随后它点击了其他案例..并再次添加它。
一旦它完成其中一个 if 语句,我想做的是转到下一行!
class Certificate(object):
"""A class for parsing and storing information about
certificates:"""
def __init__(self, sha_hash="", pem="", expiration=""):
super(Certificate, self).__init__()
self.sha_hash = sha_hash
self.pem = pem
self.expiration = expiration
def main():
cert_file = '/Users/ludeth/Desktop/testCerts.pem'
myList = []
cert = Certificate()
with open(cert_file, 'r') as myFile:
cert = Certificate()
for line in myFile:
if "SHA" in line:
cert.sha_hash = line
if "BEGIN" in line:
cert.pem = cert.pem + line
if "END" in line:
cert.pem = cert.pem + line
myList.append(cert)
break
else:
cert.pem = cert.pem + line
if __name__ == '__main__':
main()
这是因为您有多个 if
和最后一个 if/else
。如果你想始终完全匹配这些条件之一,你可以改为
if "SHA" in line:
cert.sha_hash = line
elif "BEGIN" in line:
cert.pem = cert.pem + line
elif "END" in line:
cert.pem = cert.pem + line
myList.append(cert)
break
else:
cert.pem = cert.pem + line