On this page
xmlrpc
Prev: xml | Up: stdlib_module | Next: zipapp
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — xmlrpc
Tags: #public #python3 #stdlib
Import
import xmlrpc
Overview
Package providing an XML-RPC client (xmlrpc.client) and server (xmlrpc.server) for remote procedure calls over HTTP using XML payloads. Reach for xmlrpc when integrating with existing XML-RPC endpoints (such as Supervisor, Odoo, or legacy internal services); for new APIs, prefer JSON over HTTP or gRPC.
Quick Start
import xmlrpc.client
# Marshal and unmarshal an XML-RPC call payload without needing a live network server
payload = xmlrpc.client.dumps((6, 7), methodname="multiply")
params, method = xmlrpc.client.loads(payload)
assert (method, params) == ("multiply", (6, 7))
Implementation Walkthrough
1. Calling a Remote Endpoint and Registering Server Handlers
Use ServerProxy as a context manager on the client side, and SimpleXMLRPCServer (with register_function or register_instance) on the server side:
import xmlrpc.client
from xmlrpc.server import SimpleXMLRPCServer
# Client usage
def call_remote_add(endpoint_url: str, a: int, b: int) -> int:
with xmlrpc.client.ServerProxy(endpoint_url, allow_none=True) as proxy:
return proxy.add(a, b)
# Server setup
def build_server(host: str, port: int) -> SimpleXMLRPCServer:
srv = SimpleXMLRPCServer((host, port), allow_none=True, logRequests=False)
srv.register_function(lambda a, b: a + b, "add")
return srv
Gotchas & Boundaries
- XML parsing on untrusted input:
xmlrpc.serverandxmlrpc.clientparse XML via Expat; do not exposeSimpleXMLRPCServerdirectly to untrusted public networks without request size limits and TLS (reverse proxy). - Limited type system: XML-RPC supports
int(32-bit by default),float,bool,str,list,dict(string keys only),datetime, andbytes(Binary). Passallow_none=Trueon both client and server if you needNone. - Single-threaded server:
SimpleXMLRPCServerhandles one request at a time; combinesocketserver.ThreadingMixInwithSimpleXMLRPCServerif you need concurrency.
Linked from
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/xml Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zipapp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/base64 Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/email Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/json Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/mailbox Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/mimetypes Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib