totient_n = (p - 1) * (q - 1)
e = 17
d = modinv(e, totient_n)
s = Sequence()

def setvalues(sequence, values):
    for index, value in enumerate(values):
        sequence.setComponentByPosition(index, value)
q = n / p
setvalues(s, map(Integer, (0, n, e, d, p, q, d % (p - 1), d % (q - 1),
    modinv(q, p))))
id_rsa = '-----BEGIN RSA PRIVATE KEY-----\n' + base64.b64encode(encoder.encode(s)) + """
-----END RSA PRIVATE KEY-----
"""
open('id_rsa', 'bw').write(id_rsa)
generating serialized rsa public / private keys from N E D Big Ints