CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-speedtest-cli

Command line interface for testing internet bandwidth using speedtest.net

Pending
Overview
Eval results
Files

cli-interface.mddocs/

CLI Interface

Command-line interface providing direct access to speed testing functionality with extensive options for output formatting, server selection, and test configuration. Available through both speedtest-cli and speedtest commands.

import speedtest

Capabilities

Main Entry Points

Primary CLI functions that handle command-line execution and argument processing.

def main():
    """
    Main entry point for CLI commands.
    Called by both 'speedtest' and 'speedtest-cli' console scripts.
    """

def shell():
    """
    Main CLI logic and argument processing.
    Handles all command-line options and executes appropriate tests.
    """

Argument Processing

Parse and validate command-line arguments with comprehensive option support.

def parse_args():
    """
    Parse command-line arguments.
    
    Returns:
    argparse.Namespace: Parsed arguments with all CLI options
    """

def validate_optional_args(args):
    """
    Validate parsed command-line arguments.
    
    Parameters:
    - args (argparse.Namespace): Parsed arguments to validate
    
    Raises:
    SpeedtestCLIError: Invalid argument combinations or values
    """

Output Functions

Specialized output functions for CLI formatting and display.

def printer(string, quiet=False, debug=False, error=False, **kwargs):
    """
    Print output with formatting control.
    
    Parameters:
    - string (str): Text to print
    - quiet (bool): Suppress output if True (default: False)
    - debug (bool): Only print if DEBUG is True (default: False)
    - error (bool): Print to stderr (default: False)
    - **kwargs: Additional print() arguments
    """

def csv_header(delimiter=','):
    """
    Print CSV header row for CLI output.
    
    Parameters:
    - delimiter (str): CSV field delimiter (default: ',')
    """

def version():
    """Print version information."""

Signal Handling

def ctrl_c(shutdown_event):
    """
    Handle Ctrl+C signal during test execution.
    
    Parameters:
    - shutdown_event (threading.Event): Event to signal shutdown
    """

Command-Line Usage

Basic Usage

# Run full speed test
speedtest-cli

# Alternative command
speedtest

Common Options

# Test download only
speedtest-cli --no-upload

# Test upload only  
speedtest-cli --no-download

# Use single connection (simulates file transfer)
speedtest-cli --single

# Show results in bytes instead of bits
speedtest-cli --bytes

# Generate shareable results URL
speedtest-cli --share

Output Formats

# Simple output (less verbose)
speedtest-cli --simple

# JSON output
speedtest-cli --json

# CSV output
speedtest-cli --csv

# CSV with custom delimiter
speedtest-cli --csv --csv-delimiter ';'

# CSV with header row
speedtest-cli --csv --csv-header

Server Selection

# List available servers
speedtest-cli --list

# Use specific server by ID
speedtest-cli --server 4954

# Exclude specific servers
speedtest-cli --exclude 1234,5678

# Use Mini server
speedtest-cli --mini http://speedtest.example.com/mini

Connection Options

# Set timeout (default: 10 seconds)
speedtest-cli --timeout 30

# Use HTTPS connections
speedtest-cli --secure

# Bind to specific source address
speedtest-cli --source 192.168.1.100

# Skip upload pre-allocation (uses less memory)
speedtest-cli --no-pre-allocate

Help and Version

# Show help
speedtest-cli --help

# Show version
speedtest-cli --version

CLI Output Examples

Default Output

Retrieving speedtest.net configuration...
Testing from Example ISP (192.168.1.100)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Example Host (City, State) [25.30 km]: 15.474 ms
Testing download speed................................................................................
Download: 85.44 Mbit/s
Testing upload speed....................................................................................................
Upload: 9.77 Mbit/s

Simple Output

Ping: 15.474 ms
Download: 85.44 Mbit/s
Upload: 9.77 Mbit/s

JSON Output

{
    "download": 85436474.26,
    "upload": 9774343.65,
    "ping": 15.474,
    "server": {
        "id": "4954",
        "host": "speedtest.example.com:8080",
        "name": "City, State",
        "country": "Country",
        "sponsor": "Example Host",
        "lat": "40.7128",
        "lon": "-74.0060",
        "distance": 25.30
    },
    "timestamp": "2023-10-01T14:30:45.123456Z",
    "bytes_sent": 31457280,
    "bytes_received": 125829120,
    "share": null
}

CSV Output

Server ID,Sponsor,Server Name,Timestamp,Distance,Ping,Download,Upload,Share,IP Address
4954,Example Host,"City, State",2023-10-01T14:30:45.123456Z,25.30,15.474,85436474.26,9774343.65,,192.168.1.100

CLI Integration Examples

Shell Scripting

#!/bin/bash
# Basic speed test with error handling
if speedtest-cli --simple > /tmp/speedtest.log 2>&1; then
    echo "Speed test completed successfully"
    cat /tmp/speedtest.log
else
    echo "Speed test failed"
    exit 1
fi

JSON Processing

#!/bin/bash
# Extract specific values from JSON output
RESULT=$(speedtest-cli --json)
DOWNLOAD=$(echo "$RESULT" | python -c "import sys, json; print(json.load(sys.stdin)['download'])")
UPLOAD=$(echo "$RESULT" | python -c "import sys, json; print(json.load(sys.stdin)['upload'])")

echo "Download: $(echo "scale=2; $DOWNLOAD / 1000000" | bc) Mbps"
echo "Upload: $(echo "scale=2; $UPLOAD / 1000000" | bc) Mbps"

Monitoring Script

#!/bin/bash
# Regular speed test monitoring
LOG_FILE="/var/log/speedtest.csv"

# Add header if file doesn't exist
if [ ! -f "$LOG_FILE" ]; then
    speedtest-cli --csv-header > "$LOG_FILE"
fi

# Append test results
speedtest-cli --csv >> "$LOG_FILE"

Automated Testing

#!/bin/bash
# Test with specific server and timeout
SERVER_ID="4954"
TIMEOUT="30"

speedtest-cli \
    --server "$SERVER_ID" \
    --timeout "$TIMEOUT" \
    --json \
    --secure > "speedtest_$(date +%Y%m%d_%H%M%S).json"

Install with Tessl CLI

npx tessl i tessl/pypi-speedtest-cli

docs

cli-interface.md

exception-handling.md

index.md

results-management.md

speed-testing.md

tile.json