Command line interface for testing internet bandwidth using speedtest.net
—
Comprehensive results handling with multiple export formats and sharing capabilities through the SpeedtestResults class. Provides structured access to test data with built-in formatting and sharing options.
import speedtestContainer for speed test results with extensive export and formatting capabilities.
class SpeedtestResults:
def __init__(self, download=0, upload=0, ping=0, server=None, client=None, opener=None, secure=False):
"""
Initialize SpeedtestResults instance.
Parameters:
- download (float): Download speed in bits per second (default: 0)
- upload (float): Upload speed in bits per second (default: 0)
- ping (float): Ping latency in milliseconds (default: 0)
- server (dict, optional): Server information dictionary
- client (dict, optional): Client information dictionary
- opener (urllib2.OpenerDirector, optional): URL opener for sharing
- secure (bool): Whether HTTPS was used (default: False)
"""Attributes:
download (float): Download speed in bits per secondupload (float): Upload speed in bits per secondping (float): Ping latency in millisecondsserver (dict): Server information (id, host, name, country, sponsor, etc.)client (dict): Client information (ip, lat, lon, isp, etc.)timestamp (str): ISO format timestamp of test completionbytes_received (int): Total bytes received during download testbytes_sent (int): Total bytes sent during upload testExport results in various structured formats for integration and analysis.
def dict(self):
"""
Return results as dictionary.
Returns:
dict: Complete results with download, upload, ping, server, client, and timestamp
"""
def json(self, pretty=False):
"""
Return results in JSON format.
Parameters:
- pretty (bool): Pretty-print JSON with indentation (default: False)
Returns:
str: JSON-formatted results string
"""
def csv(self, delimiter=','):
"""
Return results in CSV format.
Parameters:
- delimiter (str): CSV field delimiter (default: ',')
Returns:
str: CSV-formatted results string
"""
@staticmethod
def csv_header(delimiter=','):
"""
Return CSV header row.
Parameters:
- delimiter (str): CSV field delimiter (default: ',')
Returns:
str: CSV header string
"""Usage examples:
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
results = s.results
# Export as dictionary
data = results.dict()
print(f"Test completed at: {data['timestamp']}")
print(f"Server: {data['server']['sponsor']} in {data['server']['name']}")
# Export as pretty JSON
json_results = results.json(pretty=True)
print(json_results)
# Export as CSV
csv_data = results.csv()
print(SpeedtestResults.csv_header())
print(csv_data)Share results to speedtest.net and get shareable URLs.
def share(self):
"""
Submit results to speedtest.net for sharing.
Returns:
str: URL to shared results image
Raises:
ShareResultsConnectFailure: Failed to connect to share API
ShareResultsSubmitFailure: Failed to submit results for sharing
"""Usage example:
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
# Share results and get URL
try:
share_url = s.results.share()
print(f"Results shared: {share_url}")
except speedtest.ShareResultsConnectFailure:
print("Failed to connect to sharing service")
except speedtest.ShareResultsSubmitFailure:
print("Failed to submit results for sharing")def __repr__(self):
"""
String representation of results.
Returns:
str: Formatted string with download, upload, and ping information
"""Usage example:
s = speedtest.Speedtest()
s.get_best_server()
s.download()
s.upload()
# Print results summary
print(s.results) # Uses __repr__ method{
"download": 85436474.26, # bits per second
"upload": 9774343.65, # bits per second
"ping": 15.474, # milliseconds
"server": {
"id": "4954",
"host": "speedtest.example.com:8080",
"name": "City, State",
"country": "Country",
"sponsor": "ISP Name",
"lat": "40.7128",
"lon": "-74.0060",
"distance": 25.3
},
"client": {
"ip": "192.168.1.100",
"lat": "40.7589",
"lon": "-73.9851",
"isp": "Internet Service Provider",
"isprating": "3.7",
"rating": "0",
"ispdlavg": "0",
"ispulavg": "0"
},
"timestamp": "2023-10-01T14:30:45.123456Z",
"bytes_sent": 31457280,
"bytes_received": 125829120
}The CSV export includes the following fields in order:
Install with Tessl CLI
npx tessl i tessl/pypi-speedtest-cli