Example: Accessing a service from Python
You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules
First of all you will need the necessary XMLRPC client libraries for your python installation. On debian, and probably on ubuntu this is part of the default Python setup.
This example will show how to load a node using a very basic Python script.
First we need to import the xmlrpc libraries and set the server url:
import xmlrpclib
server = xmlrpclib.Server("http://example.com/?q=services/xmlrpc")
Accessing the available methods:
print server.system.listMethods()
Learning more about the node.load service using methodHelp:
print server.system.methodHelp('node.load')
The output will be: Returns a node.
Lets see what is the return type, and what arguments it needs:
print server.system.methodSignature('node.load')
Output: ['struct', 'int', 'array'] where the first element is the return type, the others the arguments.
From the code of node_services we can see that the second argument for the node.load method is optional:
// node.load
array(
'#method' => 'node.load',
'#callback' => 'node_service_load',
'#args' => array(
array(
'#name' => 'nid',
'#type' => 'int',
'#description' => t('A node id.')),
array(
'#name' => 'fields',
'#type' => 'array',
'#optional' => TRUE,
'#description' => t('A list of fields to return.'))),
'#return' => 'struct',
'#help' => t('Returns a node.')),
So the final method call will look like this:
nid = 1
print server.node.load(nid, [])
where the output is a struct of node 1, as defined. The second argument is an empty array, since its optional.
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion