7

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 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: 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)
Found that setting the verbose=True flag when initiating the client, outputs the raw request and response
5

I have pared down the source code in xmlrpc.client to a minimum required to send a xml rpc request (as I was interested in trying to port the functionality). It returns the response XML.

Server:

from xmlrpc.server import SimpleXMLRPCServer

def is_even(n):
    return n%2 == 0

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(is_even, "is_even")
server.serve_forever() 

Client:

import http.client

request_body = b"<?xml version='1.0'?>\n<methodCall>\n<methodName>is_even</methodName>\n<params>\n<param>\n<value><int>2</int></value>\n</param>\n</params>\n</methodCall>\n"

connection = http.client.HTTPConnection('localhost:8000')
connection.putrequest('POST', '/')
connection.putheader('Content-Type', 'text/xml')
connection.putheader('User-Agent', 'Python-xmlrpc/3.5')
connection.putheader("Content-Length", str(len(request_body)))
connection.endheaders(request_body)

print(connection.getresponse().read())

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.