Teach you how to use the XML library to implement RPC communication capabilities

Source: Internet
Author: User
1, first say the conclusion: the use of XML-RPC mechanism can be very convenient to implement RPC calls between servers.

2. The test results are as follows:

3. The source code is as follows:

The source code for the server side is as follows:

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 ()

The code for the client is as follows:

Import Xmlrpclibdef Main (): Proxy = Xmlrpclib. Serverproxy (' http://127.0.0.1:7001 ') print ("Here is the functions supported by this server:") print ("Next C Alculator addtogether: ") Print (Proxy.addtogether (' x ', ' y ', ' z ')) print (Proxy.addtogether (' x ', ' y ', ' z ')) print (p         Roxy.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.methodSigna                Ture (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 (", method Help) Print (Proxy.addtogether (' x ', ' y ', ' z ')) print ("Addtogether result") If __name__ = = ' __main__ ': Main () 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.