tags:

views:

166

answers:

2

I was just wondering, how would I be able to send a xml-rpc request in python? I know you can use xmlrpclib, but how do I send out a request in xml to access a function?

I would like to see the xml response.

So basically I would like to send the following as my request to the server:

<?xml version="1.0"?>
<methodCall>
  <methodName>print</methodName>
  <params>
    <param>
        <value><string>Hello World!</string></value>
    </param>
  </params>
</methodCall>

and get back the response

+3  A: 

Here's a simple XML-RPC client in Python:

import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.myfunction(2, 4)

Works with this server:

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)

def myfunction(x, y):
    status = 1
    result = [5, 6, [4, 5]]
    return (status, result)
server.register_function(myfunction)

# Run the server's main loop
server.serve_forever()

To access the guts of xmlrpclib, i.e. looking at the raw XML requests and so on, look up the xmlrpclib.Transport class in the documentation.

Eli Bendersky
Yes, I have that working, but how would i be able to send a request simply by using xml? And then getting the response back in xml?
PCBEEF
@PCBEEF: see the `Transport` class, as I said in the bottom. Examples are in the documentation - and you can also always look under the hood (the sources)
Eli Bendersky
Found that setting the verbose=True flag when initiating the client, outputs the raw request and response
PCBEEF
A: 

What do you mean by "get around"? xmlrpclib is the normal way to write an XML-RPC client in python. Just look at the sources (or copy them to your own module and add print statements!-) if you want to know the details of how things are done.

Alex Martelli