class Agent(object): def __init__(self, addr): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(addr) self.port = Port(sock) def list(self): self.port.write("?") return json.loads(self.port.read()) def fetch(self, want="*"): if want == "*": req = "*" else: req = json.dumps(want) self.port.write(req) return json.loads(self.port.read())
def listen(self, host): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host, SERVICE_PORT)) port = Port(sock) port.write(self.subs) while True: res = port.read() if res: self.queue.put(res)
def listen_telescreens(self, listen_port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(("", listen_port)) sock.listen(10000) while True: client_sock, _ = sock.accept() port = Port(client_sock) uuid = port.read() self.telescreens[uuid] = port