NetworkZero

Easy network discovery & messaging

Aimed at a classrom or club situation, networkzero makes it simpler to have several machines or several processes on one machine discovering each other and talking across a network. Typical examples would include:

  • Sending commands to a robot
  • Sending scores to a scoreboard
  • Having a remote sensor ping a central controller
  • A peer-to-peer chat / instant messenger

To send a message and wait for a reply:

[Computer 1]
import networkzero as nw0

echo_address = nw0.advertise("echo")
while True:
    name = nw0.wait_for_message_from(echo_address)
    nw0.send_reply_to(echo_address, "Hello " + name)
[Computer 2]
import networkzero as nw0

echo_address = nw0.discover("echo")

reply = nw0.send_message_to(echo_address, "Alice")
print(reply)
reply = nw0.send_message_to(echo_address, "Bob")
print(reply)

To send news:

[Computer 1]
import networkzero as nw0

address = nw0.advertise("data-logger")
while True:
    #
    # ... do stuff
    #
    nw0.send_news_to(address, "data", ...)
[Computer 2, 3, 4...]
import networkzero as nw0

logger = nw0.discover("data-logger")
while True:
    topic, data = nw0.wait_for_news_from(logger, "data")
    #
    # ... write the data to a database etc.
    #

Functions

General

networkzero.address(address=None)

Convert one of a number of inputs into a valid ip:port string.

Elements which are not provided are filled in as follows:

  • IP Address: the system is asked for the set of IP addresses associated with the machine and the first one is used, preferring those matching address if it is a wildcard.
  • Port number: a random port is selected from the pool of dynamically-available port numbers.

This means you can pass any of: nothing; a hostname; an IP address; an IP address with wildcards; a port number

If an IP address is supplied but is invalid, an InvalidAddressError exception is raised.

Parameters:address – (optional) Any of: an IP address, a port number, or both
Returns:a valid ip:port string for this machine
networkzero.action_and_params(commandline)

Treat a command line as an action followed by parameter

Parameters:commandline – a string containing at least an action
Returns:action, [param1, param2, ...]

Discovery

networkzero.advertise(name, address=None, fail_if_exists=False, ttl_s=20)

Advertise a name at an address

Start to advertise service name at address address. If the address is not supplied, one is constructed and this is returned by the function. ie this is a typical use:

address = nw0.advertise("myservice")
Parameters:
  • name – any text
  • address – either “ip:port” or None
  • fail_if_exists – fail if this name is already registered?
  • ttl_s – the advert will persist for this many seconds other beacons
Returns:

the address given or constructed

networkzero.discover(name, wait_for_s=60)

Discover a service by name

Look for an advert to a named service:

address = nw0.discover("myservice")
Parameters:
  • name – any text
  • wait_for_s – how many seconds to wait before giving up
Returns:

the address found or None

networkzero.discover_all()

Produce a list of all known services and their addresses

Ask for all known services as a list of 2-tuples: (name, address) This could, eg, be used to form a dictionary of services:

services = dict(nw0.discover_all())
Returns:a list of 2-tuples [(name, address), ...]

Message-Sending

networkzero.send_message_to(address, message=None, wait_for_reply_s=<Forever>)

Send a message and return the reply

Parameters:
  • address – a nw0 address (eg from nw0.discover)
  • message – any simple Python object, including text & tuples
  • wait_for_reply_s – how many seconds to wait for a reply [default: forever]
Returns:

the reply returned from the address or None if out of time

networkzero.wait_for_message_from(address, wait_for_s=<Forever>, autoreply=False)

Wait for a message

Parameters:
  • address – a nw0 address (eg from nw0.advertise)
  • wait_for_s – how many seconds to wait for a message before giving up [default: forever]
  • autoreply – whether to send an empty reply [default: No]
Returns:

the message received from another address or None if out of time

networkzero.send_reply_to(address, reply=None)

Reply to a message previously received

Parameters:
  • address – a nw0 address (eg from nw0.advertise)
  • reply – any simple Python object, including text & tuples
networkzero.send_news_to(address, topic, data=None)

Publish news to all subscribers

Parameters:
  • address – a nw0 address, eg from nw0.advertise
  • topic – any text object
  • data – any simple Python object including test & tuples [default: empty]
networkzero.wait_for_news_from(address, prefix='', wait_for_s=<Forever>)

Wait for news whose topic starts with prefix.

Parameters:
  • address – a nw0 address, eg from nw0.discover
  • prefix – any text object [default: all messages]
  • wait_for_s – how many seconds to wait before giving up [default: forever]
Returns:

a 2-tuple of (topic, data) or (None, None) if out of time