8. Example

In this chapter you'll find a short but complete example that shows how Pyro must be used and how it works. However, much more interesting examples can be found in the examples directory. For the real impatient people, I recommend the "quickstart" example, because you'll see that you can eliminate very much of the (already little!) extra work you have to do to make a Pyro application.

For the really impatient, first two minimalist Pyro examples.

Minimalist's Pyro - not using a Name Server

Server:
import Pyro.core

class JokeGen(Pyro.core.ObjBase):
        def __init__(self):
                Pyro.core.ObjBase.__init__(self)
        def joke(self, name):
                return "Sorry "+name+", I don't know any jokes."

def main():
    Pyro.core.initServer()
    daemon=Pyro.core.Daemon()
    uri=daemon.connect(JokeGen(),"jokegen")

    print "The daemon runs on port:",daemon.port
    print "The object's uri is:",uri

    daemon.requestLoop()
    
if __name__=="__main__":
    main()
Client:
import Pyro.core

# you have to change the URI below to match your own host/port.
jokes = Pyro.core.getProxyForURI("PYROLOC://localhost:7766/jokegen")

print jokes.joke("Irmen")

Minimalist's Pyro - using a Name Server

Server:
import Pyro.core
import Pyro.naming

class JokeGen(Pyro.core.ObjBase):
        def __init__(self):
                Pyro.core.ObjBase.__init__(self)
        def joke(self, name):
                return "Sorry "+name+", I don't know any jokes."

def main():
    Pyro.core.initServer()
    ns=Pyro.naming.NameServerLocator().getNS()
    daemon=Pyro.core.Daemon()
    daemon.useNameServer(ns)
    uri=daemon.connect(JokeGen(),"jokegen")
    daemon.requestLoop()

if __name__=="__main__":
    main()
Client:
import Pyro.core

# finds object automatically if you're running the Name Server.
jokes = Pyro.core.getProxyForURI("PYRONAME://jokegen")

print jokes.joke("Irmen")

