-
-
Save pdelvo/3914547 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
def get_info(host, port): | |
#Set up our socket | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((host, port)) | |
#Send 0xFE: Server list ping | |
s.send('\xfe') | |
#Send 0x01 | |
s.send('\x01') | |
#Read as much data as we can (max packet size: 241 bytes) | |
d = s.recv(256) | |
s.close() | |
#Check we've got a 0xFF Disconnect | |
assert d[0] == '\xff' | |
#Remove the packet ident (0xFF) and the short containing the length of the string | |
#Decode UCS-2 string | |
#Split into list | |
d = d[3:].decode('utf-16be').split(u'\x00') | |
#Return a dict of values | |
return {'protocol_version': int(d[1]), | |
'minecraft_version': d[2], | |
'motd': d[3], | |
'players': int(d[4]), | |
'max_players': int(d[5])} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Came up with a version compatible with both old and new responses depending on what the server sends back