A simple example of remote invocation (RPC, RMI) implemented in Python _python

Source: Internet
Author: User
Tags unique id in python

Remote calls make it possible to invoke objects, methods of the remote server in exactly the same way as local objects, methods, because we have hidden them through network programming. Remote invocation is the foundation of a distributed system.

Remote calls are generally divided into two types, remote procedure call (RPC), and remote method call (RMI).

Rpc

RPC is a function-level remote invocation, which transmits data mostly over HTTP, in the form of XML, JSON, serialized data, and so on. Here, use Python as an example of XML-RPC. First to server-side server.py:

Copy Code code as follows:

From Simplexmlrpcserver import Simplexmlrpcserver
def add (x, y):
return x + y
if __name__ = = ' __main__ ':
s = Simplexmlrpcserver ((' 127.0.0.1 ', 8080))
S.register_function (ADD)
S.serve_forever ()
S is a server object that is bound to a local 8080 port, and the Register_function () method registers the function add into S. Serve_forever () starts the server. client.py to a client:

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,client.py's console will output 7.

Let's take a look at the data that was passed in the Wireshark, the requested data:

Copy Code code 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>

Data for response:
Copy Code code as follows:

<?xml version= ' 1.0 '?>
<methodResponse>
<params>
<param>
<value>
<int> 7 </int>
</value>
</param>
</params>
</methodResponse>

Well, be concise and don't repeat it.

Rmi

RMI means a remote method call, which is larger than RPC because its base unit is an object. The general idea is to create an RMI server object that will instantiate an object that is registered in the RMI server object with the specified service name (or multiple objects, but not the same service name), and then start the RMI server. The server waits for the data sent by the client (including service name, function name, parameter) to return the processing result to the client. Pyro4 is a python based RMI implementation, let's use Pyro4 to create an RMI server, see server2.py:

Copy Code code 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 that Pyro4 generates with its own method for the Greeting_maker object, including the socket and the unique ID generated for greeting_maker. This ID is equivalent to the service name, and of course you can 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 URI of the Greeting_maker to Pyro4.proxy, you can think of a connection to the Greeting_maker on the server side and then call the Greeting_maker Get_fortune () method. If name is Letian, then the result of print Greeting_maker.get_fortune (name) is Hello, Letian ...

Related Article

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.