Command line interface for testing internet bandwidth using speedtest.net
—
Comprehensive exception hierarchy for handling various error conditions during speed testing operations. All speedtest-cli exceptions inherit from the base SpeedtestException class, providing structured error handling for different failure scenarios.
import speedtestAll speedtest-cli specific exceptions inherit from this base class.
class SpeedtestException(Exception):
"""Base exception class for all speedtest-cli errors."""Exceptions related to HTTP requests and network connectivity issues.
class SpeedtestHTTPError(SpeedtestException):
"""HTTP request errors during speed testing operations."""
class SpeedtestConfigError(SpeedtestException):
"""Configuration retrieval errors."""
class ConfigRetrievalError(SpeedtestHTTPError):
"""Failed to retrieve speedtest.net configuration."""
class SpeedtestServersError(SpeedtestException):
"""Server list retrieval errors."""
class ServersRetrievalError(SpeedtestHTTPError):
"""Failed to retrieve server list from speedtest.net."""Exceptions related to server discovery and selection processes.
class InvalidServerIDType(SpeedtestException):
"""Invalid server ID type provided."""
class NoMatchedServers(SpeedtestException):
"""No servers match the provided criteria."""
class SpeedtestBestServerFailure(SpeedtestException):
"""Failed to determine best server through latency testing."""
class SpeedtestMissingBestServer(SpeedtestException):
"""Best server not set - call get_best_server() first."""Exceptions specific to speedtest Mini server functionality.
class SpeedtestMiniConnectFailure(SpeedtestException):
"""Failed to connect to specified Mini server."""
class InvalidSpeedtestMiniServer(SpeedtestException):
"""Invalid Mini server URL provided."""Exceptions that occur during speed test execution.
class SpeedtestUploadTimeout(SpeedtestException):
"""Upload test exceeded timeout duration."""Exceptions related to sharing test results on speedtest.net.
class ShareResultsConnectFailure(SpeedtestException):
"""Failed to connect to speedtest.net share API."""
class ShareResultsSubmitFailure(SpeedtestException):
"""Failed to submit results to speedtest.net for sharing."""Exceptions specific to command-line interface usage.
class SpeedtestCLIError(SpeedtestException):
"""CLI-specific errors and invalid argument combinations."""Handle common speedtest errors gracefully:
import speedtest
try:
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
print(s.results.json(pretty=True))
except speedtest.ConfigRetrievalError:
print("Failed to retrieve speedtest configuration")
except speedtest.ServersRetrievalError:
print("Failed to retrieve server list")
except speedtest.NoMatchedServers:
print("No servers available for testing")
except speedtest.SpeedtestBestServerFailure:
print("Could not determine best server")
except speedtest.SpeedtestException as e:
print(f"Speed test failed: {e}")Handle server-related errors with fallback options:
import speedtest
s = speedtest.Speedtest()
try:
# Try to get servers
s.get_servers()
closest = s.get_closest_servers(limit=10)
# Try to find best server
best = s.get_best_server()
print(f"Selected server: {best['sponsor']}")
except speedtest.NoMatchedServers:
print("No servers match criteria, trying with no filters")
s.get_servers([]) # Get all servers
best = s.get_best_server()
except speedtest.SpeedtestBestServerFailure:
print("Auto-selection failed, using first available server")
s.best = s.closest[0] # Manually set first serverHandle errors during speed tests with retries:
import speedtest
import time
s = speedtest.Speedtest()
s.get_best_server()
# Download test with retry
max_retries = 3
for attempt in range(max_retries):
try:
download_speed = s.download()
break
except speedtest.SpeedtestHTTPError as e:
if attempt < max_retries - 1:
print(f"Download failed (attempt {attempt + 1}): {e}")
time.sleep(2) # Wait before retry
else:
print("Download test failed after all retries")
raise
# Upload test with timeout handling
try:
upload_speed = s.upload()
except speedtest.SpeedtestUploadTimeout:
print("Upload test timed out - may indicate slow upload speed")
# Could retry with different parameters
upload_speed = s.upload(pre_allocate=False) # Use less memoryHandle sharing failures gracefully:
import speedtest
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
# Try to share results
try:
share_url = s.results.share()
print(f"Results shared: {share_url}")
except speedtest.ShareResultsConnectFailure:
print("Could not connect to sharing service")
except speedtest.ShareResultsSubmitFailure:
print("Failed to submit results for sharing")
except speedtest.SpeedtestHTTPError:
print("HTTP error occurred during sharing")
# Results are still available even if sharing fails
print("Local results:")
print(s.results.json(pretty=True))Handle Mini server configuration errors:
import speedtest
s = speedtest.Speedtest()
mini_url = "http://speedtest.example.com/mini"
try:
s.set_mini_server(mini_url)
s.download()
s.upload()
except speedtest.InvalidSpeedtestMiniServer:
print(f"Invalid Mini server URL: {mini_url}")
except speedtest.SpeedtestMiniConnectFailure:
print(f"Could not connect to Mini server: {mini_url}")
# Fall back to regular speedtest.net servers
s.get_best_server()
s.download()
s.upload()Install with Tessl CLI
npx tessl i tessl/pypi-speedtest-cli