Python – Do Yo Thang

Kapow, got something up and running in about 30 minutes.

I wanted an easy command line tool to tell me where in the world an IP address was coming from.  There are plenty of online tools available that will tell you just that, but I wanted a command line tool I could use offline.

ipaddress

Python Or Ruby

I decided to use either Ruby or Python, but never having used Python before, I decided to give it a shot.

Windows, Linux, Mac, Ubuntu, What…

I need to say that you can easily do this on a non-Windows platform or even on a Windows platform using Cygwin, but let’s go the more involved route.

Steps

1. Go here and install Python version 2.7 for Windows.  If like me, it didn’t set your environmental variables properly, you might want to do that (add this to PATH –> C:\Python27).  That’s the version I used, but use whatever you want.

2. Go here and download the latest GeoIP Country Lite database, free from MaxMind.  I’m using the IPv4 for this post.  If you are so inclined, you can download the C libraries, Python libraries etc from here, but why bother.  There’s something easier.

3. It’s called pygeoip and it’s out on github for you.  A quick install Python version for you to use.  Download the Zip file.  Let’s assume your setup looks like mine, but if it doesn’t just alter things along the way to match what you have setup.

You’ve exploded GeoIP.dat.gz so that GeoIP.dat is in this folder C:\Applications\GeoIPPython

You’ve exploded pygeoip-master.zip so that it’s all in this folder C:\Applications\GeoIPPython\pygeoip-master

4. Install pygeoip now.  Open a command line and navigate to C:\Applications\GeoIPPython\pygeoip-master.  Run “python setup.py install

5. Go into the tests subfolder and create a new file.  I created one called “mytest.py“.  Here were my contents, but you can put whatever.


import pygeoip
gi4 = pygeoip.GeoIP('C:\Applications\GeoIPPython\GeoIP.dat', pygeoip.MEMORY_CACHE)
print gi4.country_name_by_addr('173.194.37.48')

6. Run your file.  Enter mytest.py from the command line.  Now, that will tell me google.com (which is 173.194.37.48) is hanging out here in the United States.

And Kapow

I can now say I’ve written a few lines of Python in my life.  Not exactly something on the bucket list, but it’s not nothing.

Advertisements
Previous Post
Leave a comment

5 Comments

  1. Genius! Very helpful.

    Reply
  2. Cool! 🙂

    Reply
    • Thanks…it’s a great way for non-technical people to do something that will amaze themselves. They’ll feel super technical if they follow it! :>

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: