How To: Make a Change-of-IP Notifier in Python

Make a Change-of-IP Notifier in Python

In this article I'll show you how to make a simple IP address notifier. The program will text you your new IP address, in the event that it changes. For those of you with dynamic IPs, this is very useful. I'm constantly frustrated when my IP changes, and it's handy to be notified via text when it happens. To use the program, you'll need Python 2.7 or later, urllib2, and a program called "text" (see this article here to get it). 

The Code

from urllib2 import urlopen

import time

import text

#imports all required modules

loop=1

#this is the loop function that allows the code to be re-run every 24 hours

ip=urlopen('http://www.whatismyip.org/').readlines()[0]

#this is the initial ip, the one you have right now

while loop==1:

    currentIP=''

    url = urlopen('http://www.whatismyip.org/')

#starts loop, creates currentIP  and url variables

    currentIP=url.readlines()[0]

#sets your current ip to currentIP variable

    if (ip!=currentIP):

#if the original ip doesn't equal the currentIP...

        text.text('YOUR.PHONE', currentIP)

#text you the new ip address

        ip=urlopen('http://www.whatismyip.org/').readlines()[0]

#and reset the original ip to the current one

    time.sleep(86400)

#time to sleep until next check

Note: DO NOT change the loop time! "WhatIsMyIP.org" doesn't like bots pinging their server multiple times. The loop time is designed to simulate a real person, and not overload the server with redundant requests. 

Uses and Other Applications 

The program itself is very simple; checks for changes in IP and lets you know via text. But, this program could also check any website for information you'd like, and text it to you. Simply change the URL and which lines the program is identifying and extracting. Of course, ensure the site you are using has no restrictions against bots pinging their servers. If you have any other ideas/suggestions, please comment! :) 

If you're interested in other articles related to Python, check out Python Gmail Notifier and Encrypting Text in Python.

Just updated your iPhone to iOS 18? You'll find a ton of hot new features for some of your most-used Apple apps. Dive in and see for yourself:

Photos by hackinghome, MJ/TR

3 Comments

" DO NOT change the loop time! "WhatIsMyIP.org" doesn't like bots pinging their server multiple times. The loop time is designed to simulate a real person, and not overload the server with redundant requests. " Didn't know that tidbit. Thanks a lot! :D

yeah, they actually throw it in as a comment in their source code....
""Please don't program a bot to use this site to grab your IPs. It kills my server and thats not nice.
Just get some cheap or free web hosting and make your own IP-only page to power your bot. Then you won't even have to parse any html, just load the IP directly - better for everyone!""

Of course, I took into account the average check time of an actual human, and am not doing any damage checking every 24 hours :) unless your IP changes every hour, it shouldnt be a problem :)

Share Your Thoughts

  • Hot
  • Latest