XML-RPC in Python简介

心得:本地 ————- 远程
调用方法就包括了:方法名、参数、返回值
这些如果在本地就可以直接传入调用处理。如果是远程的话步骤如下:
第一、将参数之类的信息转换成XML格式的数据流
第二、传入到服务器端解析之后进行执行返回结果封装成XML格式数据包
第三、客户端接收到这些XML的数据包同样进行解析得到想要的数据格式。
用网络传递的话,和系统和语言都没有关系了
这样的话我就可以在本地写一个JAVA的代码去调用一个NET的服务接口了。
通过一些语言的内库直接实现这种XML与文本流的互换。我们主要关心的就是如何写好服务器端的函数逻辑了!

import xmlrpclib
server = xmlrpclib.Server(”http://www.example.com/RPC2“)   #这个就相当于是一个对象了。能够通过这个对象去调用它的方法
result = server.calculate_a_plus_b(5,8)

XML-RPC的不足:
毕竟要经过”函数调用”->XML转换过程,运行时要付出时间的代价。但是开发时间有时候比运行时效率更重要。
(要将文本内容转换成XML一种类型的所以效率上不是很理想的!)

其它类似XML-RPC的协议:
SOAP:W3C提出的协议,功能比XML-RPC强大,但是太复杂。  

哦原来它俩是一家的呀。编写WEB服务接口就要用SOAP了!

将数据定义为xml格式,通过http协议进行远程传输。(服务端与客户端都是这样处理的!)

需要掌握的第一:服务器端如何编写
第二、客户端如何编写
可能的方法调用示例如下:
A list of possible usage patterns follows:

1. Install functions:

server = SimpleXMLRPCServer((”localhost”, 8000))
server.register_function(pow)
server.register_function(lambda x,y: x+y, ‘add’)
server.serve_forever()

2. Install an instance:

class MyFuncs:
    def __init__(self):
        # make all of the string functions available through
        # string.func_name
        import string
        self.string = string
    def _listMethods(self):
        # implement this method so that system.listMethods
        # knows to advertise the strings methods
        return list_public_methods(self) + \
                ['string.' + method for method in list_public_methods(self.string)]
    def pow(self, x, y): return pow(x, y)
    def add(self, x, y) : return x + y

server = SimpleXMLRPCServer((”localhost”, 8000))
server.register_introspection_functions()
server.register_instance(MyFuncs())
server.serve_forever()

3. Install an instance with custom dispatch method:

class Math:
    def _listMethods(self):
        # this method must be present for system.listMethods
        # to work
        return ['add', 'pow']
    def _methodHelp(self, method):
        # this method must be present for system.methodHelp
        # to work
        if method == ‘add’:
            return “add(2,3) => 5″
        elif method == ‘pow’:
            return “pow(x, y[, z]) => number”
        else:
            # By convention, return empty
            # string if no help is available
            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))
server.register_introspection_functions()
server.register_instance(Math())
server.serve_forever()

4. Subclass SimpleXMLRPCServer:

class MathServer(SimpleXMLRPCServer):
    def _dispatch(self, method, params):
        try:
            # We are forcing the ‘export_’ prefix on methods that are
            # callable through XML-RPC to prevent potential security
            # problems
            func = getattr(self, ‘export_’ + method)
        except AttributeError:
            raise Exception(’method “%s” is not supported’ % method)
        else:
            return func(*params)

    def export_add(self, x, y):
        return x + y

server = MathServer((”localhost”, 8000))
server.serve_forever()

5. CGI script:

server = CGIXMLRPCRequestHandler()
server.register_function(pow)
server.handle_request()

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • RSS
  • Slashdot
  • Technorati
  • TwitThis

Related posts:

  1. Python 多线程 XML RPC的实现
  2. 安装django环境
  3. python通过SMTPLIB发送邮件

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Contact us

Admin: Bryan Wu