XML-RPC client and server in the Python implementation (based on xmlrpclib SimpleXMLRPCServer module)

RPC is an abbreviation for Remote Procedure Call, and translated into Chinese is a remote method invocation, a call to a technical process (method) on a remote machine on the local machine, this process is also commonly known as "distributed computing" , in order to improve the technical individual discrete machines "interoperability" and invented.

XML-RPC stands for XML Remote Procedure Call, ie, XML remote method invocation.

It is a set of allowed to run on different operating systems, different environmental norms and procedures to achieve the realization of a series of Internet-based procedure call.
This remote procedure calls using http as the transport protocol, XML as the encoding format to transmit information.
Xml-Rpc defined kept simple as possible, but can be transmitted, the process returns to complex data structures.
XML-RPC in Python:

1, class libraries: xmlrpclib generally used in a client, this module is used to call a function registered in XML-RPC server, xmlrpclib not a type-safe module, unable to resist the malicious data structure, some processing work in this area need to pay to the developers themselves.

2, libraries: SimpleXMLRPCServer is generally used on the server side, this module is used to construct a basic framework for XML-RPC server

3, a basic structure of the XML-RPC Server and start: D: \ Program Files \ eclipse-workspace \ HessianPro \ basicXmlRPC \ XmlRPCserver.py

#!/usr/bin/python
# -*- coding:UTF-8 -*-


from  SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler


class XmlRPCserver():
    def datasum(self,x,y):
        print u'加法结果:',x+y
        return x+y
    
    def datadiv(self,x,y):
        print u'除法结果:',x/y
        return x/y
    
    

if __name__ == '__main__':
    rpc_obj=XmlRPCserver()
    
    server=SimpleXMLRPCServer(("localhost", 8000))
    
    server.register_function(rpc_obj.datasum, 'datasum') #注册函数
    server.register_function(rpc_obj.datadiv,'datadiv')
    
    print "Listening on port 8000..."
    server.serve_forever () # call the state to keep waiting

  

 

4, a basic structure of the XML-RPC Client: D: \ Program Files \ eclipse-workspace \ HessianPro \ basicXmlRPC \ XmlRPCclient.py

#!/usr/bin/python
# -*- coding:UTF-8 -*-

import xmlrpclib

sp=xmlrpclib.ServerProxy('http://localhost:8000')
print sp.datasum(1,2)
print sp.datadiv(4,2)

 

 

 

 

Guess you like

Origin www.cnblogs.com/apple2016/p/11763925.html