One, remote procedure call RPC
XML-RPC is-a-Remote Procedure call method, that uses, passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server was named by a URI) and get back Structur Ed data. This module supports writing XML-RPC client code; It handles all the details of translating between Conformable Python objects and XML on the wire.
Simply, the client can invoke the method provided on the server and then get the result of execution. Similar to the WebService.
Recommended to view XMLPRC source files: C:\Python31\Lib\xmlrpc
Second, the example
1) Server
Copy Code code as follows:
From Xmlrpc.server import Simplexmlrpcserver
From Xmlrpc.server import Simplexmlrpcrequesthandler
def div (x,y):
return x-y
Class Math:
def _listmethods (self):
# This method must is present for System.listmethods
# to work
return [' Add ', ' POW ']
def _methodhelp (Self, method):
# This method must is present for System.methodhelp
# to work
If method = = ' Add ':
Return "Add (2,3) => 5"
Elif method = = ' POW ':
Return "POW (x, y[, z]) => number"
Else
# By convention, return empty
# string If no help is available
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)
Server.register_introspection_functions ()
Server.register_function (Div, "div")
Server.register_function (lambda x,y:x*y, ' multiply ')
Server.register_instance (Math ())
Server.serve_forever ()
2) Client
Copy Code code as follows:
Import Xmlrpc.client
s = xmlrpc.client.ServerProxy (' http://localhost:8000 ')
Print (S.system.listmethods ())
Print (S.pow (2,3)) # Returns 28
Print (S.add (2,3)) # Returns 5
Print (S.div (3,2)) # Returns 1
Print (s.multiply (4,5)) # Returns 20
3) Result