-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmcrcon.py
90 lines (65 loc) · 2.25 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import collections
import struct
bufsize = 4096
Packet = collections.namedtuple("Packet", ("ident", "kind", "payload"))
class IncompletePacket(Exception):
def __init__(self, minimum):
self.minimum = minimum
def decode_packet(data):
"""
Decodes a packet from the beginning of the given byte string. Returns a
2-tuple, where the first element is a ``Packet`` instance and the second
element is a byte string containing any remaining data after the packet.
"""
if len(data) < 14:
raise IncompletePacket(14)
length = struct.unpack("<i", data[:4])[0] + 4
if len(data) < length:
raise IncompletePacket(length)
ident, kind = struct.unpack("<ii", data[4:12])
payload, padding = data[12:length-2], data[length-2:length]
assert padding == b"\x00\x00"
return Packet(ident, kind, payload), data[length:]
def encode_packet(packet):
"""
Encodes a packet from the given ``Packet` instance. Returns a byte string.
"""
data = struct.pack("<ii", packet.ident, packet.kind) + packet.payload + b"\x00\x00"
return struct.pack("<i", len(data)) + data
def receive_packet(sock):
"""
Receive a packet from the given socket. Returns a ``Packet`` instance.
"""
data = b""
while True:
try:
return decode_packet(data)[0]
except IncompletePacket as exc:
while len(data) < exc.minimum:
data += sock.recv(exc.minimum - len(data))
def send_packet(sock, packet):
"""
Send a packet to the given socket.
"""
sock.sendall(encode_packet(packet))
def login(sock, password):
"""
Send a "login" packet to the server. Returns a boolean indicating whether
the login was successful.
"""
send_packet(sock, Packet(0, 3, password.encode("utf8")))
packet = receive_packet(sock)
return packet.ident == 0
def command(sock, text):
"""
Sends a "command" packet to the server. Returns the response as a string.
"""
send_packet(sock, Packet(0, 2, text.encode("utf8")))
send_packet(sock, Packet(1, 0, b""))
response = b""
while True:
packet = receive_packet(sock)
if packet.ident != 0:
break
response += packet.payload
return response.decode("utf8")