zoukankan      html  css  js  c++  java
  • 举例:使用XML库的方式,实现RPC通信

    1、先说结论:使用xml-rpc的机制可以很方便的实现服务器间的RPC调用。

    2、试验结果如下:

     

    3、源码如下:

    服务器端的源代码如下:

    import operator, math
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from functools import reduce
    
    def main():
        server = SimpleXMLRPCServer(('127.0.0.1', 7001))
        server.register_introspection_functions()
        server.register_multicall_functions()
        server.register_function(addtogether)
        server.register_function(quadratic)
        server.register_function(remote_repr)
        
        print("Server ready")
        server.serve_forever()
        
    def addtogether(*things):
        """Add together everything in the list things ."""
        return reduce(operator.add, things)
        
    def quadratic(a, b, c):
        """Determine x values satisfying: a * x * x + b * x + c = 0"""
        b24ac = math.sqrt(b*b - 4.0*a*c)
        return list(set([(-b-b24ac) / 2.0*a, (-b+b24ac) / 2.0*a]))
        
    def remote_repr(arg):
        """return the repr() rendering of the supplied arg """
        return arg
        
    if __name__ == '__main__':
        main()

    客户端的代码如下:

    import xmlrpclib
    
    def main():
        proxy = xmlrpclib.ServerProxy('http://127.0.0.1:7001')
        
        print("Here are the functions supported by this server:")
        
        print("next calculator addtogether: ")
        print(proxy.addtogether('x','y','z'))
        print(proxy.addtogether('x','y','z'))
        
        print(proxy.addtogether('x','y','z'))
        print(proxy.addtogether('x','y','z'))
        for method_name in proxy.system.listMethods():
            if method_name.startswith('system.'):
                continue
                
            signatures = proxy.system.methodSignature(method_name)
            if isinstance(signatures, list) and signatures:
                for signature in signatures:
                    print('%s(%s)' %(method_name, signature))
                    
            else:
                print('%s(...)' %(method_name,))
                
            method_help = proxy.system.methodHelp(method_name)
            #if method_help:
            #    print(' ', methodHelp)
        
        print(proxy.addtogether('x','y','z'))
        print("addtogether result ")
                
    if __name__ == '__main__':
        main()
  • 相关阅读:
    叙旧
    注册表的基本操作(.Net)
    如何自己实现 JavaScript 的 new 操作符?
    装饰者模式和TypeScript装饰器
    彻底弄懂GMT、UTC、时区和夏令时
    Javascript 中 cookie 操作方式
    javascript实例教程:使用canvas技术模仿echarts柱状图
    实现memcached客户端:TCP、连接池、一致性哈希、自定义协议
    Linux终端快速检测网站是否宕机的6个方法
    爬虫是什么吗?你知道爬虫的爬取流程吗?
  • 原文地址:https://www.cnblogs.com/zhouhaibing/p/7005235.html
Copyright © 2011-2022 走看看