Reputation: 51
Hello there i'm new to python and I'm trying to do speed testing using python getting data from (speedtest.net). I have been looking through git hub and found the speedtest-cli. But it has a lot of features I don't need. I just want to make a simple script that will run 3 times. I found some API but I'm not sure how to modify it to loop three times. Any help will be appreciated. Thanks in advance
import speedtest
servers = []
# If you want to test against a specific server
# servers = [1234]
x=0
for x in range(0, 2):
s = speedtest.Speedtest()
s.get_servers(servers)
s.get_best_server()
s.download()
s.upload()
s.results.share()
results_dict = s.results.dict()
Upvotes: 3
Views: 46985
Reputation: 575
Hare Krishna 🙏
Try, this is best way to fetch net speed. It works in both Android and Windows.
import speedtest
st = speedtest.Speedtest()
while True:
download_speed = st.download()
print('Download Speed: {:5.2f} Mb'.format(download_speed/(1024*1024) ))
Upvotes: 2
Reputation: 845
we can use this way
import speedtest
def getNetSpeed():
speedTestHelper = speedtest.Speedtest()
speedTestHelper.get_best_server()
#Check download speed
speedTestHelper.download()
#Check upload speed
speedTestHelper.upload()
#generate shareable image
speedTestHelper.results.share()
#fetch result
return speedTestHelper.results.dict()
for i in range(3):
print(getNetSpeed())
Upvotes: 4
Reputation: 21
There is a way to use the library much easier Pyspeedtest
import pyspeedtest
st = pyspeedtest.SpeedTest()
st.ping()
https://pypi.org/project/pyspeedtest/
Upvotes: 2
Reputation: 4313
import speedtest
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()
Upvotes: 14