def send_data(self):
        """Send data packets from the local file to the server"""
        if not self.connection._sock:
            raise err.InterfaceError(0, "")
        conn: Connection = self.connection

        try:
            with open(self.filename, "rb") as open_file:
                packet_size = min(
                    conn.max_allowed_packet, 16 * 1024
                )  # 16KB is efficient enough
                while True:
                    chunk = open_file.read(packet_size)
                    if not chunk:
                        break
                    conn.write_packet(chunk)
        except OSError:
            raise err.OperationalError(
                ER.FILE_NOT_FOUND,
                f"Can't find file '{self.filename}'",
            )
        finally:
            if not conn._closed:
                # send the empty packet to signify we are done sending data
                conn.write_packet(b"")
Send data packets from the local file to the server
pymysql/connections.py