API

Server

class network.Server(name, kind, port=None, handler=<function raise_not_implemented at 0x2cf58c0>, text=())[source]

This is a network server capable of handling request/reply style communication (using ZeroMQ). When running, it maintains a zeroconf entry (using avahi) so that it can be discovered automatically over LANs.

Parameters:
  • name – The human-readable name of this server (e.g., “Bob’s Dungeon Game”).
  • kind – The machine-readable kind of server this is. (e.g., “dungeon”)
  • port – The TCP port to run the server on. If unspecified, it picks a random port (in the future, this will be better, and pick an unused port).
  • handler – The function to run when on recieving a client request.
  • text – A list of strings to put in the avahi record.

The handler can also be specified using the server.handler decorator:

>>> server = Server("Bob's Dungeon Game", "dungeon")
>>> @server.handler
... def on_request(msg):
...     if msg == 'attack':
...         return 'You attack a monster!'
...     return 'Nothing happens.'
...
>>> server.run()
handler(func)[source]

Decorator function which provides a more readable way of defining the handler for a server. Be aware that this will override any previously defined handler.

run()[source]

Start up the server, and serve requests until .stop() is called.

The server runs using a GLib mainloop so that avahi integration works, which may be helpful for integrating other things (you could put a Gtk application in the same thread).

stop()[source]

Stop serving requests. This function is thread-safe, so you can call it from other threads, or you can call it from within the GLib mainloop (using GLib.timeout_add or GLib.idle_add or some other signal)

Client

class network.Client(kind)[source]

This is the counterpart to the Server. This client is capable of discovering servers on the LAN and connecting to them (or to any other server) using the same request/reply protocol (ZeroMQ).

Parameters:kind – The machine-readable kind of server you want to connect to. Currently only used by find_server().
connect(target)[source]

Connect to a server. If you connect to multiple servers, ZeroMQ will load-balance between them.

Parameters:target – A ZeroMQ endpoint (e.g., “tcp://192.168.0.1:12345”)
find_server(browser_cls=<class 'network.ConsoleServerBrowser'>, connect=True)[source]

Find a server on the local network (using avahi) and (optionally) connect to it.

Parameters:
  • browser – A Browser class to provide the user interface.
  • connect – If true, the client will immediately connect to the chosen server.
Return type:

(string) The ZeroMQ endpoint of the chosen server.

recv_async()[source]

After sending a message to the server with .send_async(), calling this method will wait for the response from the server and return it.

Return type:string (the data returned from the server)
send(data)[source]

Send a message to the server, wait for the response, and return it. Both the sent and received messages are binary strings, not unicode.

Parameters:data – The data to be sent to the server.
Return type:string (the data returned from the server)
send_async(data)[source]

Send a message to the server, without waiting for a response.

Currently this method waits for acknowledgement of the message by a server, but this extra latency will be removed in the future.

In order to get the result of the message, call .read_async.

Parameters:data – The data to be sent to the server.

Project Versions

Table Of Contents

Previous topic

Tutorial

This Page