| <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 1.5 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:~/Pyro/test > pyroc test Python Remote Object Compiler (c) Irmen de Jong. Pyro V1.5 [added current directory to import path] processing module 'test' (/home/irmen/Pyro-1_5/test/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()
# locate the NS
PyroDaemon = Pyro.core.Daemon()
locator = Pyro.naming.NameServerLocator()
print 'searching for Naming Service...'
try:
ns = locator.getNS()
except (PyroError,socket.error),x:
hn = socket.gethostname()
print '\nNaming Service not found with broadcast. Trying host',hn,'...',
ns = locator.getNS(host=hn)
print 'Naming Service 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 Naming Service...',
try:
ns = locator.getNS()
except (Pyro.core.PyroError, socket.error),x:
hn = socket.gethostname()
print '\nNaming Service not found with broadcast. Trying host',hn,'...',
ns = locator.getNS(host=hn)
print 'Naming Service 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 Server Initialized. Using Pyro V1.5 Will accept shutdown requests. URI written to: /home/irmen/Pyro_NS_URI URI is: PYRO://192.168.0.99:9090/c0a80063-16d55032-c784739a-45067dfb Naming Service started.
irmen@atlantis:~/Pyro/test > python testserver.py Pyro Server Initialized. Using Pyro V1.5 searching for Naming Service... Naming Service found at 192.168.0.99 (atlantis.home.nl) port 9090 Server object "test" ready. ......
irmen@atlantis:~/Pyro/test > python testclient.py
*** Using static test_proxy.
Pyro Client Initialized. Using Pyro V1.5
Searching Naming Service... Naming Service found at 192.168.0.99 (atlantis.home.nl) port 9090
binding to object
URI: PYRO://192.168.0.99:7766/c0a80063-14ff5033-d7de73e1-150f1c0d
999
322
122
0.222222222222
**********
String1String2
*** Now a server-generated exception should occur:
Traceback (most recent call last):
File "testclient.py", line 51, 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 151, in remoteInvocation
answer.raiseEx()
File "/home/irmen/Pyro/Pyro/errors.py", line 50, in raiseEx
raise self.excObj
ValueError: Server generated exception, this is ok!
irmen@atlantis:~/Pyro/test > nsc listall Finding NS using broadcast @ port 9091 LOCATOR: Searching Pyro Naming Service... NS is at 192.168.0.99 (atlantis.home.nl) port 9090 -------------- START DATABASE :Default.test --> PYRO://192.168.0.99:7766/c0a80063-14ff5033-d7de73e1-150f1c0d :Pyro.NameServer --> PYRO://192.168.0.99:9090/c0a80063-16d55032-c784739a-45067dfb -------------- 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 Thu Jan 18 14:14:15 2001 Pyro Initializing, version 1.5 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_LOGFILE = /home/irmen/Pyro_log PYRO_MAXCONNECTIONS = 200 PYRO_MULTITHREADED = 1 PYRO_NS_BC_PORT = 9091 PYRO_NS_DEFAULTGROUP = :Default PYRO_NS_GROUPSEP = . PYRO_NS_NAME = :Pyro.NameServer PYRO_NS_PORT = 9090 PYRO_NS_ROOTCHAR = : 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. ---------------------------------------------------------------------- 01/18/01 14:14:15 ** NOTE ** PYROAdapter ** adapter daemon set to <Pyro Daemon on atlantis:9999> 01/18/01 14:14:15 ** NOTE ** NameServer ** created group :Pyro 01/18/01 14:14:15 ** NOTE ** NameServer ** created group :Default 01/18/01 14:14:15 ** NOTE ** NameServer ** registered NameServer with URI PYRO://192.168.0.99:9999/c0a80063-4e065034-3f78dae8-2d3fdc85 01/18/01 14:14:15 ** NOTE ** NameServer ** URI written to /home/irmen/Pyro_NS_URI 01/18/01 14:14:15 ** NOTE ** NS daemon ** This is Pyro Naming Service V1.4. 01/18/01 14:14:15 ** NOTE ** NS daemon ** Starting on atlantis port 9999 broadcast server on port 9091