Skip to Content

Download Service

TL;DR

The Download service downloads files from external URLs through remote machines using client.download.url(). Useful for accessing files behind firewalls or using the remote machine’s network identity. Supports progress callbacks, chunked transfers (up to 100MB chunks), tqdm progress bars, and retry with exponential backoff.

Download files from URLs through remote machines.

How do I download from a URL?

from pathlib import Path from cmdop import CMDOPClient # Connect to the remote CMDOP agent client = CMDOPClient.remote(api_key="cmd_xxx") # Download a file from a URL through the remote machine result = await client.download.url( url="https://example.com/file.zip", local_path=Path("./file.zip"), ) # Check whether the download succeeded or failed if result.success: print(f"Downloaded {result.size} bytes") else: print(f"Failed: {result.error}")

What parameters are available?

# Download with custom timeout, chunk size, and progress tracking result = await client.download.url( url="https://example.com/large-file.tar.gz", local_path=Path("./large-file.tar.gz"), timeout=300, # Total timeout in seconds chunk_size=10 * 1024 * 1024, # Transfer in 10MB chunks on_progress=progress_callback, # Called after each chunk completes )

How do I track download progress?

# Define a callback that receives bytes downloaded and total size def progress_callback(bytes_done: int, total_bytes: int): percentage = (bytes_done / total_bytes) * 100 print(f"Progress: {percentage:.1f}%") # Pass the callback to on_progress to receive updates after each chunk result = await client.download.url( url="https://example.com/file.zip", local_path=Path("./file.zip"), on_progress=progress_callback, )

What does the DownloadResult contain?

from cmdop.services.download import DownloadResult # DownloadResult fields available after a download completes result: DownloadResult print(f"Success: {result.success}") # True if download succeeded print(f"Size: {result.size} bytes") # Total bytes downloaded print(f"Duration: {result.duration_seconds}s") # Elapsed time print(f"Error: {result.error}") # None if success, error message otherwise

What download metrics are available?

For detailed metrics:

from cmdop.services.download import DownloadMetrics # DownloadMetrics provides detailed transfer statistics metrics: DownloadMetrics print(f"Total bytes: {metrics.total_bytes}") # Raw byte count print(f"Chunks transferred: {metrics.chunks_transferred}") # Number of chunks sent print(f"Retries: {metrics.retries}") # How many retries occurred print(f"Speed: {metrics.speed_mbps} MB/s") # Average transfer speed

What are common use cases?

How do I download through a remote machine?

Download files that are only accessible from the remote network:

# Access a file only reachable from the remote machine's internal network result = await client.download.url( url="http://internal-server.local/data.json", local_path=Path("./data.json"), )

How do I download large files?

For large files, use appropriate chunk size:

# Use larger chunk sizes and longer timeouts for big files result = await client.download.url( url="https://cdn.example.com/dataset.tar.gz", local_path=Path("./dataset.tar.gz"), chunk_size=20 * 1024 * 1024, # 20MB chunks (max 100MB) timeout=600, # 10 minute timeout for large transfers )

How do I show a tqdm progress bar?

from tqdm import tqdm pbar = None def progress(bytes_done, total_bytes): global pbar # Initialize the tqdm bar on the first callback with the total size if pbar is None: pbar = tqdm(total=total_bytes, unit='B', unit_scale=True) # Update the bar to reflect current progress pbar.update(bytes_done - pbar.n) # Start the download with the tqdm progress callback result = await client.download.url( url="https://example.com/file.zip", local_path=Path("./file.zip"), on_progress=progress, ) # Clean up the progress bar when done if pbar: pbar.close()

How do I retry failed downloads?

import asyncio async def download_with_retry(url, path, max_retries=3): for attempt in range(max_retries): # Attempt the download result = await client.download.url( url=url, local_path=path, ) # Return immediately on success if result.success: return result print(f"Attempt {attempt + 1} failed: {result.error}") # Wait with exponential backoff: 1s, 2s, 4s between retries await asyncio.sleep(2 ** attempt) return result # Return the last failed result after all retries exhausted

Error Handling

from cmdop.exceptions import ( CMDOPError, TimeoutError, FileTooLargeError, ) try: result = await client.download.url( url="https://example.com/file.zip", local_path=Path("./file.zip"), timeout=60, ) except TimeoutError: # Raised when the download exceeds the specified timeout print("Download timed out") except FileTooLargeError as e: # Raised when the file exceeds the maximum allowed size print(f"File too large: {e.size_bytes} > {e.max_bytes}") except CMDOPError as e: # Catch-all for any other CMDOP-related errors print(f"Download error: {e}")

How does this compare to the Files service?

OperationDownload ServiceFiles Service
SourceExternal URLRemote filesystem
Use caseDownload from internetTransfer from machine
ChunkingAutomaticAutomatic
AuthenticationURL-basedCMDOP auth

When should I use the Download service?

  • Download from external URLs through remote machine
  • Access files behind firewall/NAT
  • Use remote machine’s network identity

When should I use the Files service?

  • Transfer files from remote filesystem
  • Upload/download between local and remote
  • List and browse remote directories

What are the limitations?

  • Maximum chunk size: 100MB
  • Progress callback called per chunk
  • Requires remote agent to have network access to URL
Last updated on