# A class for communicating in Go Text Protocol # # An already-connected socket should be passed in, then this class # can be used to handle GTP simply class GTPSocket: def __init__(self, socket): self.socket = None self.id = None def get_gtp(self): msg = None while msg is None: try: msg = self.socket.recv(1024) if not self._validate_gtp(msg): print 'Error: Incoming data was not a valid GTP message' msg = None except: pass def send_gtp(self, msg): try: if _validate_gtp(msg): self.socket.send(msg) return True else: print 'Error: Outgoing data was not a valid GTP message' return False except: return False def _validate_gtp(self, gtp): return True