I have PEM file containing RSA Private key and list of certificates in it. I am using below code to separate PEM file to Key file and certificate file using pem package and then plugin them into flask server.
This code works, But I want to see if there is an efficient way of working with pem file using python?
Python Code:
from api import app
from gevent.pywsgi import WSGIServer
import pem
from pem import RSAPrivateKey
from pem import Certificate
import os
Mylist = pem.parse_file(r"C:\Desktop\MyPEMFile.pem")
if os.path.exists("APIKEY.key") == False:
for ele in Mylist:
if isinstance(ele, RSAPrivateKey):
f = open ("APIKEY.key","w")
f.write(str(ele))
f.close()
if os.path.exists("APICERTIFICATE.crt") == False:
for ele in Mylist:
if isinstance(ele, Certificate):
f= open ("APICERTIFICATE.crt","a")
f.write(str(ele))
f.close
http_server = WSGIServer(("localhost", 443), app,keyfile='APIKEY.key', certfile='APICERTIFICATE.crt')
http_server.serve_forever()