文章目录
以下代码为工作使用记录代码,请注明转载自js xmlrpc + python xmlrpc(使用记录)
node js xmlrpc
-
npm install xmlrpc
下载xmlrpc nodejs包 -
服务端
var xmlrpc = require('xmlrpc') //xmlrpc.createServer创建一个http xmlrpc server === new Server(options, false, callback) //xmlrpc.createSecureServer 创建 HTTPS xmlrpc server === new Server(options, true, callback) var server = xmlrpc.createServer({ host: 'localhost', port: 9090 }) // server 注册一个监听函数 // 的第一个参数为监听的函数名 // 第二个参数是函数内容(err,参数列表,回调给请求来的客户端回复的函数) server.on('anAction', function (err, params[, cllback]) { console.log('Method call params for \'anAction\': ' + params) //遵循xmlrpc规则 第一个参数为 返回的 err 内容 // 第二个参数为返回的 Value callback(null, 'aResult') })
-
官方文档客户端示例
// Creates an XML-RPC client. Passes the host information on where to // make the XML-RPC calls. var client = xmlrpc.createClient({ host: 'localhost', port: 9090, path: '/'}) // Sends a method call to the XML-RPC server client.methodCall('anAction', params列表, function (error, value) { // Results of the method response if(error){ console.log('err:',error) } else { console.log('返回值value为',value) } })
-
简单封装后的示例
class IDVCommand { /** * IDVcmd实例化函数 * @param {string} host IP地址,默认127.0.0.1 * @param {number} port 端口 默认10000 * @param {boolean} isSecure 是否是https请求 true = https,false=http */ constructor(host = "127.0.0.1", port = 10000, isSecure = false) { // 默认参数,也可以写到类的外面 this.CONFIG = { timeout: 10000, need_reject: true, // 如果返回err了是否触发promise的reject,也就是err了是进入.catch还是.then rejectUnauthorized: false //是否跳过HTTPS的证书验证,这里是个坑,找了很久猜找到这个字段 }; this.CONFIG.host = host; this.CONFIG.port = port; this.CONFIG.isSecure = isSecure; } /** * @param {Object} options * [options.param] {ALL} Function parameters * [options.timeout] {number} xmlrpc network timeout * [options.need_reject] {boolean} * [options.rejectUnauthorized] {boolean} rejectUnauthorized */ _xmlrpc_call(fun, options = { }) { for (const i in options) { this.CONFIG[i] = options[i]; } var self = this; return new Promise(function(resolve, reject) { var _promise = { }; var Client = self.CONFIG.isSecure? xmlrpc.createSecureClient: xmlrpc.createClient; var client = Client(self.CONFIG); // 自定义超时检测 _promise._timeout = setTimeout(() => { console.error("method " + fun + " timeout -- " + self.CONFIG.timeout +