Skip to content
python_interpreter_client.rst 1.62 KiB
Newer Older
Simple client with python interpreter
=====================================

You can easily connect on a CloudControl server with a Python shell for
debugging purpose.

Before to start, make sure you have installed the ``python-sjrpc`` package::

    aptitude install python-sjrpc

1. Start a new Python shell
---------------------------

::

    Python 2.6.6 (r266:84292, Oct  9 2010, 12:24:52) 
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 

2. Import sjrpc
---------------

>>> from sjrpc.client import SimpleRpcClient
>>> from sjrpc.utils import ConnectionProxy

3. Import socket & SSL modules
------------------------------

>>> import socket
>>> import ssl

4. Create the socket object
---------------------------

>>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Wrap it into ssl socket wrapper to enable SSL (mandatory):

>>> sock = ssl.wrap_socket(sock, certfile=None, cert_reqs=ssl.CERT_NONE, ssl_version=ssl.PROTOCOL_TLSv1)

And connect it:

>>> sock.connect(('ip.add.res.s', 1984))

5. Create the client object
---------------------------

>>> client = SimpleRpcClient(sock)

You can also create a proxy object to easily call remote methods:

>>> p = ConnectionProxy(client)

6. Launch the client event loop
-------------------------------

To keep control on the interpreter, you need to launch the event loop in a
new thread:

>>> import threading
>>> threading.Thread(target=client.run).start()

7. Authenticate & play !
------------------------

>>> p.authentify('amillet', 'jeancloud')
>>> p.list_vm()
[]