Server Usage

The server is a self-contained process to which clients connect for each session via websockets. To connect, it is sufficient for the client to know on which address the server is listening.

For testing, we recommend to run the server locally – i.e. on the localhost. Thus, we use the exemplary address ws://localhost:8765 here. If the server listens on another host and/or port, adjust the address appropriately.

Connecting the Client

The battleship_mp.client module does not need to be passed the server address. Instead, it reads the address from an environment variable called BMP_SERVER_URL.

You can easily set the variable when launching your program from the shell:

$ BMP_SERVER_URL="ws://localhost:8765" my_client_program.py

Alternatively, if you program is aware of the server address – e.g. from the CLI or a configuration file - it can programmatically modify os.environ:

import os

os.environ["BMP_SERVER_URL"] = "ws://localhost:8765"

Running the Server

The battleship_mp.server module provides a self-contained, asynchronous server implementation. The server can be run from the command line and takes the port as well as optionally the addresses or hostnames to bind to. By default, the server binds to all addresses of its host.

For testing, it is suitable to bind to localhost:

~ $ python3 -m battleship_mp.server 8765 localhost

This ensures that the server is only available to clients running on the same machine. There is no exposure to the local network or internet.

Note

The hostnames must correspond to addresses of the host on which the server runs; it does not make sense to bind to other hosts. For example, localhost conventionally just corresponds to the local loopback addresses 127.0.0.1 and ::1.