def send_rcon_command(self, command, args=tuple(), fetch_response=True): """ Send any command to the server leading whitespace is stripped from the response :param command: the comand to send :param args: tuple or list of arguments to be appended to the command. Can be also a string or an int if only one argument is expected. :param fetch_response: Whether to receive response from server. Set this to False if you're not expecting a response; WARNING: If there is a response and you don't fetch it, it may be output as a response of your next command. :return list of lines responded from the server or None if fetch_response == False """ command = build_rcon_command(command, args) command_length = encode_bytes( len(command) & 0xFF, len(command) >> 8 & 0xFF) payload = self.rcon_password_bytes + command_length + command self.send_request(OPCODE_RCON, extras=payload, return_response=False) if fetch_response: result = [] while True: response = self.receive() if response is None: break line = decode_string(response, 0, 2) if line: result.append(line.lstrip()) else: break if len(result) == 1 and result[0] == 'Invalid RCON password.': raise InvalidRconPassword return result
def test_build_rcon_command_float(self): self.assertEqual(b'gravity 0.008', utils.build_rcon_command('gravity', args=0.008)) self.assertEqual(b'test 0.008', utils.build_rcon_command('test', args=0.008))
def test_build_rcon_command_bool(self): self.assertEqual(b'test 1', utils.build_rcon_command('test', args=True)) self.assertEqual(b'test 0', utils.build_rcon_command('test', args=False))
def test_build_rcon_command_int(self): self.assertEqual(b'kick 5', utils.build_rcon_command('kick', args=5)) self.assertEqual(b'kick 5', utils.build_rcon_command('kick', args=[5])) self.assertEqual(b'kick 0', utils.build_rcon_command('kick', args=0)) self.assertEqual(b'kick 0', utils.build_rcon_command('kick', args=[0]))
def test_build_rcon_command_string(self): self.assertEqual(b'language Polish', utils.build_rcon_command('language', args='Polish')) self.assertEqual(b'language Polish', utils.build_rcon_command('language', args=['Polish']))
def test_build_rcon_command(self): self.assertEqual(b'cmdlist', utils.build_rcon_command('cmdlist')) self.assertEqual(b'cmdlist', utils.build_rcon_command('cmdlist', args=[])) self.assertEqual(b'cmdlist', utils.build_rcon_command('cmdlist', args=tuple()))