jjia6395 jjia6395 - 5 months ago 48
Python Question

How to send a xml-rpc request in python?

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

Answer

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.

Comments