Debian Codegraph @

docs/source/usage.rst

raw
1 Usage
2 =====
3 Source RCON
4 -----------
5 To connect to a server using the Source RCON protocol, use :py:class:`rcon.source.Client`.
6
7 .. code-block:: python
8
9 from rcon.source import Client
10
11 with Client('127.0.0.1', 5000, passwd='mysecretpassword') as client:
12 response = client.run('some_command', 'with', 'some', 'arguments')
13
14 print(response)
15
16 BattlEye RCon
17 -------------
18 To connecto to a server using the BattlEye RCon protocol, use :py:class:`rcon.battleye.Client`.
19
20 .. code-block:: python
21
22 from rcon.battleye import Client
23
24 with Client('127.0.0.1', 5000, passwd='mysecretpassword') as client:
25 response = client.run('some_command', 'with', 'some', 'arguments')
26
27 print(response)
28
29 Handling server messages
30 ~~~~~~~~~~~~~~~~~~~~~~~~
31 Since the BattlEye RCon server will also send server messages to the client
32 alongside command responses, you can register an event handler to process
33 those messages:
34
35 .. code-block:: python
36
37 from rcon.battleye import Client, ServerMessage
38
39 def my_message_handler(server_message: ServerMessage) -> None:
40 """Print server messages."""
41
42 print('Server message:', server_message)
43
44 with Client(
45 '127.0.0.1',
46 5000,
47 passwd='mysecretpassword',
48 message_handler=my_message_handler
49 ) as client:
50 response = client.run('some_command', 'with', 'some', 'arguments')
51
52 print('Response:', response)
53
54 Configuration
55 -------------
56 `rconclt` servers can be configured in :file:`/etc/rcon.conf`.
57 The configuration file format is:
58
59 .. code-block:: ini
60
61 [<server_name>]
62 host = <hostname_or_ip_address>
63 port = <port>
64 passwd = <password>
65
66 The :code:`passwd` entry is optional.
67
68 rconclt
69 -------
70 `rconclt` is an RCON client script to communicate with game servers via the RCON protocol using the shell.
71 To communicate with a server, run:
72
73 .. code-block:: bash
74
75 rconclt [options] <server> <command> [<args>...]
76
77 rconshell
78 ---------
79 `rconshell` is an interactive RCON console to interact with game servers via the RCON protocol.
80 To start a shell, run:
81
82 .. code-block:: bash
83
84 rconshell [server] [options]
85
86 Handling connection timeouts.
87 -----------------------------
88 You can specify an optional :code:`timeout=<sec>` parameter to allow a connection attempt to time out.
89 If a timeout is reached during a connection attempt, it will raise a `socket.timeout <https://docs.python.org/3/library/socket.html#socket.timeout>`_ exception.
90 The following example will raise a connection timeout after 1.5 seconds:
91
92 .. code-block:: python
93
94 try:
95 with Client('127.0.0.1', 5000, timeout=1.5) as client:
96 <do_stuff>
97 except socket.timeout as timeout:
98 <handle_connection_timeout>
99
100 .. _configuration: