Python script to tweet your Internet speed

Published October 13, 2022

This is a simple Python script that tweets your Internet speed. I wrote this script because my former Internet provider C* suddenly slowed down and had very slow speeds of 1Mbps; my plan was supposedly up to 100Mbps download/10Mbps upload. This is a variant of what I originally wrote.

Tweet your Internet speed

To begin, you first need a Twitter account and have Python 3.8+ installed. You will also have to install pip.

Create Twitter application

First, create a Twitter application. Go to https://apps.twitter.com/ and click on Create New App.

After creating the application, click on the tab that says Keys and Access Tokens. Generate the Consumer Key and Secret. Also, generate the Access Token and Token Secret. Do not give these values to anyone.

Click on the tab Permissions and set the access to Read and Write.

Download speedtest cli program

Download the speedtest.net command line script speedtest.py from GitHub. Save it in a directory of your choice. I copied mine to /usr/local/bin/speedtest.py.

Download my Python script

Copy my script internetspeed.py to a directory of your choice. I copied mine to /usr/local/bin/internetspeed.py.

import subprocess
import tweepy
import requests

#
# Python script to tweet your Internet speed
# Arul John { https://aruljohn.com }
#

def main():
  script_path = '/usr/local/bin/speedtest.py'
  # Find IP address and remove the last 2 octets
  ip = None
  r = requests.get('https://api.aruljohn.com/ip')
  if r.status_code == requests.codes.ok:
    ip = r.text
    if ip != None:
      ip =  '.'.join(ip.split('.')[:2]) + '.xx.xx'

  # Get the Internet speed by calling speedtest-cli
  speed = subprocess.check_output(['python', script_path, '--simple'])
  speed = "My #InternetSpeed :n" + speed
  if ip != None:
    speed += "IP address: " + ip

  # Twitter consumer key, secret, access token and secret
  consumer_key = ''
  consumer_secret = ''
  access_token = ''
  access_token_secret = ''
  auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_token_secret)
  api = tweepy.API(auth)

  # Set status on Twitter
  status = api.update_status(status=speed)

  # Display information for logging
  print(speed)

if __name__ == "__main__":
  main()

Edit internetspeed.py and replace the four values (consumer_key, consumer_secret, access_token, access_token_secret) with the values in the Twitter application you just created. Also, replace the script_path value with the full path to the speedtest.py script.

Install Python modules requests and tweepy

You may or may not have the Python modules requests and tweepy. Let's install them.

pip install requests
pip install tweepy

Run the script

Now that the dependencies are installed, run the script:

python speedtest.py

Set as cronjob to run regularly

To set a cronjob to run this script and tweet your Internet speed every 3 hours, you can use this line:

# Run every 3 hours
5 0-23/3 * * * /usr/bin/python /usr/local/bin/internetspeed.py >> /tmp/speedtest.log

This is what your tweet will look like. You can modify the script internetspeed.py accordingly to your style.

Please let me know if you run into any issues. Thanks for reading this post.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: October 13, 2022.     This post was originally written on January 05, 2017.