2012-02-16 10 views
1

我想在Ruby 1.9中使用內置的XMLRPC。不幸的是,XML-RPC未記錄在ruby-docs.org,所以我試圖建立一個基於由谷歌發現例子測試代碼:如何在Ruby中正確使用內置的XMLRPC?

# Server.rb 
require "xmlrpc/server" 
server = XMLRPC::Server.new(1234) 
server.add_handler("test") { |msg| return "responce for #{msg}" } 
server.serve() 

# Client.rb 

require "xmlrpc/client" 
server = XMLRPC::Client.new("localhost", "/", 1234) 
server.call("test", 42) == "responce for 42" 

不幸的是,這是不工作在Windows和OSX。 Server.rb失敗,出現模糊錯誤:

C:/Ruby193/lib/ruby/1.9.1/xmlrpc/client.rb:414:in `call': Uncaught exception unexpected return in method test (XMLRPC::FaultException) 
     from client.rb:6:in `<main>' 

也許有人知道我的錯誤是什麼?

+0

如果您在文件/ lib目錄/ XMLRPC/README.rdoc有ruby19-stdlib.chm有一些例子。網址可能不是最新的,但總體思路就在那裏。你應該分享你從哪裏得到你的代碼。 – 2013-03-28 16:07:33

回答

7

它的另一種方式用塊:

#server.rb: 
require "xmlrpc/server" 
server = XMLRPC::Server.new(1234) 
server.add_handler('my_test.test') { |msg|"responce for #{msg}" } 

#client.rb 
require "xmlrpc/client" 
client = XMLRPC::Client.new("localhost", "/", 1234) 
s = client.call('my_test.test','asd') 
+0

謝謝,這是我的錯:我忘記了Ruby放在塊O_O內的'return'語句中的神祕規則。 – grigoryvp 2012-02-16 12:34:52

2

你明白了吧。這是您可以使用的tutorial。你的榜樣需要稍加修改,你必須通過一個對象add_handler將用於提供給您的RPC調用:

# server.rb 
require "xmlrpc/server" 

class MyClass 
    def dosomething(a) 
    "response for #{a}" 
    end 
end 

server = XMLRPC::Server.new(1234) 
server.add_handler("test", MyClass.new) 
server.serve 

# client.rb 
require "xmlrpc/client" 
server = XMLRPC::Client.new("localhost", "/", 1234) 
puts server.call("test.dosomething", 42) == "response for 42" 
+0

因此,無法通過鎖來添加處理程序,正如其他教程中所寫的那樣? – grigoryvp 2012-02-16 11:22:01

1

我認爲這可能幫助: http://www.ntecs.de/ruby/xmlrpc4r/howto.html

#server.rb 
require "xmlrpc/server" 
server = XMLRPC::Server.new(1234) 

class MyHandler 
    def test(msg) 
     "message #{msg}" 
    end 
end 
server.add_handler(XMLRPC::iPIMethods("my_test"), MyHandler.new) 
server.serve 

#client.rb 
require "xmlrpc/client" 
server = XMLRPC::Client.new("localhost", "/", 1234) 
s = server.call('my_test.test','hello!') 
+0

因此,無法通過鎖來添加處理程序,正如其他教程中所寫的那樣? – grigoryvp 2012-02-16 11:22:19

2

請注意:默認XML-RPC/client.rb IMPL。不支持基於https連接的客戶端證書。如果你想它,你必須使用不同的lib或補丁client.rb的東西,如:

# HG changeset patch 
# User Anonymous Coward <[email protected]> 
# Date 1338149770 -10800 
# Node ID f0557306c8e4f113507fb3bab8567391949fa302 
# Parent 3eae8e8f9e065ff6cdf1c95092ad5cca635c9eac 
patch client.rb to support https with client certificate. 

diff -r 3eae8e8f9e06 -r f0557306c8e4 client.rb 
--- a/client.rb Sun May 27 22:20:18 2012 +0300 
+++ b/client.rb Sun May 27 23:16:10 2012 +0300 
@@ -292,8 +292,8 @@ 

    # Constructors ------------------------------------------------------------------- 

- def initialize(host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil, 
-     user=nil, password=nil, use_ssl=nil, timeout=nil) 
+ def initialize(host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil, 
+  user=nil, password=nil, use_ssl=nil, timeout=nil, cacert=nil, cert=nil, key=nil) 

     @http_header_extra = nil 
     @http_last_response = nil 
@@ -311,6 +311,10 @@ 
     if use_ssl 
     require "net/https" 
     @port = port || 443 
+  @cacert = cacert 
+  @cert = cert 
+  @key = key 
+ 
     else 
     @port = port || 80 
     end 
@@ -325,8 +329,19 @@ 

     # HTTP object for synchronous calls 
     Net::HTTP.version_1_2 
-  @http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port) 
-  @http.use_ssl = @use_ssl if @use_ssl 
+  @http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port) 
+  if @use_ssl 
+  @http.use_ssl = @use_ssl 
+  if nil != @cacert 
+   @http.ca_file = @cacert 
+   @http.verify_mode = OpenSSL::SSL::VERIFY_PEER 
+   @http.verify_depth = 5 
+  else 
+   @http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
+  end 
+  @http.cert = @cert 
+  @http.key = @key 
+  end 
     @http.read_timeout = @timeout 
     @http.open_timeout = @timeout 

@@ -366,7 +381,7 @@ 
     hash.each { |k,v| h[k.to_s.downcase] = v } 

     self.new(h['host'], h['path'], h['port'], h['proxy_host'], h['proxy_port'],  h['user'], h['password'], 
相關問題