There we go with the complete example:

  1. Write a module 'testmod.py' containing a class 'testclass', which will be accessed remotely.
    class testclass:
        def mul(s, arg1, arg2): return arg1*arg2
        def add(s, arg1, arg2): return arg1+arg2
        def sub(s, arg1, arg2): return arg1-arg2
        def div(s, arg1, arg2): return arg1/arg2
    
  2. Write a server, testserver.py, that creates one or more instances of the 'testclass', and registers them with the Pyro Name Server.
    import Pyro.naming
    import Pyro.core
    from Pyro.errors import PyroError,NamingError
    
    import testmod
    
    ###### testclass Pyro object
    
    class testclass(Pyro.core.ObjBase, testmod.testclass):
            pass
    
    ###### main server program
    
    def main():
            Pyro.core.initServer()
            daemon = Pyro.core.Daemon()
            # locate the NS
            locator = Pyro.naming.NameServerLocator()
            print 'searching for Name Server...'
            ns = locator.getNS()
            daemon.useNameServer(ns)
    
            # connect a new object implementation (first unregister previous one)
            try:
                    # 'test' is the name by which our object will be known to the outside world
                    ns.unregister('test')
            except NamingError:
                    pass
    
            # connect new object implementation
            daemon.connect(testclass(),'test')
    
            # enter the server loop.
            print 'Server object "test" ready.'
            daemon.requestLoop()
    
    if __name__=="__main__":
            main()
    
  3. To make it interesting, the shortest client possible looks someting like:
    import Pyro.core
    o=Pyro.core.getProxyForURI('PYRONAME://:Default.test')
    print o.mul(5,33)
    
    ... But for educational purposes, we use the long way around. Read on.
  4. Write a client, testclient.py, that will find the Name Server.
    import Pyro.naming, Pyro.core
    from Pyro.errors import NamingError
    
    # locate the NS
    locator = Pyro.naming.NameServerLocator()
    print 'Searching Name Server...',
    ns = locator.getNS()
    
    (... continued ...)
  5. Let the client query the NS for the object's URI. Then create a proxy for the remote object. Because the proxy appears the same as the real 'testclass', the client can now invoke methods on the remote objects.

    (...continued from above...)

    # resolve the Pyro object
    print 'finding object'
    try:
            URI=ns.resolve('test')
            print 'URI:',URI
    except NamingError,x:
            print 'Couldn\'t find object, nameserver says:',x
            raise SystemExit
    
    # create a proxy for the Pyro object, and return that
    test = Pyro.core.getProxyForURI(URI)
    
    print test.mul(111,9)
    print test.add(100,222)
    print test.sub(222,100)
    print test.div(2.0,9.0)
    print test.mul('*',10)
    print test.add('String1','String2')
    
  6. Run the application in the network. First, start the Name Server on one computer.
    irmen@atlantis:~ > pyro-ns
    *** Pyro Name Server ***
    Pyro Server Initialized. Using Pyro V3.7
    URI is: PYRO://10.0.0.150:9090/0a00009604005c6282a8a516d79917fd
    URI written to: e:\Pyro_NS_URI
    Name Server started.
    
  7. Start the server on another computer (or in a different shell).
    irmen@atlantis:~/ex > python testserver.py 
    Pyro Server Initialized. Using Pyro V3.5
    searching for Name Server...
    Server object "test" ready.
    
  8. Finally, run the client on a third computer (or in a different shell).
    irmen@atlantis:~/ex > python testclient.py
    Pyro Client Initialized. Using Pyro V3.5
    Searching Name Server... finding object
    URI: PYRO://10.0.0.150:7766/0a0000960c545c62a91e3021bceb7f26
    999
    322
    122
    0.222222222222
    **********
    String1String2
    
  9. You might want to peek in the Name Server:
    irmen@atlantis:~/ex > pyro-nsc listall
    Locator: searching Pyro Name Server...
    NS is at 10.0.0.150 (isengard.lan) port 9090
    -------------- START DATABASE
    :Default.test  -->  PYRO://10.0.0.150:7766/0a0000960c545c62a91e3021bceb7f26
    :Pyro.NameServer  -->  PYRO://10.0.0.150:9090/0a00009604005c6282a8a516d79917fd
    -------------- END
    
  10. And if you're interested you may want to try the logging facility of Pyro. First, set the tracelevel to something other than the default, 0. See the chapter on configuration how to do that. Usually you'll set the environment variable PYRO_TRACELEVEL to 3 (=maximum logging). Then, when you start Pyro programs (like the nameserver), they will write something like this to the logfile:
    ------------------------------------------------------------ NEW SESSION
    2005-03-13 13:23:40   Pyro Initializing, version 3.7
    This is initServer.
    Configuration settings are as follows:
    PYRO_BC_RETRIES = 2
    PYRO_BC_TIMEOUT = 2
    PYRO_CHECKSUM = 0
    PYRO_COMPRESSION = 0
    PYRO_CONFIG_FILE = 
    PYRO_DETAILED_TRACEBACK = 0
    PYRO_DNS_URI = 0
    PYRO_ES_BLOCKQUEUE = 1
    PYRO_ES_QUEUESIZE = 1000
    PYRO_LOGFILE = E:\temp\Pyro_log
    PYRO_MAXCONNECTIONS = 200
    PYRO_MOBILE_CODE = 0
    PYRO_MULTITHREADED = 1
    PYRO_NS2_BC_PORT = 9091
    PYRO_NS2_HOSTNAME = None
    PYRO_NS2_PORT = 9091
    PYRO_NS_BC_PORT = 9090
    PYRO_NS_DEFAULTGROUP = :Default
    PYRO_NS_HOSTNAME = None
    PYRO_NS_PORT = 9090
    PYRO_NS_URIFILE = E:\temp\Pyro_NS_URI
    PYRO_PICKLE_FORMAT = 2
    PYRO_PORT = 7766
    PYRO_PORT_RANGE = 100
    PYRO_PRINT_REMOTE_TRACEBACK = 0
    PYRO_SOCK_KEEPALIVE = 1
    PYRO_STDLOGGING = 0
    PYRO_STDLOGGING_CFGFILE = logging.cfg
    PYRO_STORAGE = E:\temp
    PYRO_TCP_LISTEN_BACKLOG = 200
    PYRO_TRACELEVEL = 3
    PYRO_USER_LOGFILE = E:\temp\Pyro_userlog
    PYRO_USER_TRACELEVEL = 0
    PYRO_XML_PICKLE = None
    Init done.
    ----------------------------------------------------------------------
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** created group :Pyro
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** created group :Default
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** Running in single mode
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** registered NameServer with URI PYRO://10.0.0.150:9090/0a000096043c5c63117eead5b89ea267
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** URI written to E:\temp\Pyro_NS_URI
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NS daemon ** This is the Pyro Name Server.
    2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NS daemon ** Starting on isengard port 9090  broadcast server on port 9090