-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcrcon.py
67 lines (53 loc) · 2.05 KB
/
mcrcon.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import socket
import select
import struct
import time
class MCRconException(Exception):
pass
class MCRcon(object):
socket = None
def connect(self, host, port, password):
if self.socket is not None:
raise MCRconException("Already connected")
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))
self.send(3, password)
def disconnect(self):
if self.socket is None:
raise MCRconException("Already disconnected")
self.socket.close()
self.socket = None
def read(self, length):
data = b""
while len(data) < length:
data += self.socket.recv(length - len(data))
return data
def send(self, out_type, out_data):
if self.socket is None:
raise MCRconException("Must connect before sending data")
# Send a request packet
out_payload = struct.pack('<ii', 0, out_type) + out_data.encode('utf8') + b'\x00\x00'
out_length = struct.pack('<i', len(out_payload))
self.socket.send(out_length + out_payload)
# Read response packets
in_data = ""
while True:
# Read a packet
in_length, = struct.unpack('<i', self.read(4))
in_payload = self.read(in_length)
in_id, in_type = struct.unpack('<ii', in_payload[:8])
in_data_partial, in_padding = in_payload[8:-2], in_payload[-2:]
# Sanity checks
if in_padding != b'\x00\x00':
raise MCRconException("Incorrect padding")
if in_id == -1:
raise MCRconException("Login failed")
# Record the response
in_data += in_data_partial.decode('utf8')
# If there's nothing more to receive, return the response
if len(select.select([self.socket], [], [], 0)[0]) == 0:
return in_data
def command(self, command):
result = self.send(2, command)
time.sleep(0.003) # MC-72390 workaround
return result