Skip to content

Server API

CLICK Modbus TCP simulator and server utilities.

pyclickplc.ClickServer

Modbus TCP server simulating a CLICK PLC.

bound_port property

bound_port: int | None

Return the port the server listens on, or None when it is not listening.

It is None whenever is_running() returns False.

is_running

is_running() -> bool

Return True while the server is listening.

list_clients

list_clients() -> list[ServerClientInfo]

Return the currently connected Modbus TCP clients.

disconnect_client

disconnect_client(client_id: str) -> bool

Disconnect the client with this client_id.

Return True if that client was connected, and False otherwise.

disconnect_all_clients

disconnect_all_clients() -> int

Disconnect all connected clients and return how many were closed.

start async

start() -> None

Start the Modbus TCP server.

If the server stopped listening on its own (is_running() returns False although you did not call stop()), start() closes the old listener and its clients, then listens again. If you cancel start() before it returns, the server stays stopped. If you call stop() while start() is still running, start() returns without listening.

Raises:

Type Description
RuntimeError

The server is already listening, can't listen on its host and port, or runs on another event loop.

stop async

stop() -> None

Stop the server, closing every connection.

If you cancel stop(), the server has still stopped. A client that has not closed after 1 s is dropped. If you call stop() while start() is running, the server stops what that start() opens.

Raises:

Type Description
RuntimeError

The server runs on another event loop. Stop it on that loop.

serve_forever async

serve_forever() -> None

Start the server and block until stop().

If the server stopped listening on its own and start() replaces the listener, this call keeps serving. If you cancel this call, the server stops.

Raises:

Type Description
RuntimeError

The server is already listening, can't listen on its host and port, runs on another event loop, or stopped listening on its own and start() could not listen again.

pyclickplc.MemoryDataProvider

Bases: AddressNormalizerMixin

In-memory DataProvider for testing and simple use cases.

get

get(address: str) -> PlcValue

Return the value at address. This method calls read.

set

set(address: str, value: PlcValue) -> None

Set address to value. This method calls write.

bulk_set

bulk_set(values: Mapping[str, PlcValue]) -> None

Set multiple values at once.

pyclickplc.ServerClientInfo dataclass

Connected client metadata exposed by ClickServer.

pyclickplc.run_server_tui async

run_server_tui(
    server: ClickServer,
    *,
    prompt: str = "clickserver> ",
    input_fn: Callable[[str], str] | None = None,
    output_fn: Callable[[str], None] | None = None,
) -> None

Run a basic interactive command loop for a ClickServer.