1

I'm trying to do speed testing using python getting data from speedtest.net. I have been looking through GitHub 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.

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()
8
  • Try for x in range(0,3) Commented Jan 16, 2018 at 21:01
  • or just for x in range(3). range(start, end, step) - start inclusively and end exclusively. check the docs Commented Jan 16, 2018 at 21:09
  • I have already tried that, the problem is that it doesnt loop at all. It outputs the result once and thats it. Commented Jan 16, 2018 at 21:10
  • we can't help if we don't know what is going on... in your question you only said make a simple script that will run 3 times. At least show us what kind of error you've got if range is not real problem Commented Jan 16, 2018 at 21:13
  • 1
    I think the issue is that you are looking at the results_dict value which gets reset in each loop. You have to either add the result to a variable outside the for loop scope or print off the results each loop like @Aiven answer below. Commented Jan 16, 2018 at 21:32

4 Answers 4

14
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()
Sign up to request clarification or add additional context in comments.

6 Comments

Ok now it works, but it's giving me a lot of extra info. I just wanted the download, upload, and ping lol. How can I fix that.
just pick needed info from results_dict: results_dict['download']
holy cow dude, thanks. How did you know what to print? instead of all the extra stuff.
how can I turn it into a txt/csv file i tried using open() and write etc but none worked
holy molly @aiven, thank you so much that's brilliant. Thank you so much for your help
|
4

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())

Comments

2

There is a way to use the library much easier Pyspeedtest

import pyspeedtest
st = pyspeedtest.SpeedTest()
st.ping()

https://pypi.org/project/pyspeedtest/

Comments

2

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) ))

1 Comment

Please don't add "thank you" as an answer. Instead, vote up the answers that you find helpful. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.