Skip to content

Instantly share code, notes, and snippets.

@level09
Created December 23, 2011 16:01
Show Gist options
  • Save level09/1514559 to your computer and use it in GitHub Desktop.
Save level09/1514559 to your computer and use it in GitHub Desktop.
Create Drupal Node with python thorough XMLRPC Service
#! /usr/bin/python
import xmlrpclib, pprint
class DrupalNode:
def __init__(self, title, body, ntype='article', uid=1, username ='admin'):
self.title = title
self.body = body
self.type = ntype
self.uid = uid
self.status = 1
self.name = username
config = {
'url': 'http://example.com/services/xmlrpc',
'username': 'admin',
'password': 'nidal',
}
s = xmlrpclib.ServerProxy(config['url'])
#print s.system.listMethods()
res = s.system.connect()
session = s.user.login(config['username'], config['password']);
#print session
sessname = session['session_name']
sessid = session['sessid']
user = session['user']
n = DrupalNode('test node','node body ...')
try:
s.node.create(sessid, n)
except xmlrpclib.Fault, err:
print "A fault occurred"
print "Fault code: %d" % err.faultCode
print "Fault string: %s" % err.faultString
@dgtlmoon
Copy link

try:
s.node.create(sessid, n)

I dont think it supports 'sessid' being sent here anymore

@level09
Copy link
Author

level09 commented Jan 17, 2012

since protocol inspection is totally broken, I'm not really sure how to get this to work.

http://drupal.org/node/839330

@dgtlmoon
Copy link

yes, but check out my fork, i got it work by using Cookie/header handling for the session

@level09
Copy link
Author

level09 commented Jan 17, 2012

Cool, could you share a link please ?

@dgtlmoon
Copy link

@level09
Copy link
Author

level09 commented Jan 17, 2012

Thanks, much appreciated

@level09
Copy link
Author

level09 commented Jan 22, 2012

I got your code working fine ( I had to give anonymous permissions to create nodes) .

but for some reason I'm unable to create taxonomy terms (D7) , this code fails :

new_node = { 'type': 'page',
                 'uid' : '1',
                 'name': 'admin',
                 'title': 'Just a little test',
                 'language' : 'und',
                 'body': { 'und' : { '0' : {'value' : 'Ordenar bibliotecas es ejercer de un modo silencioso el arte de la critica. --- Jorge Luis Borges. (1899-1986) Escritor argentino.' } }} ,
                 'field_price': { 'und' : { '0' : {'value' : 'this is the price ... ' } }} ,
                 'field_color': { 'und' : { '0' :  {'tid': '1'}  }} ,
    }

have you tried creating nodes that has term reference fields ?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment