Python Xmlrpc Module
XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data.
Importing the Module
The xmlrpc package collects server and client modules.
import xmlrpc.client
import xmlrpc.server
Client (xmlrpc.client)
To make a call to an XML-RPC server, use xmlrpc.client.ServerProxy.
import xmlrpc.client
s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.pow(2, 3)) # Returns 2**3 = 8
print(s.add(2, 3)) # Returns 2+3 = 5
print(s.div(5, 2)) # Returns 5//2 = 2
Server (xmlrpc.server)
The xmlrpc.server module provides a basic server framework for XML-RPC servers.
SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
with SimpleXMLRPCServer(('localhost', 8000),
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Register a function under a different name
def adder_function(x, y):
return x + y
server.register_function(adder_function, 'add')
# Run the server's main loop
print("Serving XML-RPC on localhost port 8000...")
server.serve_forever()