"""Listens to an RCON server for messages.""" import asyncio import logging import math import berconpy as rcon IP = "217.72.204.88" PORT = 2313 PASSWORD = "u1BSlMM4LH0" log = logging.getLogger("berconpy") log.setLevel(logging.DEBUG) handler = logging.StreamHandler() handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")) log.addHandler(handler) client = rcon.AsyncRCONClient() @client.dispatch.on_login async def on_login(): print("on_login") @client.dispatch.on_message async def on_message(message: str): print("on_message:", message) @client.dispatch.on_command async def server_response_to_command(response: str): # this event also includes keep alive commands we send to the server; # for handling commands, reading the return value of # `await client.send_command()` is the recommended method if not response: return print("on_command: ") print("on_command:", response) # Other events are documented in AsyncRCONClient async def main(): async with client.connect(IP, PORT, PASSWORD): await asyncio.sleep(math.inf) if __name__ == "__main__": asyncio.run(main())