1

我正在尝试理清如何使用 XMLRPC::Client 类的 new3 方法。这就是我所拥有的:

#!/usr/bin/ruby

require "xmlrpc/client"

params = {
  host: "https://192.168.1.2",
  path: "rpc/api",
  port: "443",
  proxy_host: "",
  proxy_port: "",
  user: "username",
  password: "password",
  use_ssl: "true",
  timeout: 300,
}

session = XMLRPC::Client.new3(params)
session.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE)

sys_ver = session.call('api.systemVersion')
users = session.call("user.listUsers", session)

如果我使用 new2 方法,脚本将连接并返回 404 错误。但是,如果我使用 new3 它会返回

/usr/lib/ruby/1.9.1/net/http.rb:762:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
    from /usr/lib/ruby/1.9.1/net/http.rb:762:in `open'
    from /usr/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    from /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
    from /usr/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    from /usr/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    from /usr/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    from /usr/lib/ruby/1.9.1/net/http.rb:750:in `start'
    from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:535:in `do_rpc'
    from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:420:in `call2'
    from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:410:in `call'
    from sat_test.rb:24:in `<main>'

我是否正确使用了 new3 方法?

另外,如果我改用新方法,是否需要为每个参数(proxy_host=、proxy_port= 等)设置 parameter=value?

4

1 回答 1

2

问题似乎更多是因为我的脚本编码错误。这就是我现在所拥有的:

#!/usr/bin/ruby

require "xmlrpc/client"

params = {
  host: "REDACTED",
  path: "/rpc/api",
  use_ssl: "true",
  user: "REDACTED",
  pass: "REDACTED"
}

client = XMLRPC::Client.new3(params)
client.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE)

session = client.call('auth.login', params[:user], params[:pass])

sys_ver = session.call('api.systemVersion', session)
users = session.call("user.listUsers", session)
users.each { |uname| puts uname }

我已经删除了 params 哈希中不必要的参数,并确保所有内容都被正确引用。

它有效,只给我留下一个 404 错误,这脚本的问题。但是,至少它现在连接并给了我一些东西。

于 2013-04-22T21:14:53.910 回答