0
# Results Management
1
2
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.
3
4
```python
5
import speedtest
6
```
7
8
## Capabilities
9
10
### SpeedtestResults Class
11
12
Container for speed test results with extensive export and formatting capabilities.
13
14
```python { .api }
15
class SpeedtestResults:
16
def __init__(self, download=0, upload=0, ping=0, server=None, client=None, opener=None, secure=False):
17
"""
18
Initialize SpeedtestResults instance.
19
20
Parameters:
21
- download (float): Download speed in bits per second (default: 0)
22
- upload (float): Upload speed in bits per second (default: 0)
23
- ping (float): Ping latency in milliseconds (default: 0)
24
- server (dict, optional): Server information dictionary
25
- client (dict, optional): Client information dictionary
26
- opener (urllib2.OpenerDirector, optional): URL opener for sharing
27
- secure (bool): Whether HTTPS was used (default: False)
28
"""
29
```
30
31
**Attributes:**
32
- `download` (float): Download speed in bits per second
33
- `upload` (float): Upload speed in bits per second
34
- `ping` (float): Ping latency in milliseconds
35
- `server` (dict): Server information (id, host, name, country, sponsor, etc.)
36
- `client` (dict): Client information (ip, lat, lon, isp, etc.)
37
- `timestamp` (str): ISO format timestamp of test completion
38
- `bytes_received` (int): Total bytes received during download test
39
- `bytes_sent` (int): Total bytes sent during upload test
40
41
### Data Export
42
43
Export results in various structured formats for integration and analysis.
44
45
```python { .api }
46
def dict(self):
47
"""
48
Return results as dictionary.
49
50
Returns:
51
dict: Complete results with download, upload, ping, server, client, and timestamp
52
"""
53
54
def json(self, pretty=False):
55
"""
56
Return results in JSON format.
57
58
Parameters:
59
- pretty (bool): Pretty-print JSON with indentation (default: False)
60
61
Returns:
62
str: JSON-formatted results string
63
"""
64
65
def csv(self, delimiter=','):
66
"""
67
Return results in CSV format.
68
69
Parameters:
70
- delimiter (str): CSV field delimiter (default: ',')
71
72
Returns:
73
str: CSV-formatted results string
74
"""
75
76
@staticmethod
77
def csv_header(delimiter=','):
78
"""
79
Return CSV header row.
80
81
Parameters:
82
- delimiter (str): CSV field delimiter (default: ',')
83
84
Returns:
85
str: CSV header string
86
"""
87
```
88
89
Usage examples:
90
```python
91
s = speedtest.Speedtest()
92
s.get_best_server()
93
s.download()
94
s.upload()
95
96
results = s.results
97
98
# Export as dictionary
99
data = results.dict()
100
print(f"Test completed at: {data['timestamp']}")
101
print(f"Server: {data['server']['sponsor']} in {data['server']['name']}")
102
103
# Export as pretty JSON
104
json_results = results.json(pretty=True)
105
print(json_results)
106
107
# Export as CSV
108
csv_data = results.csv()
109
print(SpeedtestResults.csv_header())
110
print(csv_data)
111
```
112
113
### Result Sharing
114
115
Share results to speedtest.net and get shareable URLs.
116
117
```python { .api }
118
def share(self):
119
"""
120
Submit results to speedtest.net for sharing.
121
122
Returns:
123
str: URL to shared results image
124
125
Raises:
126
ShareResultsConnectFailure: Failed to connect to share API
127
ShareResultsSubmitFailure: Failed to submit results for sharing
128
"""
129
```
130
131
Usage example:
132
```python
133
s = speedtest.Speedtest()
134
s.get_best_server()
135
s.download()
136
s.upload()
137
138
# Share results and get URL
139
try:
140
share_url = s.results.share()
141
print(f"Results shared: {share_url}")
142
except speedtest.ShareResultsConnectFailure:
143
print("Failed to connect to sharing service")
144
except speedtest.ShareResultsSubmitFailure:
145
print("Failed to submit results for sharing")
146
```
147
148
### String Representation
149
150
```python { .api }
151
def __repr__(self):
152
"""
153
String representation of results.
154
155
Returns:
156
str: Formatted string with download, upload, and ping information
157
"""
158
```
159
160
Usage example:
161
```python
162
s = speedtest.Speedtest()
163
s.get_best_server()
164
s.download()
165
s.upload()
166
167
# Print results summary
168
print(s.results) # Uses __repr__ method
169
```
170
171
## Data Structures
172
173
### Result Dictionary Structure
174
175
```python
176
{
177
"download": 85436474.26, # bits per second
178
"upload": 9774343.65, # bits per second
179
"ping": 15.474, # milliseconds
180
"server": {
181
"id": "4954",
182
"host": "speedtest.example.com:8080",
183
"name": "City, State",
184
"country": "Country",
185
"sponsor": "ISP Name",
186
"lat": "40.7128",
187
"lon": "-74.0060",
188
"distance": 25.3
189
},
190
"client": {
191
"ip": "192.168.1.100",
192
"lat": "40.7589",
193
"lon": "-73.9851",
194
"isp": "Internet Service Provider",
195
"isprating": "3.7",
196
"rating": "0",
197
"ispdlavg": "0",
198
"ispulavg": "0"
199
},
200
"timestamp": "2023-10-01T14:30:45.123456Z",
201
"bytes_sent": 31457280,
202
"bytes_received": 125829120
203
}
204
```
205
206
### CSV Format
207
208
The CSV export includes the following fields in order:
209
- Server ID
210
- Sponsor
211
- Server Name
212
- Timestamp (ISO format)
213
- Distance (km)
214
- Ping (ms)
215
- Download (bits/s)
216
- Upload (bits/s)
217
- Share URL (if available)
218
- Client IP