title: What is a low impact way of checking if the internet is really slow in python?
body: <p>I have a python script which I run as a background process on my mac every 10 minutes. Basically, it downloads the latest image from a server and depending on my internet speed, it downloads a hi-res (20mb on 5Mb/s connections or better) or low-res (6mb on 5 to 1 Mb/s connections) version of the image. </p>
  
  <p>Thus in the beginning of my script, I am using the python package <code>speedtest-cli</code> to test my internet speed. However, inherent in any speed test is the use of some of my bandwidth.</p>
  
  <p>If possible, what I would like to do before the speed test is some simple and very low bandwidth test just to see if my internet connection is at some baseline level before I do my speed test. That baseline level can be measured in download speed, ping time, or any useful metric that can inform me of the basic quality of my connection. Thus if my internet is too slow, I quit before using up any of the limited bandwidth with the speed test. </p>
  
  <p>Accuracy is not that important here. I'm not concerned with the difference between slow and really slow internet. After the speed test has been run, if the download speed is not at least 1 Mb/s, it exits. Thus this baseline test can be any simple test where baseline is somewhere below 1 Mb/s download speed.</p>
  
  <p>Using ping could be a reasonable solution. Another <a href="https://stackoverflow.com/questions/5050405/how-to-check-network-bandwidth-with-python">question</a> provides a solution for pinging which is provided in this <a href="https://gist.github.com/zed/255009" rel="nofollow noreferrer">gist</a> but that is rather elaborate, and requires root to run, which I would rather avoid if possible.</p>
  
  <p>Below is a simple version of the script I am using:</p>
  
  <pre><code>import requests
  import sys
  import os
  import logging
  import socket
  import json
  
  # python himawari.py
  # stolen from https://gist.github.com/celoyd/39c53f824daef7d363db
  # requires speedtest-cli ('pip install speedtest-cli')
  
  # check if we have internet
  def internet(host="8.8.8.8", port=53, timeout=3):
      try:
          socket.setdefaulttimeout(timeout)
          socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
          return True
      except Exception as ex:
          return False
  
  print("Checking internet speed:")
  
  if internet():
      print "Internet connection exists..."
      os.system("rm -f /Users/scott/git-projects/live-earth-desktop/speedtest.json")
      os.system("speedtest-cli --json &gt;&gt; /Users/scott/git-projects/live-earth-desktop/speedtest.json")
  else:
      print "No internet connection. Quitting..."
      os._exit(1)
  
  with open('/Users/scott/git-projects/live-earth-desktop/speedtest.json') as data_file:    
      try:
          data = json.load(data_file)
      except ValueError:
          print("data was not valid JSON")
          os._exit(1)
  
  
  speed = data["download"]
  
  print_speed = str(round(speed//1000000))
  print("Download speed: ~" + print_speed + " Mb/s")
  
  if (speed &gt; 5000000): # 5 Mb/s
      print("Internet speed is good. Downloading hi-res image.")
      # Download hi-res image here
  elif (speed &gt; 1000000): # 1 Mb/s
      print("Internet speed is ok. Downloading low-res image.")
      # Download low-res image here
  else:
      print("Internet speed is poor. Quitting.")
      os._exit(1)
  </code></pre>