Skip to content

Instantly share code, notes, and snippets.

@compbrain
Created July 29, 2011 02:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save compbrain/1113002 to your computer and use it in GitHub Desktop.
Save compbrain/1113002 to your computer and use it in GitHub Desktop.
Python Teeworlds Watcher
#!/usr/bin/python
__author__ = 'Will Nowak <wan@ccs.neu.edu>'
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment