# A socket class for communicating in Go Text Protocol # # A simple connect method is defined, and everything else is # suffixed with _gtp, so that this can be subclassed easily, # e.g. for communicating with a server import socket class GTPSocket: def __init__(self, info): self.info = info self.socket = None def connect(self): try: self.socket = socket.create_connection(info) return True except: return False 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