Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active January 1, 2016 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drmalex07/8164218 to your computer and use it in GitHub Desktop.
Save drmalex07/8164218 to your computer and use it in GitHub Desktop.
Invoke Drupal XML-RPC call through Python's xmlrpclib. #drupal #xmlrpclib #php
import xmlrpclib
session_info = (None,None)
token = None
class CookieAwareTransport(xmlrpclib.Transport):
''' Enhance xmlrpclib.Transport so that it sends the
appropriate authn/authz headers
'''
def send_content(self, connection, request_body):
connection.putheader("X-Foo", "Baz")
if session_info[0]:
cookie = "%s=%s;" % session_info
connection.putheader('Cookie', cookie)
print "Added a cookie header: %s" % (cookie)
if token:
connection.putheader('X-Csrf-Token', token)
print "Added a token header: %s" % (token)
xmlrpclib.Transport.send_content(self, connection, request_body)
# Try it!
url = "https://drupal7-1.localdomain/drupal7-a/api/1";
proxy = xmlrpclib.ServerProxy(url, transport=CookieAwareTransport())
auth = proxy.user.login('admin', 'admin')
session_info = (auth['session_name'], auth['sessid'])
t = proxy.user.token()
token = t['token']
# Now we are logged-in, attempt to create a new 'page' node
x = {
'type': 'page',
'title': 'Hello XMLRPC-created node',
'body': 'I am created via an XMLRPC call',
}
r = proxy.node.create(x)
print r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment