<previous | top | next> | Pyro Manual |
Object names can be absolute or relative. Relative object names are searched in the default group (which has a special name, set by PYRO_NS_DEFAULTGROUP
). Absolute names are always searched from the root. Absolute object names start with a special character that signifies the root; the colon (':
'). Relative object names don't have this character at the start.
Object names can be simple or compound. Compound object names consist of multiple parts separated by a special group separator character: the dot ('.
'). Compound names are used to search within the namespace hierarchy. Each component of the name is the name of a group. The last name component can be the name of a group or an object (usually the latter).
Let's finish with a few examples to clarify all this. Note that by name alone you cannot distinguish a group name or an object name.
:
| The namespace root group |
:TestObj
| The TestObj name in the root (most likely an object name) |
:Test.simple.object1
| The object1 name in the simple group in the Test group in the root. |
Test.simple.object1
| The object1 name in the simple group in the Test group in the default group (which is ":Default" if not configured otherwise. This is the Default group in the root). |
object1
| The object1 name in the default group. |
Pyro.naming.NameServer
)
Pyro.naming.PersistentNameServer
)
The persistent NS stores its naming database on disk. Currently this is implemented the easy
way; there is a direct mapping between the group names and directories on disk, and between
object names + URI's and files in these directories on disk. The database by default is
stored in a "Pyro_NS_database" directory that is created in the directory configured
by PYRO_STORAGE
. There is a special option for the script that specifies
another location, if needed. See below.
Usually you don't access the NameServer
or PersistentNameServer
classes
directly: there are scripts to start the right name server.
connect
method of the daemon to connect your object
instances to the daemon on the server. The daemon will register your object with the
name server too, if you supplied a NS to the daemon and your object isn't transient (it has a name).
But, when using the persistent name server, there is a complication here: if you didn't explicitly remove your object from the NS, the entry will still be there the next time. Your connect attempt will then fail because your object cannot be registered again in the NS.
The solution is to use the new connectPersistent
method of the Pyro daemon.
Except for the method name, you call it exactly like the regular connect
method.
It tries to find your object in the NS. If it's there already, the previous URI is used for your object
(that also means that the object's GUID is replaced by the previous GUID that was found in the
NS). If it isn't there, the regular connect
call takes over.
Of course you could always play safe and explicitly unregister any possible previous occurrences from the NS before you connect new instances. This is what all examples do by the way, so you can safely run an example again and again.
For your information, the code that starts the Persistent Name Server uses connectPersistent
to connect the name server object to the daemon. Why? because the name server itself
is also registered in the NS database, and it is necessary that when the NS restarts, it
uses the URI of the previous instance if found in the persistent database.
BCGuard()
function that returns a BC request validator object, or None
.
NSGuard()
function that returns a NS new conn validator object, or None
.
Pyro.naming.BCReqValidator
. You must override the two
methods that check for each command if it is allowed or if it is refused.
These are acceptLocationCmd(self)
and acceptShutdownCmd(self)
, and they return 0 or 1 (accept or deny).
You can access self.addr
to have the client's address (ip,port).
You can call self.reply('message')
to send a message back to
the client. This may be polite to let it know why you refused the command.
bin
directory that will start a Pyro Name Server for you:
ns
(Name Server)
rns
(Restarting Name Server)
ns
command but the script will restart the
name server if it quits or crashes. Do not confuse this with the -d
argument,
which will start a Name Server that has its database persistent on disk. Of course it is
very useful to combine the rns
with the -d
switch!!!ns.bat, rns.bat
nsc
command (explained in the Usage chapter) you can control a Name
Server that is already running.
Pyro.naming
package.
This object gets a Pyro proxy for the NS for you.
There are essentially three ways to get a reference to the Name Server:
NameServerLocator
's broadcast mechanism. This only works if your network supports broadcasting and the NS is reachable by a broadcast request.
locator = Pyro.naming.NameServerLocator() ns = locator.getNS()If your network doesn't allow broadcasts, or the broadcast can't reach the NS, this mechanism doesn't work. There is a simple workaround: just set the
PYRO_NS_HOSTNAME
config option to the hostname
on which your NS can be found. Note however that the locator first tries to find the NS
by using the broadcast mechanism, and when this fails, uses PYRO_NS_HOSTNAME
to find the NS.
You have a delay each time the NS is searched. A better option is the following;
NameServerLocator
's direct host mechanism. This only works if you know the host on which the NS is located.
The port
argument is optional. If the NS has been started using a non-default port number you can use it to specify the port number.
locator = Pyro.naming.NameServerLocator() ns = locator.getNS(host='hostname', port=7777)If you specify the hostname yourself, the locator doesn't attempt to find the NS with the broadcast mechanism, and therefore there is no lookup delay. Also you can specify a port number different from the default port. You could ofcourse supply
Pyro.config.PYRO_NS_HOSTNAME
as the hostname for the lookup. This way,
when it is not set, you're using the broadcast lookup. If it's set, you're using the direct host lookup.
Pyro_NS_URI
':
uri = open('Pyro_NS_URI','r').read() uri = Pyro.core.PyroURI(uri) # convert string to real URI object ns = Pyro.naming.NameServerProxy(uri) # create a static proxy for the NS ... you can now invoke methods, such as ns.ping() ...Note: The URI changes every time a Pyro server or object is created. You cannot use a previously written URI when you have restarted the server.
Pyro.naming.NameServerProxy
) contains all necessary logic already. So just use that one. If you use the locator (see above) you will get a correct proxy automatically.