Guest User

Untitled

a guest
Feb 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #!/usr/bin/python
  2. #stolen and modified from the reddit post about the raspbeery pi tweeting at comcast
  3. #run this every 10 minutes (or w/e) with cron:
  4. #"crontab -e"
  5. #*/10 * * * * /home/pi/lolbandwidth.py
  6. import os
  7. import sys
  8. import csv
  9. import datetime
  10. import time
  11.  
  12. def test():
  13. #run speedtest-cli
  14. print 'running test'
  15. #install with "pip install speedtest-cli"
  16. csvlog = '/home/pi/speed_data.csv'
  17. a = os.popen("speedtest-cli --simple").read()
  18. print 'ran'
  19. #split the 3 line result (ping,down,up)
  20. lines = a.split('\n')
  21. print a
  22. ts = time.time()
  23. date =datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  24. #if speedtest could not connect set the speeds to 0
  25. if "Cannot" in a:
  26. p = 100
  27. d = 0
  28. u = 0
  29. #extract the values for ping down and up values
  30. else:
  31. p = lines[0][6:11]
  32. d = lines[1][10:14]
  33. u = lines[2][8:12]
  34. print date,p, d, u
  35. #save the data to file for local network plotting
  36. out_file = open(csvlog, 'a')
  37. writer = csv.writer(out_file)
  38. writer.writerow((ts*1000,p,d,u))
  39. out_file.close()
  40. return
  41.  
  42. if __name__ == '__main__':
  43. test()
  44. print 'completed'
Add Comment
Please, Sign In to add comment