code: def create_rsa_key(bits=2048,
                     keyfile=None,
                     format='PEM',
                     passphrase=None):
      
      if passphrase and format != 'PEM':
          raise Exception(
              "passphrase is only supported for PEM encoded private keys")
      rsakey = RSA.generate(bits)
      if passphrase and isinstance(passphrase, collections.Callable):
          passphrase = passphrase()
      output = rsakey.exportKey(format=format, passphrase=passphrase)
      if keyfile:
          with open(keyfile, 'w') as outputfile:
              outputfile.write(output)
      log.info("generated private key:\n\n%s", output)
      return rsakey
docs: Generate a new RSA key with the specified key size.
  
      :param int bits:
          bit size of the key modulus
  
      :param str keyfile:
          file the key should be written to
  
      :param str format:
          format for the key file, either PEM or DER
  
      :param str passphrase:
          pass phrase for encrypting the key file. If pass phrase is a callable
          its return value will be used.
  
      :return:
          RSA private key instance