Jun 26, 2013

IP Geolocator (Python Script)

Here's the 1st modified python script to perform geolocation checking on whois. By supplying an IP address, it will locate the country and the whois[OrgName].

import re
import sys
import urllib2
import BeautifulSoup
 
usage = "Run the script: ./geolocate.py IPAddress"
if len(sys.argv)!=2:
    print(usage)
    sys.exit(0)
if len(sys.argv) > 1:
    ipaddr = sys.argv[1]
 
geody = "http://www.geody.com/geoip.php?ip=" + ipaddr
html_page = urllib2.urlopen(geody).read()
soup = BeautifulSoup.BeautifulSoup(html_page)
# Filter paragraph containing geolocation info.
paragraph = soup('p')[3]
# Remove html tags using regex.
geo_txt = re.sub(r'<.*?>', '', str(paragraph))
print geo_txt[32:].strip()