一、简单介绍
为了解决在系统的80port提供RPC的服务。而又不影响正在运行的WEB服务。人们想出了用HTTP协议传输RPC包的办法。对于差点儿是专门用于传输文本的HTTP协议。要在其上传输RPC封包。最方便的方法莫过于把RPC封包编码成文本形式——比如XML文件。
XML- RPC(http://www.xml-rpc.com)是由美国UserLand公司指定的一个RPC协议。它将RPC信息封包编码为XML,然后通过 HTTP传输封包;
简单的理解:
将数据定义为xml格式。通过http协议进行远程传输。
二、优点
1. 传输复杂的数据。
2. 通过程序语言的封装,实现远程对象的调用。
三、Python中xmlrpc应用
server端:
from SimpleXMLRPCServer import * #import SimpleXMLRPCRequestHandler def div(x,y): return x -y class Math: def _listMethods(self): return ['add','pow'] def _methodHelp(self,method): if method == 'add': return "add(2,3) =>5" elif method == 'pow': return "pow(x,y[,z])=>number" else: return "" def _dispatch(self,method,params): if method == 'pow': return pow(*params) elif method == 'add': return params[0] + params[1] else: raise 'bad method' server = SimpleXMLRPCServer(("localhost",8000)) # register_introspection_functions:Registers the XML-RPC introspection methods in the system namespace server.register_introspection_functions() server.register_function(div,"div") server.register_function(lambda x,y:x*y,'multiply') server.register_instance(Math()) #serve_forever:Handle one request at a time until shutdown. #Polls for shutdown every poll_interval seconds. Ignores #self.timeout. If you need to do periodic tasks, do them in #another thread. server.serve_forever()
client:
import xmlrpclib s = xmlrpclib.ServerProxy('http://localhost:8000') print(s.system.listMethods()) print(s.pow(2,3)) print(s.add(2,3)) print(s.div(3,2)) print(s.multiply(4,5))
执行情况:
[maokx@maokexu1 xmlrpc]$ python server.py /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders /usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 - [maokx@maokexu1 xmlrpc]$ python client.py ['add', 'div', 'multiply', 'pow', 'system.listMethods', 'system.methodHelp', 'system.methodSignature'] 8 5 1 20 [maokx@maokexu1 xmlrpc]$ python client.py ['add', 'div', 'multiply', 'pow', 'system.listMethods', 'system.methodHelp', 'system.methodSignature'] 8 5 1 20 [maokx@maokexu1 xmlrpc]$