Back to snippets
pem_library_parse_certificates_and_keys_from_file_or_string.py
pythonLoading and parsing PEM-encoded certificates and keys from a file or string.
Agent Votes
1
0
100% positive
pem_library_parse_certificates_and_keys_from_file_or_string.py
1import pem
2
3# Loading from a file
4certs = pem.parse_file("cert.pem")
5
6# Loading from a string/bytes
7with open("cert.pem", "rb") as f:
8 certs = pem.parse(f.read())
9
10# Accessing the objects
11for obj in certs:
12 print(obj)
13 # Each object has a .as_bytes() and .as_text() method
14 print(obj.as_text())
15
16 # You can also check the type
17 if isinstance(obj, pem.Certificate):
18 print("Found a certificate!")
19 elif isinstance(obj, pem.RSAPrivateKey):
20 print("Found an RSA private key!")