Advertisement
Jenkins1337

speedtest

Dec 4th, 2021 (edited)
7
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Importing necessary library
  2. import speedtest
  3. import time
  4. import datetime
  5. import msvcrt
  6. import csv
  7. import os
  8.  
  9.  
  10. fn_ts = time.time()
  11. fn_st = datetime.datetime.fromtimestamp(fn_ts).strftime('%Y-%m-%d_%H-%M-%S')
  12. print(fn_st)
  13.  
  14. #path and filename
  15. desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
  16. file_name = desktop + '\\' + fn_st + '_speedtest.csv'
  17. print(file_name)
  18.  
  19. #create csv file
  20. header = ['timestamp', 'download', 'upload', 'ping', 'server', 'country']
  21. with open(file_name, 'w', encoding='UTF8', newline='') as f:
  22.     writer = csv.writer(f)
  23.  
  24.     # write the header
  25.     writer.writerow(header)
  26.  
  27. print(f'file created: {file_name}')
  28.  
  29. #start loop to continiously write speed tests into a text file
  30. i = 0
  31. while True:
  32.     i = i + 1
  33.  
  34.     #start speed test
  35.     test = speedtest.Speedtest()
  36.  
  37.     print("Loading server List...")
  38.     test.get_servers()
  39.  
  40.     print("Choosing best server...")
  41.     best    = test.get_best_server()
  42.     host    = best['host']
  43.     country = best['country']
  44.     print(f"Found: {host} located in {country}")
  45.  
  46.     print("Performing download test...")
  47.     download_result = test.download()
  48.  
  49.     print("Performing upload test...")
  50.     upload_result = test.upload()
  51.  
  52.     print("Performing ping test...")
  53.     ping_result = test.results.ping
  54.  
  55.     #creat strings and convert into mbit/s
  56.     mbits_download  = round(download_result/1024/1024,2)
  57.     mbits_upload    = round(upload_result/1024/1024,2)
  58.     ms_ping         = round(ping_result,2)
  59.  
  60.  
  61.     string_download = f'Download speed: {mbits_download} Mbit/s'
  62.     string_upload   = f'Upload speed: {mbits_upload} Mbit/s'
  63.     string_ping     = f'Ping: {ping_result} ms'
  64.  
  65.     print(string_download)
  66.     print(string_upload)
  67.     print(string_ping)
  68.     #speed test is finished
  69.  
  70.     ts = time.time()
  71.     st = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S')
  72.  
  73.     #append data to csv file
  74.     data = [st, mbits_download, mbits_upload, ms_ping, host, country]
  75.     with open(file_name, 'a', encoding='UTF8', newline='') as f:
  76.         writer = csv.writer(f)
  77.  
  78.         # write the data
  79.         writer.writerow(data)
  80.  
  81.     # exit script with "enter"
  82.     print('-------------\n')
  83.     if msvcrt.kbhit():
  84.         if msvcrt.getwche() == '\r':
  85.             break
  86.     time.sleep(5)
  87. print(i)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement