-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
35 lines (29 loc) · 1.11 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class RconError(ValueError):
pass
def strip_rcon_logline(response):
lines = response.splitlines()
if len(lines) >= 1:
last_line = lines[len(lines) - 1]
if 'rcon from' in last_line:
return '\n'.join(lines[:len(lines) - 1])
return response
def send_rcon_command(host, port, rcon_password, command,
raise_errors=False, num_retries=3, timeout=3.0):
from valve.rcon import (RCON, RCONCommunicationError, RCONAuthenticationError)
attempts = 0
while attempts < num_retries:
attempts += 1
try:
with RCON((host, port), rcon_password, timeout=timeout) as rcon:
response = rcon(command)
return strip_rcon_logline(response)
except ConnectionRefusedError:
return None
except KeyError:
raise RconError('Incorrect rcon password')
except (RCONCommunicationError, RCONAuthenticationError) as e:
if attempts >= num_retries:
if raise_errors:
raise RconError(str(e))
else:
return None