berconpy.RCONClient

class berconpy.RCONClient(*, dispatch=None, protocol=None)

Bases: object

An implementation of the RCON client protocol using asyncio.

Methods

add_listener(event, func)

A shorthand for the EventDispatcher.add_listener() method.

close()

Closes the connection.

connect(ip, port, password)

Returns an asynchronous context manager for logging into the given IP and port with password.

is_connected()

Indicates if the client has a currently active connection with the server.

is_logged_in()

Indicates if the client is currently authenticated with the server.

is_running()

Indicates if the client is running.

listen([event])

A shorthand for the EventDispatcher.listen() decorator.

remove_listener(event, func)

A shorthand for the EventDispatcher.remove_listener() method.

send_command(command)

Sends a command to the server and waits for a response.

wait_for(event, *[, check, timeout])

A shorthand for EventDispatcher.wait_for().

add_listener(event, func)

A shorthand for the EventDispatcher.add_listener() method.

See the EventDispatcher for a list of supported events.

Parameters:
  • event (str) – The event to listen for.

  • func (Callable) – The function to dispatch when the event is received.

Return type:

None

close()

Closes the connection.

This method is idempotent and can be called multiple times consecutively.

Changed in version 1.1: This method no longer causes the current task to be cancelled.

connect(ip, port, password)

Returns an asynchronous context manager for logging into the given IP and port with password.

Example usage:

client = berconpy.RCONClient()
async with client.connect(ip, port, password):
    print("Connected!")
print("Disconnected!")

If an unexpected error occurs after successfully logging in, the current task that the context manager is used in will be cancelled to prevent the script being stuck in an infinite loop.

Raises:
  • LoginFailure – The client failed to log into the server.

  • RuntimeError – This method was called while the client is already connected.

is_connected()

Indicates if the client has a currently active connection with the server.

Return type:

bool

is_logged_in()

Indicates if the client is currently authenticated with the server.

Return type:

bool | None

Returns:

True if authenticated or None if no response has been received from the server.

Raises:

LoginFailure – The client failed to log into the server.

is_running()

Indicates if the client is running. This may not necessarily mean that the client is connected.

Return type:

bool

listen(event=None)

A shorthand for the EventDispatcher.listen() decorator.

Example usage:

>>> client = RCONClient()
>>> @client.listen()
... async def on_login():
...     print("We have logged in!")
Parameters:

event (Optional[str]) – The event to listen for. If None, the function name is used as the event name.

Return type:

Callable[[TypeVar(T)], TypeVar(T)]

remove_listener(event, func)

A shorthand for the EventDispatcher.remove_listener() method.

This method should be a no-op if the given event and function does not match any registered listener.

Parameters:
  • event (str) – The event used by the listener.

  • func (Callable) – The function used by the listener.

Return type:

None

async send_command(command)

Sends a command to the server and waits for a response.

Parameters:

command (str) – The command to send.

Return type:

str

Returns:

The server’s response as a string.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

async wait_for(event, *, check=None, timeout=None)

A shorthand for EventDispatcher.wait_for().