<previous | contents | next> | Pyro Manual |
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.
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." 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()
import Pyro.core import sys # you have to change the URI below to match your own host/port. jokes = Pyro.core.getProxyForURI("PYROLOC://localhost:7767/jokegen") print jokes.joke("Irmen")
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." Pyro.core.initServer() ns=Pyro.naming.NameServerLocator().getNS() daemon=Pyro.core.Daemon() daemon.useNameServer(ns) uri=daemon.connect(JokeGen(),"jokegen") daemon.requestLoop()
import Pyro.core # finds object automatically if you're running the Name Server. jokes = Pyro.core.getProxyForURI("PYRONAME://jokegen") print jokes.joke("Irmen")
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
import sys, socket 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: ns.unregister('test') # this is the name by which our object will be known to the outside world 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()
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.
import sys, socket 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 ...)
(...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')
irmen@atlantis:~ > ns *** Pyro Name Server *** Pyro Server Initialized. Using Pyro V2.4 Will accept shutdown requests. URI written to: /home/irmen/Pyro_NS_URI URI is: PYRO://10.0.0.150:9090/0a000096-08c30adb-eeb74c26-31095a7a Name Server started.
irmen@atlantis:~/ex > python testserver.py Pyro Server Initialized. Using Pyro V2.4 searching for Name Server... Server object "test" ready.
irmen@atlantis:~/ex > python testclient.py *** Using static testmod_proxy. Pyro Client Initialized. Using Pyro V2.4 finding object URI: PYRO://10.0.0.150:7766/0a000096-08f20adc-5b774c37-97f7f18a 999 322 122 0.222222222222 ********** String1String2
irmen@atlantis:~/ex > nsc listall Finding NS using broadcast @ port 9091 LOCATOR: Searching Pyro Name Server... NS is at 10.0.0.150 (atlantis.lan) port 9090 -------------- START DATABASE :Default.test --> PYRO://10.0.0.150:7766/0a000096-08f20adc-5b774c37-97f7f18a :Pyro.NameServer --> PYRO://10.0.0.150:9090/0a000096-08c30adb-eeb74c26-31095a7a -------------- END
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 2002-01-16 17:11:42 Pyro Initializing, version 2.4 This is initServer. Configuration settings are as follows: PYRO_BC_RETRIES = 2 PYRO_BC_TIMEOUT = 2 PYRO_COMPRESSION = 0 PYRO_CONFIG_FILE = PYRO_DNS_URI = 0 PYRO_LOGFILE = /home/irmen/Pyro_log PYRO_MAXCONNECTIONS = 200 PYRO_MOBILE_CODE = 0 PYRO_MULTITHREADED = 1 PYRO_NS_BC_PORT = 9091 PYRO_NS_DEFAULTGROUP = :Default PYRO_NS_HOSTNAME = None PYRO_NS_PORT = 9090 PYRO_NS_URIFILE = /home/irmen/Pyro_NS_URI PYRO_PORT = 7766 PYRO_PORT_RANGE = 100 PYRO_STORAGE = /home/irmen PYRO_TRACELEVEL = 3 PYRO_USER_LOGFILE = /home/irmen/Pyro_userlog PYRO_USER_TRACELEVEL = 0 Init done. ---------------------------------------------------------------------- 2002-01-16 17:11:42 ** NOTE ** PYROAdapter ** adapter daemon set to <Pyro Daemon on atlantis:9090> 2002-01-16 17:11:42 ** NOTE ** NameServer ** created group :Pyro 2002-01-16 17:11:42 ** NOTE ** NameServer ** created group :Default 2002-01-16 17:11:42 ** NOTE ** NameServer ** registered NameServer with URI PYRO://10.0.0.150:9090/0a000096-09080add-0c699119-2620c341 2002-01-16 17:11:42 ** NOTE ** NameServer ** URI written to /home/irmen/Pyro_NS_URI 2002-01-16 17:11:42 ** NOTE ** NS daemon ** This is Pyro Name Server V2.1. 2002-01-16 17:11:42 ** NOTE ** NS daemon ** Starting on atlantis port 9090 broadcast server on port 9091
<previous | contents | next> | Pyro Manual |