Implement the client and server models based on the ICE framework in python

Source: Internet
Author: User

Implement the client and server models based on the ICE framework in python

 

ICE (Internet Communication Engine) is a Communication middleware implemented by zeroc.

Features:

1. Multiple Languages Support C ++, Java, python, C #, etc,

2. Support for distributed systems, including load balancing, location services, and real-time startup of computing nodes.

3. Provides a publishing-subscription mechanism for message generation ICEStorm

 

I. Writing slice files must be implemented according to the slice-defined syntax.

Printer. ice

1 2 3 4 5 module Demo { interface Printer { void printString(string s); }; };

 

2. Compile the slice code. The official tutorial provides the command line compilation method:

? Slice2py Printer. ice

This method also requires additional installation of the slice2py command. To save trouble, we use dynamic loading of the slice file in the program and compiling it.

?

3. Implement server code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 43 44 import sys, traceback, Ice # Dynamically load and compile slice files Ice.loadSlice("./Printer.ice") # Demo is the name of the module exported from Printer. ice. import Demo # Implement a service class class PrinterI(Demo.Printer): def printString(self, s, current=None): print s status = 0 ic = None try: # Initialize ice run time (ice run time) Ice. Communicator ic = Ice.initialize(sys.argv) # Initialize an adapter named "SimplePrinterAdapter" # Use the default protocol TCP/IP listener port 10000 adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -p 10000") # Instantiate a working servant for our Printer interface object = PrinterI() # Add the instantiated servant to the adapter. His identifier is "SimplePrinter" adapter.add(object, ic.stringToIdentity("SimplePrinter")) # Activate the adapter adapter.activate() # Waiting for the end signal ic.waitForShutdown() except: traceback.print_exc() status = 1 if ic: # Clean up try: ic.destroy() except: traceback.print_exc() status = 1

 

4. Implement the client

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 import sys, traceback, Ice import Demo status = 0 ic = None try: # Initialize the runtime environment ic = Ice.initialize(sys.argv) # Obtain the proxy of the remote printer service base = ic.stringToProxy("SimplePrinter:default -p 10000") # Request the server to confirm: "Is this the proxy interface of Demo: Printer? " printer = Demo.PrinterPrx.checkedCast(base) if not printer: raise RuntimeError("Invalid proxy") # Remote call, which looks like a local service printer.printString("Hello World!") except: traceback.print_exc() status = 1 if ic: # Clean up try: ic.destroy() except: traceback.print_exc() status = 1 sys.exit(status)

 

 


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.