How do I get home? Reliably getting the external IP in python

I recently had some help setting up an IP camera in my apartment from some friends and we collectively discovered that DynDNS's static IP service was no longer free. As a cheap student, that was terrible news. Once everything was set up (some port forwarding and a free version install of VitaminD) I went ahead and got down to writing up a barebones python script that would make sure I never lost track of the camera while away. The first step was figuring out what my external IP was at any time, which proved to be a little more roundabout than I'd hoped. Services like whatismyip.org tend to either reject bots or present the result as an image, ostensibly because they don't want to become an API for the exact task I had in mind. It hit me that I have a remote host that can identify a client, this site. So I whipped up a very simple one-line PHP file:

<?PHP print $_SERVER['REMOTE_ADDR'] ; ?>

I tossed this on my server and had python grab it. Sure enough, it contained exactly my external IP address. The next step was to get that information back out to myself. I would say this is where it gets hackity, but I'm 90% sure there's a much better way to do the above as well. I had python build an html file with a redirect and push it up via FTP. The code then checks the external IP once an hour, if it changes it updates the redirect page. I'll stop shy of pointing out where I've stored the files, but the camera itself is password protected.

This was a very short term fix, as I've got a trip in the very near future

One thought on “How do I get home? Reliably getting the external IP in python”

Leave a Reply