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