<previous | top | 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.
(Note: the example below is from Pyro 2.4 on Linux)
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 def error(s): raise ValueError('Server generated exception, this is ok!')
irmen@atlantis:~/ex > pyroc test Python Remote Object Compiler (c) Irmen de Jong. Pyro V2.4 [added current directory to import path] processing module 'test' (/home/irmen/ex/test.py)... examining class testclass ... Generating proxy for testclass This release of Pyro doesn't need server-side skeleton code. All done. Output can be found in test_proxy.py .
import sys, socket import Pyro.naming import Pyro.core from Pyro.errors import PyroError,NamingError import test ###### testclass Pyro object class testclass(Pyro.core.ObjBase, test.testclass): pass ###### main server program def main(): Pyro.core.initServer() PyroDaemon = Pyro.core.Daemon() # locate the NS locator = Pyro.naming.NameServerLocator() print 'searching for Name Server...' ns = locator.getNS(Pyro.config.PYRO_NS_HOSTNAME) print 'Name Server found at',ns.URI.address,'('+(Pyro.protocol.getHostname(ns.URI.address) or '??')+') port',ns.URI.port PyroDaemon.useNameServer(ns) # connect a new object implementation (first unregister previous one) try: ns.unregister('test') except NamingError: pass # connect new object implementation PyroDaemon.connect(testclass(),'test') # enter the server loop. print 'Server object "test" ready.' while 1: PyroDaemon.handleRequests(3.0) sys.stdout.write('.') sys.stdout.flush() if __name__=="__main__": main()
import sys, socket import Pyro.naming, Pyro.core # look for static proxy try: import test_proxy print '*** Using static test_proxy.' dynproxy = 0 except ImportError: print '*** No static test_proxy found. Using dynamic proxy.' dynproxy = 1 Pyro.core.initClient() # locate the NS locator = Pyro.naming.NameServerLocator() print 'Searching Name Server...', ns = locator.getNS(Pyro.config.PYRO_NS_HOSTNAME) print 'Name Server found at',ns.URI.address,'('+(Pyro.protocol.getHostname(ns.URI.address) or '??')+') port',ns.URI.port(... continued ...)
(...continued from above...)
# resolve the Pyro object print 'binding to object' try: URI=ns.resolve('test') print 'URI:',URI except Pyro.core.PyroError,x: print 'Couldn\'t bind object, nameserver says:',x raise SystemExit # create a proxy for the Pyro object, and return that if dynproxy: # use dynamic proxy test = Pyro.core.getProxyForURI(URI) else: # use static (precompiled) proxy test = test_proxy.testclass(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') print '*** Now a server-generated exception should occur:' print test.error()
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... Name Server found at 10.0.0.150 (atlantis.lan) port 9090 Server object "test" ready. .............
irmen@atlantis:~/ex > python testclient.py *** Using static test_proxy. Pyro Client Initialized. Using Pyro V2.4 Searching Name Server... Name Server found at 10.0.0.150 (atlantis.lan) port 9090 binding to object URI: PYRO://10.0.0.150:7766/0a000096-08f20adc-5b774c37-97f7f18a 999 322 122 0.222222222222 ********** String1String2 *** Now a server-generated exception should occur: Traceback (most recent call last): File "testclient.py", line 52, in ? print test.error() File "test_proxy.py", line 32, in error return S.adapter.remoteInvocation('error',0) File "/home/irmen/Pyro/Pyro/protocol.py", line 187, in remoteInvocation answer.raiseEx() File "/home/irmen/Pyro/Pyro/errors.py", line 57, in raiseEx raise self.excObj ValueError: Server generated exception, this is ok!
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_BINARY_PICKLE = 1 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