~ubuntu-branches/debian/sid/pyro/sid

« back to all changes in this revision

Viewing changes to docs/8-example.html

  • Committer: Bazaar Package Importer
  • Author(s): Carl Chenet, Carl Chenet, Jakub Wilk
  • Date: 2010-09-14 01:04:28 UTC
  • Revision ID: james.westby@ubuntu.com-20100914010428-02r7p1rzr7jvw94z
Tags: 1:3.9.1-2
[Carl Chenet]
* revert to 3.9.1-1 package because of the development status 
  of the 4.1 package is unsuitable for stable use
  DPMT svn #8557 revision (Closes: #589172) 
* added debian/source
* added debian/source/format
* package is now 3.0 (quilt) source format
* debian/control
  - Bump Standards-Version to 3.9.1

[Jakub Wilk]
* Add ‘XS-Python-Version: >= 2.5’ to prevent bytecompilation with python2.4
  (closes: #589053).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<html>
 
3
<!-- $Id: 8-example.html,v 2.21.2.2 2008/06/28 23:41:02 irmen Exp $ -->
 
4
<head>
 
5
  <title>PYRO - Example</title>
 
6
  <link rel="stylesheet" type="text/css" href="pyromanual_print.css" media="print">
 
7
  <link rel="stylesheet" type="text/css" href="pyromanual.css" media="screen">
 
8
</head>
 
9
 
 
10
<body>
 
11
  <div class="nav">
 
12
  <table width="100%">
 
13
    <tr>
 
14
      <td align="left"><a href="7-features.html">&lt;previous</a> | <a href="PyroManual.html">contents</a> | <a href=
 
15
      "9-security.html">next&gt;</a></td>
 
16
 
 
17
      <td align="right">Pyro Manual</td>
 
18
    </tr>
 
19
  </table>
 
20
<hr></div>
 
21
 
 
22
  <h2>8. Example</h2>
 
23
  In this chapter you'll find a short but complete example that shows how Pyro must be used
 
24
  and how it works. However, much more interesting examples can be found in the <code>examples</code> directory.
 
25
  For the real impatient people, I recommend the &quot;quickstart&quot; example, because you'll see that you
 
26
  can eliminate very much of the (already little!) extra work you have to do to make a Pyro application.
 
27
 
 
28
  <p>For the really impatient, first two minimalist Pyro examples.</p>
 
29
 
 
30
  <h3>Minimalist's Pyro - not using a Name Server</h3>
 
31
 
 
32
  <dl>
 
33
    <dt>Server:</dt>
 
34
 
 
35
    <dd>
 
36
      <pre>
 
37
import Pyro.core
 
38
 
 
39
class JokeGen(Pyro.core.ObjBase):
 
40
        def __init__(self):
 
41
                Pyro.core.ObjBase.__init__(self)
 
42
        def joke(self, name):
 
43
                return &quot;Sorry &quot;+name+&quot;, I don't know any jokes.&quot;
 
44
 
 
45
Pyro.core.initServer()
 
46
daemon=Pyro.core.Daemon()
 
47
uri=daemon.connect(JokeGen(),&quot;jokegen&quot;)
 
48
 
 
49
print &quot;The daemon runs on port:&quot;,daemon.port
 
50
print &quot;The object's uri is:&quot;,uri
 
51
 
 
52
daemon.requestLoop()
 
53
</pre>
 
54
    </dd>
 
55
 
 
56
    <dt>Client:</dt>
 
57
 
 
58
    <dd>
 
59
      <pre>
 
60
import Pyro.core
 
61
 
 
62
# you have to change the URI below to match your own host/port.
 
63
jokes = Pyro.core.getProxyForURI(&quot;PYROLOC://localhost:7766/jokegen&quot;)
 
64
 
 
65
print jokes.joke(&quot;Irmen&quot;)
 
66
</pre>
 
67
    </dd>
 
68
  </dl>
 
69
 
 
70
  <h3>Minimalist's Pyro - using a Name Server</h3>
 
71
 
 
72
  <dl>
 
73
    <dt>Server:</dt>
 
74
 
 
75
    <dd>
 
76
      <pre>
 
77
import Pyro.core
 
78
import Pyro.naming
 
79
 
 
80
class JokeGen(Pyro.core.ObjBase):
 
81
        def __init__(self):
 
82
                Pyro.core.ObjBase.__init__(self)
 
83
        def joke(self, name):
 
84
                return &quot;Sorry &quot;+name+&quot;, I don't know any jokes.&quot;
 
85
 
 
86
Pyro.core.initServer()
 
87
ns=Pyro.naming.NameServerLocator().getNS()
 
88
daemon=Pyro.core.Daemon()
 
89
daemon.useNameServer(ns)
 
90
uri=daemon.connect(JokeGen(),&quot;jokegen&quot;)
 
91
daemon.requestLoop()
 
92
</pre>
 
93
    </dd>
 
94
 
 
95
    <dt>Client:</dt>
 
96
 
 
97
    <dd>
 
98
      <pre>
 
99
import Pyro.core
 
100
 
 
101
# finds object automatically if you're running the Name Server.
 
102
jokes = Pyro.core.getProxyForURI(&quot;PYRONAME://jokegen&quot;)
 
103
 
 
104
print jokes.joke(&quot;Irmen&quot;)
 
105
</pre>
 
106
    </dd>
 
107
  </dl>
 
108
 
 
109
  <h3>There we go with the complete example:</h3>
 
110
 
 
111
  <ol>
 
112
    <li>Write a module 'testmod.py' containing a class 'testclass', which will be accessed remotely.
 
113
      <pre>
 
114
class testclass:
 
115
    def mul(s, arg1, arg2): return arg1*arg2
 
116
    def add(s, arg1, arg2): return arg1+arg2
 
117
    def sub(s, arg1, arg2): return arg1-arg2
 
118
    def div(s, arg1, arg2): return arg1/arg2
 
119
</pre>
 
120
    </li>
 
121
 
 
122
    <li>Write a server, testserver.py, that creates one or more instances of the 'testclass', and registers them with
 
123
    the Pyro Name Server.
 
124
      <pre>
 
125
import Pyro.naming
 
126
import Pyro.core
 
127
from Pyro.errors import PyroError,NamingError
 
128
 
 
129
import testmod
 
130
 
 
131
###### testclass Pyro object
 
132
 
 
133
class testclass(Pyro.core.ObjBase, testmod.testclass):
 
134
        pass
 
135
 
 
136
###### main server program
 
137
 
 
138
def main():
 
139
        Pyro.core.initServer()
 
140
        daemon = Pyro.core.Daemon()
 
141
        # locate the NS
 
142
        locator = Pyro.naming.NameServerLocator()
 
143
        print 'searching for Name Server...'
 
144
        ns = locator.getNS()
 
145
        daemon.useNameServer(ns)
 
146
 
 
147
        # connect a new object implementation (first unregister previous one)
 
148
        try:
 
149
                # 'test' is the name by which our object will be known to the outside world
 
150
                ns.unregister('test')
 
151
        except NamingError:
 
152
                pass
 
153
 
 
154
        # connect new object implementation
 
155
        daemon.connect(testclass(),'test')
 
156
 
 
157
        # enter the server loop.
 
158
        print 'Server object &quot;test&quot; ready.'
 
159
        daemon.requestLoop()
 
160
 
 
161
if __name__==&quot;__main__&quot;:
 
162
        main()
 
163
</pre>
 
164
    </li>
 
165
 
 
166
    <li>To make it interesting, the shortest client possible looks someting like:
 
167
      <pre>
 
168
import Pyro.core
 
169
o=Pyro.core.getProxyForURI('PYRONAME://:Default.test')
 
170
print o.mul(5,33)
 
171
</pre>... But for educational purposes, we use the long way around. Read on.
 
172
    </li>
 
173
 
 
174
    <li>Write a client, testclient.py, that will find the Name Server.
 
175
      <pre>
 
176
import Pyro.naming, Pyro.core
 
177
from Pyro.errors import NamingError
 
178
 
 
179
# locate the NS
 
180
locator = Pyro.naming.NameServerLocator()
 
181
print 'Searching Name Server...',
 
182
ns = locator.getNS()
 
183
</pre>(... continued ...)
 
184
    </li>
 
185
 
 
186
    <li>Let the client query the NS for the object's URI. Then create a proxy for the remote object. Because the proxy
 
187
    appears the same as the real 'testclass', the client can now invoke methods on the remote objects.
 
188
 
 
189
      <p>(...continued from above...)</p>
 
190
      <pre>
 
191
# resolve the Pyro object
 
192
print 'finding object'
 
193
try:
 
194
        URI=ns.resolve('test')
 
195
        print 'URI:',URI
 
196
except NamingError,x:
 
197
        print 'Couldn\'t find object, nameserver says:',x
 
198
        raise SystemExit
 
199
 
 
200
# create a proxy for the Pyro object, and return that
 
201
test = Pyro.core.getProxyForURI(URI)
 
202
 
 
203
print test.mul(111,9)
 
204
print test.add(100,222)
 
205
print test.sub(222,100)
 
206
print test.div(2.0,9.0)
 
207
print test.mul('*',10)
 
208
print test.add('String1','String2')
 
209
</pre>
 
210
    </li>
 
211
 
 
212
    <li>Run the application in the network. First, start the Name Server on one computer.
 
213
      <pre>
 
214
irmen@atlantis:~ &gt; pyro-ns
 
215
*** Pyro Name Server ***
 
216
Pyro Server Initialized. Using Pyro V3.7
 
217
URI is: PYRO://10.0.0.150:9090/0a00009604005c6282a8a516d79917fd
 
218
URI written to: e:\Pyro_NS_URI
 
219
Name Server started.
 
220
</pre>
 
221
    </li>
 
222
 
 
223
    <li>Start the server on another computer (or in a different shell).
 
224
      <pre>
 
225
irmen@atlantis:~/ex &gt; python testserver.py 
 
226
Pyro Server Initialized. Using Pyro V3.5
 
227
searching for Name Server...
 
228
Server object &quot;test&quot; ready.
 
229
</pre>
 
230
    </li>
 
231
 
 
232
    <li>Finally, run the client on a third computer (or in a different shell).
 
233
      <pre>
 
234
irmen@atlantis:~/ex &gt; python testclient.py
 
235
Pyro Client Initialized. Using Pyro V3.5
 
236
Searching Name Server... finding object
 
237
URI: PYRO://10.0.0.150:7766/0a0000960c545c62a91e3021bceb7f26
 
238
999
 
239
322
 
240
122
 
241
0.222222222222
 
242
**********
 
243
String1String2
 
244
</pre>
 
245
    </li>
 
246
 
 
247
    <li>You might want to peek in the Name Server:
 
248
      <pre>
 
249
irmen@atlantis:~/ex &gt; pyro-nsc listall
 
250
Locator: searching Pyro Name Server...
 
251
NS is at 10.0.0.150 (isengard.lan) port 9090
 
252
-------------- START DATABASE
 
253
:Default.test  --&gt;  PYRO://10.0.0.150:7766/0a0000960c545c62a91e3021bceb7f26
 
254
:Pyro.NameServer  --&gt;  PYRO://10.0.0.150:9090/0a00009604005c6282a8a516d79917fd
 
255
-------------- END
 
256
</pre>
 
257
    </li>
 
258
 
 
259
    <li>And if you're interested you may want to try the logging facility of Pyro. First, set the tracelevel to
 
260
    something other than the default, 0. See the chapter on configuration how to do that. Usually you'll set the
 
261
    environment variable <code>PYRO_TRACELEVEL</code> to 3 (=maximum logging). Then, when you start Pyro programs (like
 
262
    the nameserver), they will write something like this to the logfile:
 
263
      <pre>
 
264
------------------------------------------------------------ NEW SESSION
 
265
2005-03-13 13:23:40   Pyro Initializing, version 3.7
 
266
This is initServer.
 
267
Configuration settings are as follows:
 
268
PYROSSL_CA_CERT = ca.pem
 
269
PYROSSL_CERTDIR = E:\temp\certs
 
270
PYROSSL_CLIENT_CERT = client.pem
 
271
PYROSSL_SERVER_CERT = server.pem
 
272
PYRO_BC_RETRIES = 2
 
273
PYRO_BC_TIMEOUT = 2
 
274
PYRO_CHECKSUM = 0
 
275
PYRO_COMPRESSION = 0
 
276
PYRO_CONFIG_FILE = 
 
277
PYRO_DETAILED_TRACEBACK = 0
 
278
PYRO_DNS_URI = 0
 
279
PYRO_ES_BLOCKQUEUE = 1
 
280
PYRO_ES_QUEUESIZE = 1000
 
281
PYRO_LOGFILE = E:\temp\Pyro_log
 
282
PYRO_MAXCONNECTIONS = 200
 
283
PYRO_MOBILE_CODE = 0
 
284
PYRO_MULTITHREADED = 1
 
285
PYRO_NS2_BC_PORT = 9091
 
286
PYRO_NS2_HOSTNAME = None
 
287
PYRO_NS2_PORT = 9091
 
288
PYRO_NS_BC_PORT = 9090
 
289
PYRO_NS_DEFAULTGROUP = :Default
 
290
PYRO_NS_HOSTNAME = None
 
291
PYRO_NS_PORT = 9090
 
292
PYRO_NS_URIFILE = E:\temp\Pyro_NS_URI
 
293
PYRO_PICKLE_FORMAT = 2
 
294
PYRO_PORT = 7766
 
295
PYRO_PORT_RANGE = 100
 
296
PYRO_PRINT_REMOTE_TRACEBACK = 0
 
297
PYRO_SOCK_KEEPALIVE = 1
 
298
PYRO_STDLOGGING = 0
 
299
PYRO_STDLOGGING_CFGFILE = logging.cfg
 
300
PYRO_STORAGE = E:\temp
 
301
PYRO_TCP_LISTEN_BACKLOG = 200
 
302
PYRO_TRACELEVEL = 3
 
303
PYRO_USER_LOGFILE = E:\temp\Pyro_userlog
 
304
PYRO_USER_TRACELEVEL = 0
 
305
PYRO_XML_PICKLE = None
 
306
Init done.
 
307
----------------------------------------------------------------------
 
308
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** created group :Pyro
 
309
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** created group :Default
 
310
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** Running in single mode
 
311
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** registered NameServer with URI PYRO://10.0.0.150:9090/0a000096043c5c63117eead5b89ea267
 
312
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NameServer ** URI written to E:\temp\Pyro_NS_URI
 
313
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NS daemon ** This is the Pyro Name Server.
 
314
2005-03-13 13:23:44 [1084:MainThread] ** NOTE ** NS daemon ** Starting on isengard port 9090  broadcast server on port 9090
 
315
</pre>
 
316
    </li>
 
317
  </ol>
 
318
  <div class="nav">
 
319
  <hr>
 
320
  <table width="100%">
 
321
    <tr>
 
322
      <td align="left"><a href="7-features.html">&lt;previous</a> | <a href="PyroManual.html">contents</a> | <a href=
 
323
      "9-security.html">next&gt;</a></td>
 
324
 
 
325
      <td align="right">Pyro Manual</td>
 
326
    </tr>
 
327
  </table></div>
 
328
</body>
 
329
</html>