[英]Access Python XML-RPC Service from Ruby Client
I have a XMLRPC server running: 我有一个运行的XMLRPC服务器:
#!/usr/bin/env python
from SimpleXMLRPCServer import SimpleXMLRPCServer
import subprocess
import os
def testls():
retcode=subprocess.call(["ls", "/etc"])
return retcode
server = SimpleXMLRPCServer(('192.168.15.20',8888),logRequests=True)
server.register_function(testls)
server.register_introspection_functions()
try:
server.serve_forever()
finally:
server.server_close()
I need to be able to call it from a ruby client. 我需要能够从红宝石客户端调用它。 From what I have found.
从我所发现的。 it seems that it should be this:
似乎应该是这样的:
require xmlrpc/client
server = XMLRPC::Client.new2("http://192.168.15.20:8888/")
But it returns the following, which suggests that it is not able to reach the service. 但是它返回以下内容,这表明它无法访问该服务。
=> #<XMLRPC::Client:0x2ab0f5caacf8 @parser=nil, @timeout=30, @path="/", @password=nil, @http_header_extra=nil, @use_ssl=false, @host="192.168.15.20", @user=nil, @proxy_port=nil, @auth=nil, @cookie=nil, @create=nil, @port=8888, @http=#<Net::HTTP 192.168.15.20:8888 open=false>, @proxy_host=nil, @http_last_response=nil>
I had better success with the following, but I was not able to invoke the method: 我在以下方面取得了较好的成功,但是无法调用该方法:
irb(main):069:0> server = XMLRPC::Client::Proxy.new("http://192.168.15.20","8888")
=> #<XMLRPC::Client::Proxy:0x2ab0f5c4ef48 @args=[], @server="http://192.168.15.20", @meth=:call, @prefix="8888.">
irb(main):070:0> s = server.call('system.listMethods')
NoMethodError: undefined method `call' for "http://192.168.15.20":String
from /usr/lib/ruby/1.8/xmlrpc/client.rb:608:in `send'
from /usr/lib/ruby/1.8/xmlrpc/client.rb:608:in `method_missing'
from (irb):70
from :0
What is the appropriate code to invoke a call from Ruby to a Python XML-RPC Server? 调用从Ruby到Python XML-RPC Server的调用的合适代码是什么?
I know this is a rather old question, but since it doesn't have a selected answer, I thought I'd share my insights: 我知道这是一个比较老的问题,但是由于没有选定的答案,我想分享一下我的见解:
In ruby, the class XMLRPC::Client
represents a lower level interface to the server: 在ruby中,类
XMLRPC::Client
代表服务器的下层接口:
server = XMLRPC::Client.new( "192.168.15.20", 8888 )
server.call 'system.listMethods'
The class XMLRPC::Client::Proxy
is a higher level interface to (parts of) the server that allows for a more natural server method invocation: XMLRPC::Client::Proxy
是服务器(部分)的高层接口,允许更自然的服务器方法调用:
system = server.proxy('system')
system.listMethods
Similarly, to access the server's 'root' methods (like those registered via register_function
in Python): 类似地,为了访问服务器的“根”的方法(如那些通过注册
register_function
在Python):
main = server.proxy
main.testls
I'm not familiar with xmlrpc in Ruby, but I think you have to pass an XMLRPC::Client Object to XMLRPC::Client::Proxy. 我不熟悉Ruby中的xmlrpc,但我认为您必须将XMLRPC :: Client对象传递给XMLRPC :: Client :: Proxy。 Try stuff like this:
试试这样的东西:
client = XMLRPC::Client.new( "http://192.168.15.20", 8888 )
server = XMLRPC::Client::Proxy.new( client, 8888 )
server.call "system.listMethods"
Maybe it works this way. 也许它是这样工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.