js xmlrpc + python xmlrpc(使用记录)

以下代码为工作使用记录代码,请注明转载自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 + 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值