Remote Call makes the method of calling remote server objects and methods similar to that of calling local objects and methods, because we have hidden them through network programming. Remote Call is the basis of a distributed system.
There are two types of remote calls: Remote Procedure Call (RPC) and remote method call (RMI ).
RPC
RPC is a function-level remote call. It transmits data over HTTP in the form of XML, JSON, and serialized data. Here, we use python as an xml-rpc example. First give server. py to the server:
Copy codeThe Code is as follows:
From SimpleXMLRPCServer import SimpleXMLRPCServer
Def add (x, y ):
Return x + y
If _ name _ = '_ main __':
S = SimpleXMLRPCServer ('127. 0.0.1 ', 127 ))
S. register_function (add)
S. serve_forever ()
S is a server object bound to the local port 8080. The register_function () method registers function add to s. Serve_forever () starts the server. And then the client. py:
From xmlrpclib import ServerProxy
If _ name _ = '_ main __':
S = ServerProxy ("http: // 127.0.0.1: 8080 ")
Print s. add (3, 4)
Now, run server. py, and then run client. py. The console where client. py is located outputs 7.
We use wireshark to check what the data passed during this period looks like and the requested data:
Copy codeThe Code is as follows:
<? Xml version = '1. 0'?>
<MethodCall>
<MethodName>
Add
</MethodName>
<Params>
<Param>
<Value>
<Int> 3 </int>
</Value>
</Param>
<Param>
<Value>
<Int> 4 </int>
</Value>
</Param>
</Params>
</MethodCall>
Response Data:
Copy codeThe Code is as follows:
<? Xml version = '1. 0'?>
<MethodResponse>
<Params>
<Param>
<Value>
<Int> 7 </int>
</Value>
</Param>
</Params>
</MethodResponse>
Well, I will not repeat it here.
RMI
RMI indicates a remote method call. The granularity is larger than that of RPC because the basic unit of RMI is an object. The general idea is as follows: Create an RMI server object and instantiate an object with the specified service name (or multiple objects, but the service name should not be the same) register to the RMI server object, and then start the RMI server. The data (including the service name, function name, and parameters) that the server waits for the client to send, and the processing result is returned to the client. Pyro4 is a python-based RMI implementation. Here we use Pyro4 to create an RMI server. See server2.py:
Copy codeThe Code is as follows:
Import Pyro4
Class GreetingMaker (object ):
Def get_fortune (self, name ):
Return "Hello, {0}. \ n". format (name)
Greeting_maker = GreetingMaker ()
Daemon = Pyro4.Daemon ()
Uri = daemon. register (greeting_maker)
Print "Ready. Object uri =", uri
Daemon. requestLoop ()
The uri variable is the uri generated by Pyro4 for the greeting_maker object using its own method, including the socket and the unique id generated for greeting_maker. This id is equivalent to the service name. You can also specify a more understandable service name. The following is the client client2.py:
Import Pyro4
Uri = raw_input ("Pyro uri:"). strip ()
Name = raw_input ("Your name:"). strip ()
Greeting_maker = Pyro4.Proxy (uri)
Print greeting_maker.get_fortune (name)
The uri to be entered is the uri generated by server2.py. By passing the greeting_maker uri to Pyro4.Proxy, you can consider the connection established with greeting_maker on the server side, and then call the get_fortune () method of greeting_maker. If the name is letian, the result of print greeting_maker.get_fortune (name) is Hello, letian ..