A
A
andreysuper12018-03-27 23:56:30
Python
andreysuper1, 2018-03-27 23:56:30

How to find out if a minecraft server is running at a certain time?

It is required to find out if the minecraft server is running on a specific ip and port. Preferably with a python implementation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fadi Haj, 2018-03-31
@fdhaj

def isminecraft(addr, timeout=5):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect(addr)
        s.settimeout(timeout)

        buf = struct.pack('xBB', 4, len(addr[0].encode('utf-8')))
        buf += addr[0].encode('utf-8')
        buf += struct.pack('HB', addr[1], 1)

        pkt = struct.pack('B', len(buf))
        pkt += buf
        pkt += b'\x01\x00'

        s.send(pkt)

        try:
            bufsize = struct.unpack('B', s.recv(3)[2:])[0]
            js = s.recv(bufsize, socket.MSG_WAITALL)
        except socket.timeout:
            return (False, None)

    try:
        response = json.loads(js)
        version = response['version']['name']
        return (True, version)
    except (ValueError, KeyError):
        return (False, None)

Usage example:
addrs = [
    ('192.168.99.100', 22508),
    ('192.168.99.100', 32761),
]

for addr in addrs:
    isok, version = isminecraft(addr)

    if isok:
        print('[%s] Minecraft version: %s' % (str(addr), version))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question