The preceding example is composed of two main functions: pyro_server.py and pyro_client.py.
In pyro_server.py, the Server class object provides the welcomeMessage() method, returning a string equal to the name inserted in the client session:
class Server(object):
@Pyro4.expose
def welcomeMessage(self, name):
return ("Hi welcome " + str (name))
Pyro4 uses daemon objects to dispatch incoming calls to appropriate objects. A server must create just one daemon that manages everything from its instance. Each server has a daemon that knows about all the Pyro objects that the server provides:
daemon = Pyro4.Daemon()
As for the pyro_client.py function, the remote call is first performed and creates a Proxy object. In particular, the Pyro4 client uses proxy objects to forward method calls to the remote objects, and then passes the results back to the calling code:
server = Pyro4.Proxy("PYRONAME:server")
In order to execute a client-server connection, we need to have a Pyro4 nameserver running. In Command Prompt, type the following:
C:>python -m Pyro4.naming
After this, you'll see the following message:
Not starting broadcast server for localhost.
NS running on localhost:9090 (127.0.0.1)
Warning: HMAC key not set. Anyone can connect to this server!
URI = PYRO:Pyro.NameServer@localhost:9090
The preceding message means that the nameserver is running in your network. Finally, we can start the server and the client scripts in two separate Windows consoles:
- To run pyro_server.py, just type the following:
C:>python pyro_server.py
- Following that, you'll see something like this:
Ready. Object uri = PYRO:obj_76046e1c9d734ad5b1b4f6a61ee77425@localhost:63269
- Then, run the client by typing the following:
C:>python pyro_client.py
- The following message will be printed out:
What is your name?
- Insert a name (for example, Ruvika):
What is your name? Ruvika
- The following welcome message will be displayed:
Hi welcome Ruvika