From charlesreid1

Pyro ("Python Remote Objects") is a way of networking machines together so that computers or devices can remotely call Python objects on other machines. It is pretty magical.

This article refers to "host" (as the machine that is hosting the Pyro server, and the objects of interest) and "client" (as the machine that is connecting to the Pyro server and making the remote calls to the Pyro object).

Hello World with Pyro

Running a simple "Hello World" program with Pyro requires three steps:

1. Start the Pyro nameserver on the host

2. Register the remote object with Pyro

3. Call the remote object with the client

Step 1: Start the Host Nameserver

This step must only be run once in order to use Pyro on your host machine. You can start the Pyro server from the command line:

$ python -m Pyro4.naming --host 10.0.0.2

You can specify a number of different options when starting the nameserver. See the Pyro documentation page on the nameserver for more details.

Step 2: Create and Register Pyro Objects

The next step is to expand your object of interest to make it into a Pyro object. This means adding some extra code to create an instance of the object and register it with the nameserver.

First, we write code that is independent of Pyro. Here's an example dummy object:

import Pyro4

class Dummy(object):
    def __init__(self):
        print "Hi! I'm a dummy."
    def printme(self):
        print "dummy "*10

Now just below that we add a function so that, when this file is run directly, it will create and register an instance of a Dummy with Pyro:

if __name__=="__main__":
    Pyro4.config.HMAC_KEY = 'somelongrandomstringthatnobodywilleverguessinamillionyears'
    pyrodaemon = Pyro4.Daemon(host='10.0.0.2')
    d = Dummy()
    print pyrodaemon.register(d,'Dummy')
    pyrodaemon.requestLoop()

Step 3: Call the Remote Object from the Client

The last step is to call this Dummy remote object from our client computer. We can do this by

if __name__=="__main__":
    Pyro4.config.HMAC_KEY = 'somelongrandomstringthatnobodywilleverguessinamillionyears'
    mydummy = Pyro4.Proxy('PYRO:Dummy@10.0.0.2:33014')
    print mydummy.printme()

You will not see anything printed from the client side (maybe "None" or something), but you will see the dummy object print "dummy" 10 times on the host machine.

Voila!