Preparatory work
To associate IP with a physical location, we need to have a database that contains such a corresponding relationship.
We can use the open source database geolitecity, which can more accurately associate the IP address with the city in which it resides.
: http://dev.maxmind.com/geoip/legacy/geolite/
After download we unzip: xz-d GeoLiteCity.dat.xz, such as:/my/lib/ip/geolitecity.dat
Install the Pygeoip library. This library is used to query the Geolitecity database
Code:
#!/usr/bin/python#--*--coding=utf-8--*--import PYGEOIPGI = Pygeoip. GeoIP ('/my/lib/ip/geolitecity.dat ') def Printrecord (TGT): rec = gi.record_by_addr (TGT) city = rec[' city ']region = rec[' Region_code ']country = rec[' country_name ']long = rec[' longitude ']lat = rec[' latitude ']print ' [*] Host: ' + TGT + ' Geo-locat ed. ' Print ' [+] ' + str (city) + ', ' +str (region) + ', ' +str (country) print ' [+] Longitude: ' +str (LAT) + ', Dimension: ' + str (long) TGT = ' 183. 141.110.74 ' Printrecord (TGT)
183.141.110.74 is a random proxy IP address, check the address:
Query results
Python stunt-associating IP addresses and physical locations with Pygeoip