def _socket_handler(self, conn, addr): conn.settimeout(1.0) buffer = b'' feeder = Feeder(conn) while not self.do_stop: try: command = None while command is None and not self.do_stop: command, buffer = feeder.feed(buffer) if command: logging.info("Command = %s", command) self._send_data_to_socket(conn, command.reply()) if type(command) in [Quit, QuitD]: conn.close() break elif type(command) == Finish: conn.close() self.do_stop = True break except socket.timeout: continue except OSError as msg: logging.error("OSError: %s", msg) self.stop_server() else: self._send_data_to_socket(conn, AckFinish().pack()) conn.close() self.threads.remove(threading.currentThread()) logging.info("Thread off conn=%s", conn)
def run_client(self, conn): feeder = Feeder(self.commands) tail = bytes() while True: try: chunk = tail + conn.recv(self.CHUNK_SIZE) packet, tail = feeder.feed(chunk) if not packet: continue process = getattr(self, packet.__class__.__name__.lower()) kwargs = {} kw_only = get_keyword_args(process) if 'conn' in kw_only: kwargs['conn'] = conn process(packet, **kwargs) except (socket.timeout, OSError): conn.close() self.clients.pop(conn, None) return
def setUp(self): self.connection = Mock() self.feeder = Feeder(connection=self.connection)