#!/usr/bin/env python # -*- coding: utf-8 -*- # Modular system for querying game servers. # # Author: Roman Vasiyarov # License: GPLv3 # $Id$ # # Minecraft module # import sys import struct from datetime import datetime SOCK_TCP = True NAMES = [ "paragraph", "protocol", "version", "motd", "clients", "clientsmax", # should be last "ping" ] MC_MAGIC_QUERY = 0xfe MC_MAGIC_RESPONSE = 0xff MC_MAGIC_SEPARATOR = b"\x00\xa7".decode("utf-16be") # 0x00 for pre-1.4 and looked like motd$clients$clientsmax # still works on 1.6.4 MC_QUERY_VERSION = 0x01 def process(pkt, verbose=False): n = 3 # "header" size seq = [None] * len(NAMES) out = "" try: seq = struct.unpack(">BH", pkt[:n]) except: pass try: if seq[0] != MC_MAGIC_RESPONSE: raise # get byte-length of message ln = seq[1] * 2 msg = pkt[n:n + ln].decode("utf-16be") par, msg = msg.split(MC_MAGIC_SEPARATOR) seq = msg.split(b"\x00") except: seq = [] return seq + [None] * max(0, len(NAMES) - 1 - len(seq)) def query(sock, servers, verbose = False): if not sock or not len(servers): return info = {} pkt = struct.pack("