/usr/share/doc/pyro/html/8-example.html is in pyro-doc 1:3.14-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>PYRO - Example</title>
<link rel="stylesheet" type="text/css" href="pyromanual_print.css" media="print">
<link rel="stylesheet" type="text/css" href="pyromanual.css" media="screen">
</head>
<body>
<div class="nav">
<table width="100%">
<tr>
<td align="left"><a href="7-features.html"><previous</a> | <a href="PyroManual.html">contents</a> | <a href=
"9-security.html">next></a></td>
<td align="right">Pyro Manual</td>
</tr>
</table>
<hr></div>
<h2>8. Example</h2>
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 <code>examples</code> 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.
<p>For the really impatient, first two minimalist Pyro examples.</p>
<h3>Minimalist's Pyro - not using a Name Server</h3>
<dl>
<dt>Server:</dt>
<dd>
<pre>
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()
</pre>
</dd>
<dt>Client:</dt>
<dd>
<pre>
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")
</pre>
</dd>
</dl>
<h3>Minimalist's Pyro - using a Name Server</h3>
<dl>
<dt>Server:</dt>
<dd>
<pre>
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()
</pre>
</dd>
<dt>Client:</dt>
<dd>
<pre>
import Pyro.core
# finds object automatically if you're running the Name Server.
jokes = Pyro.core.getProxyForURI("PYRONAME://jokegen")
print jokes.joke("Irmen")
</pre>
</dd>
</dl>
<h3>There we go with the complete example:</h3>
<ol>
<li>Write a module 'testmod.py' containing a class 'testclass', which will be accessed remotely.
<pre>
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
</pre>
</li>
<li>Write a server, testserver.py, that creates one or more instances of the 'testclass', and registers them with
the Pyro Name Server.
<pre>
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()
</pre>
</li>
<li>To make it interesting, the shortest client possible looks someting like:
<pre>
import Pyro.core
o=Pyro.core.getProxyForURI('PYRONAME://:Default.test')
print o.mul(5,33)
</pre>... But for educational purposes, we use the long way around. Read on.
</li>
<li>Write a client, testclient.py, that will find the Name Server.
<pre>
import Pyro.naming, Pyro.core
from Pyro.errors import NamingError
# locate the NS
locator = Pyro.naming.NameServerLocator()
print 'Searching Name Server...',
ns = locator.getNS()
</pre>(... continued ...)
</li>
<li>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.
<p>(...continued from above...)</p>
<pre>
# 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')
</pre>
</li>
<li>Run the application in the network. First, start the Name Server on one computer.
<pre>
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.
</pre>
</li>
<li>Start the server on another computer (or in a different shell).
<pre>
irmen@atlantis:~/ex > python testserver.py
Pyro Server Initialized. Using Pyro V3.5
searching for Name Server...
Server object "test" ready.
</pre>
</li>
<li>Finally, run the client on a third computer (or in a different shell).
<pre>
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
</pre>
</li>
<li>You might want to peek in the Name Server:
<pre>
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
</pre>
</li>
<li>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 <code>PYRO_TRACELEVEL</code> to 3 (=maximum logging). Then, when you start Pyro programs (like
the nameserver), they will write something like this to the logfile:
<pre>
------------------------------------------------------------ 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
</pre>
</li>
</ol>
<div class="nav">
<hr>
<table width="100%">
<tr>
<td align="left"><a href="7-features.html"><previous</a> | <a href="PyroManual.html">contents</a> | <a href=
"9-security.html">next></a></td>
<td align="right">Pyro Manual</td>
</tr>
</table></div>
</body>
</html>
|