First, Introduction
To address the service of RPC on the 80 port of the system without affecting the Web service being executed, people have come up with the method of transmitting RPC packets with the HTTP protocol. The most convenient way to transfer RPC packets on an HTTP protocol that is almost exclusively for transmitting text is to encode the RPC packet into textual form-for example, an XML file.
XML-RPC (http://www.xml-rpc.com) is an RPC protocol specified by the United States Userland Company. It encodes the RPC information packets into XML and then transmits the packets over HTTP;
Simple to understand:
The data is defined in XML format and transmitted remotely through the HTTP protocol.
Second, the benefits
1. Transmission of complex data.
2. Implement the invocation of the remote object through the encapsulation of the program language.
Iii. XMLRPC application in Python
Server-side:
From simplexmlrpcserver Import *
#import Simplexmlrpcrequesthandler
def div (x, y):
return x-y
Class Math:
def _listmethods (self):
return [' Add ', ' POW ']
def _methodhelp (self,method):
If method = = ' Add ':
return "Add (2,3) =>5"
Elif method = = ' POW ':
return "POW (x,y[,z]) =>number"
Else:
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))
# Register_introspection_functions:registers The Xml-rpc introspection methods in the System namespace
Server.register_introspection_functions ()
Server.register_function (Div, "div")
Server.register_function (lambda x,y:x*y, ' multiply ')
Server.register_instance (Math ())
#serve_forever: Handle One request at a time until shutdown.
#Polls for shutdown every poll_interval seconds. Ignores
#self. Timeout. If you need to does periodic tasks, do them in
#another thread.
Server.serve_forever ()
Client:
Import Xmlrpclib
s = xmlrpclib. Serverproxy (' http://localhost:8000 ')
Print (S.system.listmethods ())
Print (S.pow (2,3))
Print (S.add (2,3))
Print (S.div (3,2))
Print (s.multiply (4,5))
Operating conditions:
[email protected] xmlrpc]$ python server.py
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:37] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
/usr/lib64/python2.6/rfc822.py, Readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, Readheaders, 205, Exit Readheaders
Localhost.localdomain--[16/nov/2014 23:25:45] "POST/RPC2 http/1.0" 200-
[email protected] xmlrpc]$ python client.py
[' Add ', ' div ', ' multiply ', ' pow ', ' system.listmethods ', ' system.methodhelp ', ' system.methodsignature ']
8
5
1
20
[email protected] xmlrpc]$ python client.py
[' Add ', ' div ', ' multiply ', ' pow ', ' system.listmethods ', ' system.methodhelp ', ' system.methodsignature ']
8
5
1
20
[Email protected] xmlrpc]$
Python's signature dish xmlrpc