Skip to content

Instantly share code, notes, and snippets.

@compbrain
Created July 29, 2011 02:17

Revisions

  1. compbrain revised this gist Jul 29, 2011. 1 changed file with 51 additions and 0 deletions.
    51 changes: 51 additions & 0 deletions watch_teeworlds.py
    Original file line number Diff line number Diff line change
    @@ -1 +1,52 @@
    #!/usr/bin/python

    __author__ = 'Will Nowak <[email protected]>'

    import socket


    def GetUdpSocket():
    return socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    class TeeWorldsWatcher(object):
    def __init__(self):
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def RequestStatus(self, host, port):
    self.sock.sendto("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFFgief",
    (host, int(port)))

    def ReadStatus(self):
    buf, _ = self.sock.recvfrom(2048)
    return buf

    def ParseStatusBuffer(self, buf):
    status = {'players': {}}
    if not len(buf) > 14:
    raise Exception('Invalid status packet')
    status_packet = buf[14:]
    split_vitals = status_packet.split("\x00", 8)
    (version, server_name, server_map, gametype, has_password, ping,
    num_players, max_players) = split_vitals[:8]
    status['version'] = version.strip()
    status['name'] = server_name.strip()
    status['map'] = server_map.strip()
    status['gametype'] = gametype.strip()
    status['has_password'] = bool(int(has_password))
    status['ping'] = int(ping)
    status['num_players'] = int(num_players)
    status['max_players'] = int(max_players)
    playerstring = str(split_vitals[-1])
    while playerstring:
    player, score, playerstring = playerstring.split("\x00", 2)
    status['players'][player] = int(score)
    return status

    if __name__ == '__main__':
    import sys
    _, host, port = sys.argv
    tww = TeeWorldsWatcher()
    tww.RequestStatus(host, port)
    buf = tww.ReadStatus()
    print tww.ParseStatusBuffer(buf)

  2. compbrain created this gist Jul 29, 2011.
    1 change: 1 addition & 0 deletions watch_teeworlds.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    #!/usr/bin/python