jjia6395 jjia6395 -5 years ago 378
Python Question

How to send a xml-rpc request in python?

I was just wondering, how would I be able to send a

request in python? I know you can use
, but how do I send out a request in
to access a function?

I would like to see the

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

<?xml version="1.0"?>
<value><string>Hello World!</string></value>

and get back the response

Answer Source

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),

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

# Run the server's main loop

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download