I generated a private and a public key using OpenSSL with the following commands:
openssl genrsa -out private_key.pem 512 openssl rsa -in private_key.pem -pu
If on Python3, You also need to open the key in binary mode, e.g:
with open('private_key.pem', 'rb') as privatefile: