Last active
January 1, 2016 15:29
-
-
Save drmalex07/8164218 to your computer and use it in GitHub Desktop.
Invoke Drupal XML-RPC call through Python's xmlrpclib. #drupal #xmlrpclib #php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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