速度测试 python 脚本

新手上路,请多包涵

你好,我是 python 的新手,我正在尝试使用 python 从 (speedtest.net) 获取数据进行速度测试。我一直在查看 git hub 并找到了 speedtest-cli。但是它有很多我不需要的功能。我只想制作一个将运行 3 次的简单脚本。我找到了一些 API,但我不确定如何将其修改为循环三次。任何帮助将不胜感激。提前致谢

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

原文由 MrBiscuit 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 381
1 个回答
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()

原文由 aiven 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题