尝试Python的XML-RPC远程调用

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

何为XML-RPC?

XML-RPC 是 XML Web 服务的鼻祖。它是一个用于远程过程调用(remote procedure call,RPC)的简单规范,这种调用使用 HTTP 作为传输协议,并使用 XML 词汇表作为消息有效负载。由于 XML-RPC 非常简单(整个规范打印出来还不到十页纸),它已经变得非常流行,现在大多数语言都有了标准的或已经可用的 XML-RPC 实现。这些语言中包括 Python,它在版本 2.2 中就开始捆绑 xmlrpclib(Fredrik Lundh 开发的 XML-RPC 实现)了。

首先,我们打算将CMS(Context Manager System)系统进行Python的改造,第一件事,先向外公开版本的变化,可供远程调用。

import SimpleXMLRPCServer

#定义自己的CMS类

class MyCMS:

def getVersion(self):#向外公开版本的方法

return "Powerd By Python 0.1a"

cms = MyCMS()

server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8888))

server.register_instance(cms)

print "Listening on port 8888"

server.serve_forever()#服务器执行,并监听8888端口

执行后截图:

650)this.width=650;" src="http://www.faridea.com/bbs/images/fileType/gif.gif" onload="javascript:if(this.width650)this.width=650;" align=middle border=0此主题相关图片

650)this.width=650;" onclick=javascript:window.open(this.src); src="http://www.faridea.com/bbs/images/upload/2005/01/08/121426.gif" onload="javascript:if(this.width650)this.width=650;" align=middle border=0

客户端调用代码,获得最新的版本信息

import xmlrpclib

server = xmlrpclib.ServerProxy("http://localhost:8888";)

version = server.getVersion()

print "version:"+version

执行后截图:

650)this.width=650;" src="http://www.faridea.com/bbs/images/fileType/gif.gif" onload="javascript:if(this.width650)this.width=650;" align=middle border=0此主题相关图片

650)this.width=650;" onclick=javascript:window.open(this.src); src="http://www.faridea.com/bbs/images/upload/2005/01/08/121456.gif" onload="javascript:if(this.width650)this.width=650;" align=middle border=0

总结:

比同等的JAVA实现代码量明显减少,使精力能够更多的集中到系统本身中来

JAVA的一个XmlRpc实现:http://ws.apache.org/xmlrpc/

JAVA调用代码如下:

XmlRpcClient xmlrpc = null;

try

{

xmlrpc = new XmlRpcClient("http://localhost:8888/";);

}

catch (MalformedURLException e)

{

e.printStackTrace();

}

Vector params = new Vector();

try

{

String result = (String) xmlrpc.execute("getVersion", params);

System.out.println(result);

}

catch (XmlRpcException e1)

{

e1.printStackTrace();

}

catch (IOException e1)

{

e1.printStackTrace();

}

执行效果如图:

650)this.width=650;" src="http://www.faridea.com/bbs/images/fileType/gif.gif" onload="javascript:if(this.width650)this.width=650;" align=middle border=0此主题相关图片

650)this.width=650;" onclick=javascript:window.open(this.src); src="http://www.faridea.com/bbs/images/upload/2005/01/08/143009.gif" onload="javascript:if(this.width650)this.width=650;" align=middle border=0

想了解更多,请到这里:

http://www.python.org/doc/current/lib/module-xmlrpclib.html

http://www.python.org/doc/current/lib/module-SimpleXMLRPCServer.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航