def update(self, timeout=0.002): rlist = [] if self.s is not None: rlist.append(self.s) rlist.extend(self.connections.keys()) r, _, _ = select.select(rlist, [], [], timeout) for c in r: if c is self.s: client_sock, endpoint = self.s.accept() print('accept from: {}'.format(endpoint)) cid = six.text_type(uuid.uuid4()) conn = Connection(cid, client_sock, endpoint, self.RX_SIZE) self.connections[client_sock] = conn self.connections_endpoints[endpoint] = conn else: conn = self.connections.get(c) if not conn: continue try: for packet in conn.recv(): self.rq.put((conn.cid, packet)) except ConnectionReset: self.connections.pop(c) return self.recv()
def connect(self, endpoint): if endpoint in self.connections_endpoints: raise RuntimeError("Already connected to {}".format(endpoint)) c = socket.socket() c.connect(endpoint) cid = six.text_type(uuid.uuid4()) conn = Connection(cid, c, endpoint, self.RX_SIZE) self.connections[c] = conn self.connections_endpoints[endpoint] = conn
def build_request(self, method, *args, **kwargs): rid = six.text_type(uuid.uuid4()) ret = { 'id': rid, 'jsonrpc': '2.0', 'method': method, 'params': args or kwargs or [], } self.pending_response[rid] = None return ret
def handle_request(self, req): ret = { 'id': req['id'], 'jsonrpc': req['jsonrpc'], } method = req['method'] params = req['params'] try: result = self.dispatch(method, *params) ret['result'] = result except Exception as e: ret['error'] = { 'message': '{}\n\n|--- REMOTE TRACEBACK ---|\n{}|--- REMOTE TRACEBACK END ---|' .format(six.text_type(e), traceback.format_exc()) } return ret
def handleConnected(self2): self.connections[self2] = six.text_type(uuid.uuid4()) self.connections_endpoints[self2.address] = self2 print('server on accept. {}'.format(self2))