Looking to implement better geo-location with Python.
I had replied to similar question at here: How to find location with IP address in Python?, so let me put the answer on here also with the installation step.
Assuming that you got the ip address already, you can try to use the IP2Location Python Library to get the user location. First of all you would have to install the IP2Location Python Library. You can install it by using PyPi: pip install IP2Location
, or download the release from here: https://github.com/chrislim2888/IP2Location-Python/releases. After that, you can use the library along with the database downloaded from IP2Location or IP2Location LITE like this:
import os
import IP2Location
database = IP2Location.IP2Location(os.path.join("data", "IP2LOCATION-LITE-DB11.BIN"))
rec = database.get_all(ip)
print(rec.country_short)
print(rec.country_long)
print(rec.region)
print(rec.city)
print(rec.isp)
print(rec.latitude)
print(rec.longitude)
print(rec.domain)
print(rec.zipcode)
print(rec.timezone)
print(rec.netspeed)
print(rec.idd_code)
print(rec.area_code)
print(rec.weather_code)
print(rec.weather_name)
print(rec.mcc)
print(rec.mnc)
print(rec.mobile_brand)
print(rec.elevation)
print(rec.usage_type)
Depends on your requirement, for example if you want to get the user's country name and region name, you can do this:
import os
import IP2Location
database = IP2Location.IP2Location(os.path.join("data", "IP2LOCATION-LITE-DB11.BIN"))
rec = database.get_all(ip)
user_country = rec.country_long
user_region = rec.region
For more details, you can visit here: https://www.ip2location.com/developers/python
Github link: https://github.com/chrislim2888/IP2Location-Python
Hostip.info is an open-source project with the goal to build/maintain a database mapping IP addresses to cities. Their about page explains the data sources relied on to populate this database.
Using HostIP, there are two ways to get location data from an IP address:
They also have a well-designed and easy-to-use RESTFUL API: just pass in your ip address after the i***p=*** in the GET request string):
import urllib
response = urllib.urlopen('http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true').read()
print(response)
Second, the Project Website also makes its complete database available for download.
I'm using ipinfodb, is free (registration required) and has 2 queries per sec limit and seems to be accurate.
try:
http://api.ipinfodb.com/v3/ip-city/?key={{API_KEY}}&ip=190.188.221.244&timezone=true
returns:
OK;;190.188.221.244;AR;ARGENTINA;BUENOS AIRES;LA PLATA;-;-34.931;-57.949;-03:00
Install the Python module pygeoip
C:>pip install pygeoip
Download the binary file of GeoLite City from here: https://dev.maxmind.com/geoip/legacy/geolite/
Then:
>>> import pygeoip
>>> g = pygeoip.GeoIP('GeoLiteCity.dat')
>>> ip = '134.222.199.110' #just an example
>>> g.record_by_addr(ip)['latitude']
52.38239999999999
>>> g.record_by_addr(ip)['longitude']
4.899499999999989
>>> g.record_by_addr(ip)['time_zone']
'Europe/Amsterdam'
Contrary to the freegeoip solution I see in the other comments, this option has no limits on the number of IPs per hour, can be used locally without Internet connection, and the geocoordinates are generally more accurate.
Found https://freegeoip.net/; python sample below.
import requests
FREEGEOPIP_URL = 'http://freegeoip.net/json/'
SAMPLE_RESPONSE = """{
"ip":"108.46.131.77",
"country_code":"US",
"country_name":"United States",
"region_code":"NY",
"region_name":"New York",
"city":"Brooklyn",
"zip_code":"11249",
"time_zone":"America/New_York",
"latitude":40.645,
"longitude":-73.945,
"metro_code":501
}"""
def get_geolocation_for_ip(ip):
url = '{}/{}'.format(FREEGEOPIP_URL, ip)
response = requests.get(url)
response.raise_for_status()
return response.json()
"Geopy makes it easy for developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources, such as wikis.
geopy currently includes support for six geocoders: Google Maps, Yahoo! Maps, Windows Local Live (Virtual Earth), geocoder.us, GeoNames, MediaWiki pages (with the GIS extension), and Semantic MediaWiki pages. "