public
Last active

  • Download Gist
gistfile1.py
Python
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
import socket
import struct
import json
 
def unpack_varint(s):
d = 0
for i in range(5):
b = ord(s.recv(1))
d |= (b & 0x7F) << 7*i
if not b & 0x80:
break
return d
 
def pack_varint(d):
o = ""
while True:
b = d & 0x7F
d >>= 7
o += struct.pack("B", b | (0x80 if d > 0 else 0))
if d == 0:
break
return o
 
def pack_data(d):
return pack_varint(len(d)) + d
 
def pack_port(i):
return struct.pack('>H', i)
 
def get_info(host='localhost', port=25565):
 
# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
 
# Send handshake + status request
s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01"))
s.send(pack_data("\x00"))
 
# Read response
unpack_varint(s) # Packet length
unpack_varint(s) # Packet ID
l = unpack_varint(s) # String length
 
d = ""
while len(d) < l:
d += s.recv(1024)
 
# Close our socket
s.close()
 
# Load json and return
return json.loads(d.decode('utf8'))

Thanks for this! I wrote a simple OO wrapper using it as a base. https://github.com/markfickett/minecraftstatus/blob/master/mcstatus.py

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.