key = DSA.generate(1024)
seq = asn1.DerSequence()
seq[:] = [0, key.p, key.q, key.g, key.y, key.x]
exported_key = (
    """-----BEGIN DSA PRIVATE KEY-----
%s-----END DSA PRIVATE KEY-----""" %
    seq.encode().encode('base64'))
print(exported_key)
seq2 = asn1.DerSequence()
data = '\n'.join(exported_key.strip().split('\n')[1:-1]).decode('base64')
seq2.decode(data)
p, q, g, y, x = seq2[1:]
key2 = DSA.construct((y, g, p, q, x))
assert key == key2
How to generate the PEM serialization for the public RSA/DSA key