<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_file(self, filename, tunnel_addr, tunnel_port):
 """ Writes memory dump to a local file :type filename: str :param filename: memory dump output filename :type tunnel_addr: str :param tunnel_port: ssh tunnel hostname or ip :type tunnel_port: int :param tunnel_port: ssh tunnel port """

        if self.progressbar:
            self.bar = ProgressBar(widgets=self.widgets,
                                   maxval=self.max_size).start()
            self.bar.start()

        with open(filename, 'wb') as self.outfile:
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.connect((tunnel_addr, tunnel_port))
            self.sock.settimeout(self.sock_timeout)
            bytes_since_update = 0
            while True:
                try:
                    data = self.sock.recv(self.recv_size)
                    data_length = len(data)
                    if not data:
                        break
                    self.outfile.write(data)
                    self.transfered = self.transfered + data_length
                    bytes_since_update +=  data_length
                    data = None
                    data_length = 0
                    if bytes_since_update > self.update_threshold:
                        self.update_progress()
                        bytes_since_update = 0

                except (socket.timeout, socket.error) as ex:
                    if isinstance(ex, socket.timeout):
                        break
                    elif isinstance(ex, socket.error):
                        if ex.errno == errno.EINTR:
                            pass
                        else:
                            self.cleanup()
                            raise
                    else:
                        self.cleanup()
                        raise

        self.cleanup()
        logger.info('{0}: capture complete: {1}'.format(self.remote_addr,
                                                        filename))
        return True