舉例:使用XML庫的方式,實現RPC通訊,xmlrpc

來源:互聯網
上載者:User

舉例:使用XML庫的方式,實現RPC通訊,xmlrpc

1、先說結論:使用xml-rpc的機制可以很方便的實現伺服器間的RPC調用。

2、實驗結果如下:

 

 

3、源碼如下:

伺服器端的原始碼如下:

import operator, mathfrom SimpleXMLRPCServer import SimpleXMLRPCServerfrom functools import reducedef 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 xmlrpclibdef 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()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.