This is a toy example of XML-RPC in Python and Nodejs.
Usage:
$ python server.py
In a separate shell:
$ python client.py
Or to use the node client
$ node client.js
const xmlrpc = require('xmlrpc'); // npm install xmlrpc | |
const client = xmlrpc.createClient({host: 'localhost', port: '8000'}) | |
client.methodCall('respond', [21], (error, resp) => { | |
if (error) { | |
console.log(`Something went wrong! ${error}`); | |
return 1; | |
} | |
console.log(resp.ptime.toLocaleString()); | |
console.log(JSON.stringify(resp)); | |
}); |
from xmlrpc.client import ServerProxy | |
import datetime | |
proxy = ServerProxy("http://localhost:8000/") | |
resp = proxy.respond(12) | |
print(resp) |
from datetime import datetime | |
from dataclasses import dataclass | |
from xmlrpc.server import SimpleXMLRPCServer | |
import xmlrpc.client | |
@dataclass | |
class Answer: | |
value: int | |
description: str = 'The secret to the universe' | |
@dataclass | |
class Response: | |
answer: Answer | |
ptime: datetime | |
@classmethod | |
def respond(cls, input): | |
return cls( | |
answer=Answer(value=2 * input), | |
ptime=datetime.now(), | |
) | |
server = SimpleXMLRPCServer(("localhost", 8000)) | |
print("Listening on port 8000...") | |
server.register_function(Response.respond, "respond") | |
server.serve_forever() |