Advertise & Discover Services¶
The examples here all refer to the
In the context of networkzero, an address is a string combining an IP address and a port, eg “192.0.2.1:12345”. You can specify just the IP address, just the port, both or neither, depending on how much control you want.
Advertise a service¶
import networkzero as nw0 address = nw0.advertise("myservice1") print("Service is at", address)
If you are running a service in one process, advertising it will enable other processes and machines to know what address to connect to. This example shows the minimum you need supply—just a name—to advertise a service. If you haven’t chosen an address, one will be returned which you can then use to send and receive messages.
Advertise a service with a specific address¶
import networkzero as nw0 ip_address = "192.0.2.1" address = nw0.advertise("myservice2", ip_address) print("Service is at", address)
If you know exactly what address you want to use for your service, you can specify that when advertising. In this example, no port is given so one will be selected at random from the range of ports designated as dynamic.
Advertise a service with a specific port¶
import networkzero as nw0 address = nw0.advertise("myservice3", 1234) print("Service is at address", address)
It may be that you’ve agreed a particular port to work with, regardless of the network you’re on. In that case, you can supply just a port number and a suitable IP address will be supplied by networkzero.
Advertise a service with a wildcard address¶
import networkzero as nw0 ip_address = "127.*" address = nw0.advertise("myservice4", ip_address) print("Service is at", address)
Sometimes the machine you’re on has several IP addresses, for example when it is connected to to a wireless and to a wired network, or when it supports a virtual machine. Most of the time, networkzero will select the best address but if it doesn’t you can specify which of the networks you want to use.
Note that this doesn’t mean you have to specify the entire IP address, merely enough of it to distinguish one network from another.
Discover a service¶
On machine A¶
import networkzero as nw0 nw0.advertise("myservice5")
On machine B¶
import networkzero as nw0 myservice = nw0.discover("myservice5") print("myservice is at", myservice)
Once a service has been advertised by name, that name can be discovered by any other networkzero program on the same network. You only need to know the name used. Discovery will wait for up to 60 seconds for a service to be advertised. If one is discovered, its address string is returned; if not, None is returned.
Discover all services¶
On machine A¶
import networkzero as nw0 nw0.advertise("myservice7")
On machine B¶
import networkzero as nw0 nw0.advertise("myservice8")
On machine C¶
import networkzero as nw0 services = nw0.discover_all() print("Services:", services)
It can be useful to see what services are advertised across the whole network. This can be used, for example, in a chat room to discover new members joining the room. Pairs of names and addresses are returned, suitable for converting into a Python dictionary.