Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #17 from smcveigh941/master
Browse files Browse the repository at this point in the history
Issue #13 : Added Python code snippet for geo locating an IP address
  • Loading branch information
sukhdeepg authored Oct 2, 2019
2 parents 2ff9c9b + 1605aa0 commit caa5c9c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Python/geo_locate_ip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Useful python code snippet for quickly geolocating an IP address

import re
import sys
import urllib2
import BeautifulSoup

usage = "Run the script: ./geolocate.py <ip address>"

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()

0 comments on commit caa5c9c

Please sign in to comment.