Example: Accessing a service from Python
Last modified: November 12, 2009 - 08:34
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:
<?php
// 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.

accessing secured service (I think)
Very new to drupal but though this might help someone else who is trying to access an authenticated service from another machine
I had to check allow anonymous access to the services module -> access services (but I allow nothing else unauthenticated)
Then I had to generate an api key and check both use keys and use sessid in the services configuration. I left domain checking alone.
Then in python a simple demoL
>>> import xmlrpclib
>>> api = 'your_key'
>>> url = "http://yoursite.com/?q=services/xmlrpc"
>>> uname = "you"
>>> passwd = "secret"
>>> drupal = xmlrpclib.ServerProxy(url)
>>> res = drupal.system.connect(api)
>>> lgin = drupal.user.login(api,res['sessid'],uname,passwd)
>>> drupal.node.load(api,lgin['sessid'],1,[])
Not sure if this is completely correct but it is a start.
Python Services API
I've started the Python Services API project, which provides a set of classes and and tests that Python developers can use as a basis for integration with the Services module.
I started a Python Drupal
I started a Python Drupal Services project on Github.
http://guaka.org/