Previous page

Next page

Locate page in Contents

Creating a Basic Application

The following steps are required in any programs using the Parallels Python API:

  1. Import the prlsdkapi package. This is the main Parallels Python API package containing the majority of the classes and additional modules.
  2. Initialize the API using the prlsdkapi.init_desktop_sdk() function. To verify that the API was initialized successfully, use the prlsdkapi.is_sdk_initialized() function.
  3. Create an instance of the prlsdkapi.Server class. The Server class provides methods for logging in and for obtaining other object references (a virtual_machine object in particular).
  4. Perform the login operation using the Server.login_local() method.

To exit gracefully, the program should perform the following steps:

  1. Log off using the Server.logoff() method. The method does not accept any parameters and simply ends the client session.
  2. Deinitialize the API using the prlsdkapi.deinit_sdk() function.

Example

#!/usr/bin/env python

# -*- coding: utf-8 -*-

#

# Example of prlsdkapi usage.

#

  

# Import the main Parallels Python API package.

import prlsdkapi

  

# Import some of the standard Python modules.

# We will not use all of them in this sample, but

# we will use them in other samples later.

import sys, time, getopt, operator, re, random

  

# Define constants for easy referencing of the Parallels Python API modules.

consts = prlsdkapi.prlsdk.consts

  

# An exception class to use to terminate the program.

class Halt(Exception):

    pass

  

"""

Parallels Service login.

  

    @param server: A new instance of the prlsdkapi.Server class.

    @param user: User name (must be a valid host OS user).

    @param password: User password.

    @param security_level: Connection secuirty level. Must be one of the

                           prlsdk.consts.PSL_xxx constants.

"""

def login_server(server, user, password, security_level):

  

    try:

        # The call returns a prlsdkapi.Result object on success.

        result = server.login_local('', 0, security_level).wait()

    except prlsdkapi.PrlSDKError, e:

        print "Login error: %s" % e

        raise Halt

    # Obtain a LoginResponse object contained in the Result object.

    # LoginResponse contains the results of the login operation.

    login_response = result.get_param()

    

    # Get the Parallels Desktop version number.

    product_version = login_response.get_product_version()

  

    # Get the host operating system version.    

    host_os_version = login_response.get_host_os_version()

  

    # Get the host UUID.

    host_uuid = login_response.get_server_uuid()

  

    print""

    print "Login successful"

    print""

    print "Parallels Desktop version: " + product_version

    print "Host OS verions:           " + host_os_version

    print "Host UUID:                 " + host_uuid

    print ""

  

###################################################################################

  

def main():

  

    # Initialize the library for Parallels Desktop.

    prlsdkapi.init_desktop_sdk()

  

    # Create a Server object and log in to Parallels Desktop.

    server = prlsdkapi.Server()

    login_server(server, "root", "secret", consts.PSL_NORMAL_SECURITY);

  

    # Log off and deinitialize the library.

    server.logoff()

    prlsdkapi.deinit_sdk()

    

if __name__ == "__main__":

    try:

        sys.exit(main())

    except Halt:

        pass