Skip to content

Instantly share code, notes, and snippets.

@amirkdv
Created April 18, 2022 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirkdv/0417a7ae290881cc5460be44787fcd5b to your computer and use it in GitHub Desktop.
Save amirkdv/0417a7ae290881cc5460be44787fcd5b to your computer and use it in GitHub Desktop.
This is a toy example of XML-RPC in Python and Nodejs.

XML-RPC Example

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment