-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedtest.py
More file actions
41 lines (36 loc) · 1.28 KB
/
speedtest.py
File metadata and controls
41 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import speedtest
# https://stackoverflow.com/questions/48289636/speedtest-python-script
def test():
s = speedtest.Speedtest()
s.get_servers()
s.get_best_server()
s.download()
s.upload()
res = s.results.dict()
return res["download"], res["upload"], res["ping"]
def main():
# write to csv
with open('file.csv', 'w') as f:
f.write('download,upload,ping\n')
for i in range(3):
print('Making test #{}'.format(i+1))
d, u, p = test()
f.write('{},{},{}\n'.format(d, u, p))
# pretty write to txt file
with open('file.txt', 'w') as f:
for i in range(3):
print('Making test #{}'.format(i+1))
d, u, p = test()
f.write('Test #{}\n'.format(i+1))
f.write('Download: {:.2f} Kb/s\n'.format(d / 1024))
f.write('Upload: {:.2f} Kb/s\n'.format(u / 1024))
f.write('Ping: {}\n'.format(p))
# simply print in needed format if you want to use pipe-style: python script.py > file
for i in range(3):
d, u, p = test()
print('Test #{}\n'.format(i+1))
print('Download: {:.2f} Kb/s\n'.format(d / 1024))
print('Upload: {:.2f} Kb/s\n'.format(u / 1024))
print('Ping: {}\n'.format(p))
if __name__ == '__main__':
main()