Python JSON-RPC ExampleΒΆ

This is an example of how to use the Memset API with Python and the 3rd party module jsonrpclib for JSON-RPC client access.

Substitute API_KEY_HEX with a valid API key and it can be run with a Python 2 interpreter.

#!/usr/bin/env python
"""
    Memset API example with Python and JSON-RPC

    http://www.jsonrpc.org/specification

    Install the JSON-RPC library from

    https://pypi.python.org/pypi/jsonrpclib
"""

uri = "https://API_KEY_HEX:@api.memset.com/v1/jsonrpc/"

from jsonrpclib import Server, MultiCall
from pprint import pprint

def main():
    s = Server(uri)

    # get the product list
    r = s.service.list()
    pprint(r)

    # get the server list
    r = s.server.list()
    pprint(r)

    # get server information (one parameter)
    r = s.server.info(name="myserver1")
    pprint(r)

    # set a new nickname for a product (two parameters)
    r = s.service.set_nickname(name="myserver1", nickname="www")
    pprint(r)

    # reboot a server
    r = s.server.reboot(name="myserver1")
    pprint(r)

    # batch mode
    batch = MultiCall(s)
    batch.internal.echo(string="Hello World")
    batch.internal.version()
    batch.internal.noop()
    batch.internal.echo(string="Goodbye!")
    # run the batch - this returns an iterator which gives the results
    # in order that they were added
    r = batch()
    pprint(list(r))

if __name__ == "__main__":
    main()

Previous topic

Python Example

Next topic

Python Firewalling Example

This Page