Created
July 29, 2011 02:17
-
-
Save compbrain/1113002 to your computer and use it in GitHub Desktop.
Python Teeworlds Watcher
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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment